當前位置:首頁 » 編程語言 » 中國象棋c語言

中國象棋c語言

發布時間: 2023-03-24 20:31:17

c語言實現中國象棋馬的走法

首先要定義一個ChessBoard[10][9];
其次計算出馬所在的輪斗旦當銷蠢前位置chessboard[x][y];
然後計算出四個馬臘擾腿的位置,分別是chessboard[x-1][y],[x+1][y],[x][y-1],[x][y+1]
最後判斷八個位置是否可行。

㈡ 博雅象棋只能說給的語言是什麼

中國象棋作為中華文化的瑰寶,所承載的益智文化源遠流長。棋盤上十橫九豎,九十個交叉點,旗子縱橫馳騁,延續著古人作戰時「擒賊先擒王」的戰略布局,可謂是猛源古代文化益智益心的智力傳承。而有別於朝鮮象棋、國際象棋,中國象棋屬於「象棋C語言」(象棋China),是中國古代智慧的結晶。

而作為現代計算機程序編輯語言的代表—— 「C語言」,它承載著是現代科技探索的廣闊可能性。博雅互動作為一家棋牌科技型公司,秉持著做中國百年棋牌文化的傳播者,也投入了大量的科技人才,用來構建鏈接科技與文化的橋梁。

博雅中國象棋於2012年就在手機端上線,真正做到了代表科技的「C語言」來承載 「中國象棋C語言」。

在「博雅杯」象棋公益賽的決賽現場,也看到了這種新科技與老文化傳承的交替。決賽環節大家還是運用「老文化」中的實體棋在棋盤猛消上廝殺。每個人都凝神靜氣枝知知,表情嚴肅。

㈢ c語言 象棋 走法 表示

語言象棋走法分三種:進、退、平。
象棋棋盤的九條豎線從左到右排列為:
紅方:一、二、三、四、五、六、七、八、九
黑方:1、2、3、4、5、6、7、8、9

進:如馬二進三,就是馬從第二條豎線向前方(對方的方向)走到第三條豎線上。

退:如車一退一,就是把車向後(自己的方向)走一個格子;車一平二,就是向後走兩個格子。 ——(兵、卒不可退)

平:如炮二平五,就是第二條線的炮走到第五條線上。 ——(馬不可平)

如果兩只馬在同一條線上,那麼可以說成前馬進一、後馬退三......

紅棋與黑棋不同:如果紅棋走炮二平五,黑棋也想走同樣的棋路,就要寫成炮2平5.

㈣ 求用C語言做的中國象棋程序,人人對戰的,不必人機對戰,能在TC下運行的,謝謝了

#include <graphics.h>
#include<conio.h>
#include<string.h>
#include<bios.h>

void qizi(int,int,char *);
void qipan();
void jiemian();
main()
{
int gdriver,gmode,i=0,c=0;
gdriver=DETECT;
gmode=0;
printf("%d,%d",gdriver,gmode);
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"c:\\tc");

while(c!=27)
{
c=getch();
clrscr();
jiemian(i);
if(c==80)
{
i++;
if(i==4)
{
i=0;
}
}
if(i==1)
{
if(c==13)
{
clrscr();
qipan();
break;
}
}
}
getch();
closegraph();
restorecrtmode();
return 0;
}
void qipan()
{
int i,j;
char *a1="A",*a2="B",*a3="C",*a4="D",*a5="E",*a6="F",*a7="G",*a8="H",*a9="I",
*a10="J",*a11="K",*a12="L",*a13="M",*a14="N";
setbkcolor(GREEN);
setlinestyle(0,0,3);
setcolor(1);
rectangle(20,20,360,400);
rectangle(30,30,350,390);
for(i=1;i<8;i++)
{
setlinestyle(0,0,3);
line(i*40+30,30,i*40+30,190);
line(i*40+30,230,i*40+30,390);
}
for(j=1;j<9;j++)
{
setlinestyle(0,0,3);
line(30,j*40+30,350,j*40+30);
}
setlinestyle(3,0,3);
line(150,30,230,110);
line(230,30,150,110);
line(150,310,230,390);
line(230,310,150,390);
setusercharsize(4,1,2,1);
settextstyle(1,0,4);
outtextxy(70,195,"chinese chess");

qizi(190,30,a1);
qizi(150,30,a2);
qizi(230,30,a2);
qizi(110,30,a3);
qizi(270,30,a3);
qizi(70,30,a4);
qizi(310,30,a4);
qizi(30,30,a5);
qizi(350,30,a5);
qizi(70,110,a6);
qizi(310,110,a6);
qizi(30,150,a7);
qizi(110,150,a7);
qizi(190,150,a7);
qizi(270,150,a7);
qizi(350,150,a7);
qizi(190,390,a8);
qizi(150,390,a9);
qizi(230,390,a9);
qizi(110,390,a10);
qizi(270,390,a10);
qizi(70,390,a11);
qizi(310,390,a11);
qizi(30,390,a12);
qizi(350,390,a12);
qizi(70,310,a13);
qizi(310,310,a13);
qizi(30,270,a14);
qizi(110,270,a14);
qizi(190,270,a14);
qizi(270,270,a14);
qizi(350,270,a14);

setcolor(BLUE);
rectangle(30,410,610,460);
setcolor(4);
outtextxy(40,420,"A->shuai B->shi C->xiang D->ma E->che F->pao G->bing");
setcolor(8);
outtextxy(40,440,"H->jiang I->shi J->xiang K->ma L->che M->pao N->zu");
return;
}
void qizi(int x,int y,char *q)
{
int *p="G";
if(strcmp(q,p)<1)
{
setlinestyle(0,0,3);
setcolor(RED);
circle(x,y,15);
setfillstyle(0,3);
floodfill(x,y,RED);
}
else
{
setlinestyle(0,0,3);
setcolor(DARKGRAY);
circle(x,y,15);
setfillstyle(0,3);
floodfill(x,y,DARKGRAY);
}
settextstyle(0,0,2);
outtextxy(x-7,y-7,q);
return;
}

void jiemian(int i)
{
setbkcolor(GREEN);
settextstyle(1,0,8);
setcolor(BLUE);
outtextxy(50,70,"chinese chess");
settextstyle(0,0,3);
setcolor(RED);
outtextxy(260,215,"start");
outtextxy(260,255,"again");
outtextxy(260,295,"undo");
outtextxy(260,335,"exit");
rectangle(250,210+i*40,390,240+i*40);
}

㈤ 急需用c語言寫中國象棋的代碼,只要紅色方布局和走棋

接上面中國象棋代碼:

if( check_turn == 12) //相的走法規范

{ if((x == check_x && y == check_y))

{temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 "); printf("還是你的回合"); Sleep(500);

}

else if( x >= 15 &&(abs(y - check_y) == 8 && abs(x - check_x) == 4))

{if((x == 22 && (y == 11 || y == 27))||(x == 18 &&

( y == 3 || y == 19 || y == 35)) ||(x == 14 && (y == 11|| y ==27)))

{ if( map[(x+check_x)/2][(y+check_y)/2] == '+') check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else {printf("棋子卡住,不可執行"); Sleep(500); } }

else {printf("不合法的下法 ");Sleep(500); }

}

else {printf("不合法的下法 "); Sleep(500); }

}

if( check_turn == 13) //士的走法規范

{ if((x == check_x && y == check_y))

{temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 "); printf("還是你的回合"); Sleep(500); }

else if( abs(x - check_x)== 2 && abs( y - check_y) == 4 &&((x==22 &&(y == 15

|| y == 23)) || ( x == 20 && y == 19) || ( x == 18 && ( y == 15 || y == 23)))) {check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map); }

else { printf("不合法的下法 "); Sleep(500); } }

if( check_turn == 14) //將的走法規范

{ if((x == check_x && y == check_y))

{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 "); printf("還是你的回合"); Sleep(500); }

else if( ((abs(x - check_x)== 2 && abs( y - check_y) == 0 )|| (abs(x - check_x)== 0

&&abs( y - check_y) == 4)) && x >= 18 && x <= 22 && y >= 15 && y <= 23 )

{ check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map); }

else { printf("不合法的下法 "); Sleep(500); } }

if( check_turn == 15) //炮的走法規范

{ if((x == check_x && y == check_y))

{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 "); printf("還是你的回合"); Sleep(500); }

else if( y == check_y )

{ int check_pao = 0;

if( x > check_x)

{ for(j = check_x + 2; j<= x ;j = j+ 2)

{ if(map[j][y] == '+' ); else check_pao++;}

if(check_pao == 1&& temp == '+') // 直線行走但不可吃棋子 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else if( check_pao == 2 && temp != '+') //跳躍吃棋 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("不合法的下法 "); Sleep(500); } }

else { for(j = check_x - 2; j>= x;j = j - 2)

{ if(map[j][y] == '+' ); else { check_pao++;} }

if(check_pao == 1&& temp == '+') //直線行走但不可吃棋子 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else if( check_pao == 2 && temp != '+') //跳躍吃棋 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("不合法的下法 "); Sleep(500); } }

}

else if( x == check_x )

{ int check_pao = 0;

if( y > check_y)

{ for(j = check_y + 4; j<= y ;j = j+4)

{ if(map[x][j] == '+' ); else check_pao++;}

if(check_pao == 1&& temp == '+') //直線行走但不可吃棋子 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else if( check_pao == 2 && temp != '+') //跳躍吃棋 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("不合法的下法 "); Sleep(500); } }

else {for(j = check_y - 4; j>= y;j = j - 4)

{if(map[x][j] == '+' ); else check_pao++;}

if(check_pao == 1&& temp == '+') //直線行走但不可吃棋子 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else if( check_pao == 2 && temp != '+') //跳躍吃棋 check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("不合法的下法 "); Sleep(500); } }

}

else { printf("不合法的下法 ");Sleep(500); }

}

if( check_turn == 16) //卒的走法規范

{ if ( x >= 14)

{ if((x == check_x && y == check_y))

{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 "); printf("還是你的回合"); Sleep(500); }

else if( x == check_x - 2 && y == check_y) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("不合法的下法 "); Sleep(500); }

}

else{ if((x == check_x && y == check_y))

{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 "); printf("還是你的回合"); Sleep(500); }

else if((x - check_x == 0 && abs(y-check_y) ==4) ||( x - check_x == -2

&& abs(y-check_y) == 0)) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("不合法的下法 "); Sleep(500); } }

}

}

else { if( check_turn == 20) //車的走法規范 (帥方)

{ if((x == check_x && y == check_y))

{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 "); printf("還是你的回合"); Sleep(500); }

else if( y == check_y )

{ if( x > check_x)

{ for(j = check_x + 2; j < x;j = j + 2)

{ if(map[j][y] == '+'); else {printf("不合法的下法 "); Sleep(500); break; } }

if( j >= x) check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

}

if( x < check_x)

{ for(j = check_x - 2; j > x;j = j - 2)

{ if(map[j][y] == '+'); else { printf("不合法的下法 "); Sleep(500); break; } }

if( j <= x) check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

}

}

else if( x == check_x )

{ if( y > check_y)

{ for(j = check_y + 4; j < y;j = j + 4)

{ if(map[x][j] == '+'); else { printf("不合法的下法 "); Sleep(500); break; } }

if( j >= y) check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

}

if( y < check_y)

{ for(j = check_y - 4; j > y;j = j - 4)

{ if(map[x][j] == '+'); else { printf("不合法的下法 ");Sleep(500); break; } }

if( j <= y) check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

}

}

else { printf("不合法的下法 "); Sleep(500); }

}

if( check_turn == 21) //馬的走法規范

{ if((x == check_x && y == check_y))

{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 ");printf("還是你的回合"); Sleep(500); }

else if( (abs( x - check_x) == 2&& abs( y - check_y) == 8)&&

map[check_x][(y+check_y)/2] =='+')

{ check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map); }

else if( (abs( x - check_x) == 4&& abs( y - check_y) == 4)&&

map[(x + check_x)/2][check_y] == '+' )

{ check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map); }

else { printf("不合法的下法 ");Sleep(500); } }

if( check_turn == 22) //相的走法規范

{ if((x == check_x && y == check_y))

{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 ");printf("還是你的回合"); Sleep(500); }

else if( x <= 12 && (abs(y - check_y) == 8 && abs(x - check_x) == 4))

{ if((x == 4 && (y == 11 || y == 27))||(x == 8 && ( y == 3 || y == 19 || y == 35))

||(x == 12 && (y == 11|| y ==27)))

{ if( map[(x+check_x)/2][(y+check_y)/2] == '+') check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("棋子卡住,不可執行");Sleep(500); } }

else {printf("不合法的下法 ");Sleep(500); }

}

else { printf("不合法的下法 ");Sleep(500); } }

if( check_turn == 23) //士的走法規范

{ if((x == check_x && y == check_y))

{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 ");printf("還是你的回合"); Sleep(500); }

else if( abs(x - check_x)== 2 && abs( y - check_y) == 4 &&((x==4 &&

(y == 15 || y == 23)) || ( x == 6 && y == 19) || ( x == 8 && ( y == 15 || y == 23))))

{ check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map); }

else { printf("不合法的下法 ");Sleep(500); } }

if( check_turn == 24) //將的走法規范

{ if((x == check_x && y == check_y))

{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 ");printf("還是你的回合"); Sleep(500); }

else if( ((abs(x - check_x)== 2 && abs( y - check_y) == 0 )|| (abs(x - check_x)== 0 &&abs( y - check_y) == 4)) && x >= 4 && x <= 8 && y >= 15 && y <= 23 )

{ check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map); }

else {printf("不合法的下法 ");Sleep(500); } }

if( check_turn == 25) //炮的走法規范

{ if((x == check_x && y == check_y))

{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 ");printf("還是你的回合"); Sleep(500); }

else if( y == check_y )

{ int check_pao = 0;

if( x > check_x)

{ for(j = check_x + 2; j<= x ;j = j+ 2)

{ if(map[j][y] == '+' ); else check_pao++;}

if(check_pao == 1&& temp == '+') //直線行走但不可吃棋子 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else if( check_pao == 2 && temp != '+') //跳躍吃棋 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("不合法的下法 ");Sleep(500); } }

else { for(j = check_x - 2; j>= x;j = j - 2)

{ if(map[j][y] == '+' ); else { check_pao++;} }

if(check_pao == 1&& temp== '+') //直線行走但不可吃棋子 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else if( check_pao == 2 && temp != '+') //跳躍吃棋 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("不合法的下法 ");Sleep(500); } }

}

else if( x == check_x )

{ int check_pao = 0;

if( y > check_y)

{ for(j = check_y + 4; j<= y ;j = j+4)

{ if(map[x][j] == '+' ); else check_pao++;}

if(check_pao == 1&& temp == '+') //直線行走但不可吃棋 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else if( check_pao == 2 && temp != '+') //跳躍吃棋 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("不合法的下法 ");Sleep(500); } }

else { for(j = check_y - 4 ; j>= y;j = j - 4)

{ if(map[x][j] == '+' ); else check_pao++;}

if(check_pao ==1&& temp == '+') //直線行走但不可吃棋子 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else if( check_pao == 2&& temp != '+') //跳躍吃棋 check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("不合法的下法 ");Sleep(500); } }

}

else { printf("不合法的下法 ");Sleep(500); }

}

if( check_turn == 26) //卒的走法規范

{ if( x <= 12)

{ if((x == check_x && y == check_y))

{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 "); printf("還是你的回合"); Sleep(500); }

else if( x == check_x + 2 && y == check_y) check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("不合法的下法 ");Sleep(500); } }

else{ if((x == check_x && y == check_y))

{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;

printf("三思而後行 ");printf("還是你的回合"); Sleep(500); }

else if((x - check_x == 0 && abs(y-check_y) ==4) ||( x - check_x == 2

&& abs(y-check_y) == 0)) check_main2(&temp,&temp1,&turn,&turn1,&num,&if_return,map);

else { printf("不合法的下法 ");Sleep(500); } }

}

}

}

}

system("cls");

if( if_return) return;

for(i = 0; i < 27; i++)puts(map[i]);

}

Sleep(5000);

}

int main( )

{ while(1)

{ xiangqi( );

printf(" 重來,請按鍵. ");

getch( );

}

return 0;

}

㈥ 求C語言程序,在中國象棋棋盤上,放置一個馬,能夠不重復走過棋盤的每一個位置,輸出九十個坐標

#include<stdio.h>
/*
問題描述:在n*m的棋盤上,馬只能走日字。馬從(x,y)出發,把棋盤的每一個點都走一遍,且只走一次,

找出所有路徑。
*/
void find(int x,int y,int dep);
int check(int x,int y);
void output();

int n=5,m=4;
int fx[8]={1,2,2,1,-1,-2,-2,-1};
int fy[8]={2,1,-1,-2,-2,-1,1,2};
int a[5][4];
int dep,x,y,count;
int main()
{
int i,j;
dep=1;////記錄遞歸深度,即走過的點的個數,當dep=n*m,找到一組解
count=0;

printf("Please input the original point:\n");
scanf("%d%d",&x,&y);

if(x<0 || x>4 || y<0 || y>3)
{
printf("wrong data!\n");
return 1;
}

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=0;
}
}

a[x][y]=1;

find(x,y,2);
if(count==0)
{
printf("NO solution!\n");
}
else
{
printf("Total count=%d\n",count);
}
return 0;
}

void find(int x,int y,int dep)
{
int i;
int xx,yy;
for(i=0;i<8;i++)//加上方向增量,形成新的坐標
{
xx=x+fx[i];
yy=y+fy[i];

if(check(xx,yy)==1)//判斷坐標是否出界,或者已經走過
{
a[xx][yy]=dep;//走向新的坐標
if(dep==n*m)
{
output();
}
else
{
find(xx,yy,dep+1);//從新的坐標出發,遞歸下一層
}
a[xx][yy]=0;//回溯,恢復未走標志
}
}
}

int check(int x,int y)
{
int flag = 1;
if(x<0 || x>4 || y<0 || y>3)
{
flag = 0;
}
if(a[x][y]!=0)
{
flag=0;
}
return flag;
}

void output()
{
count++;
printf("\ncount=%d\n",count);

int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
}
//原始文件請參考:CSDN馬的遍歷問題

㈦ 急需用c語言寫中國象棋的代碼,只要紅色方布局和走棋

#include #include #include #include #include
int x,y,i,j,k,p,q, num = 1, round; //象棋游戲的全局變數
int place_x1 = 0,place_y1 = 0,place_x2 = 0,place_y2 = 0;
int check_x,check_y,check_turn; //基本參數
char ch, turn = 'O',turn1 = 'N',temp,temp1;
char check_1[9][3] ={"車","馬","象","士","將","炮","兵","+-"}; //取棋子時只判斷前8合法
char check_2[9][3] ={"車","馬","相","仕","帥","炮","卒","+-"}; //下棋時多一空位合法
char check[3];
void check_main1(char* temp,char* temp1,char* turn,char* turn1,int *num,int *if_return,char map[100][100])
{ //(象棋函數 判斷 將方 下棋是否合法
check[0] = *temp; check[1] = *temp1; check[2] = '\0'; char a,b;
for ( i = 0; i < 8; i++)
{ if ( strcmp(check_2[i],check) == 0)
{ *temp = *turn; *temp1 = *turn1; *turn = 'O'; *turn1 = 'N';
if( i < 7){ printf(" 帥方的%s被吃\n",check_2[i]); Sleep(500); } *num = *num + 1;
for( k = 4; k <= 8; k = k + 2) //判斷 帥 是否死亡
{ for(j = 15; j <= 23; j= j+ 4)
{ if (map[k][j] == check_2[4][0] && map[k][j+1] == check_2[4][1])
{ place_x2 = k; place_y2 = j; break; } }
if( j <= 23) break;
}
if( k == 10)
{printf(" 帥 被將死 將方獲得勝利\n"); printf("按任意鍵返回菜單");
getch( ); *if_return = 1; return;
}
for( k = 18; k <= 22; k = k + 2) //判斷 將 是否死亡
{for(j = 15; j <= 23; j= j+ 4)
{if(map[k][j] == check_1[4][0] && map[k][j+1] == check_1[4][1])
{place_x1 = k; place_y1 = j; break; } }
if( j <= 23) break;
}
if ( k == 24)
{printf(" 將 被將死 帥方獲得勝利\n"); printf("按任意鍵返回菜單");
getch( ); *if_return = 1; return;
}
if ( place_y1 == place_y2)
{for( k = place_x2 + 2; k <= place_x1 - 2; k = k +2) {if(map[k][place_y1] != '+') break;}
if( k == place_x1)
{if(round == 1) printf(" 將方對將 帥方勝利");
else if( round == 2) printf(" 帥方對將 將方勝利");
printf("按任意鍵返回菜單"); getch( ); *if_return = 1; return;
}
}
break;
}
} // for ( i = 0; i < 8; i++)循環結束
if( i == 8) {printf("不合法的走法\n"); Sleep(500); }
}
void check_main2(char* temp,char* temp1,char* turn,char* turn1,int *num,int *if_return,char map[100][100])
{ //象棋函數 判斷 帥方 下棋是否合法
check[0] = *temp; check[1] = *temp1; check[2] = '\0'; char a,b;
for ( i = 0; i < 8; i++)
{if ( strcmp(check_1[i],check) == 0)
{ *temp = *turn; *temp1 = *turn1; *turn = 'O'; *turn1 = 'N';
if( i < 7) {printf(" 將方的%s被吃",check_1[i]); Sleep(500); } *num = *num + 1;
for( k = 4; k <= 8; k = k + 2) //判斷 帥 是否死亡
{for(j = 15; j <= 23; j= j+ 4)
{if(map[k][j] == check_2[4][0] && map[k][j+1] == check_2[4][1])
{place_x2 = k; place_y2 = j; break; } }
if( j <= 23) break;
}
if( k == 10)
{printf(" 帥 被將死 將方獲得勝利\n");printf("按任意鍵返回菜單"); getch( );
*if_return = 1; return;
}
for( k = 18; k <= 22; k = k + 2) //判斷 將 是否死亡
{for(j = 15; j <= 23; j= j+ 4)
{if(map[k][j] == check_1[4][0] && map[k][j+1] == check_1[4][1])
{place_x1 = k; place_y1 = j; break; } }
if( j <= 23) break;
}
if( k == 24)
{printf(" 將 被將死 帥方獲得勝利\n");printf("按任意鍵返回菜單"); getch( );
*if_return = 1; return; }
if( place_y1 == place_y2)
{for( k=place_x2 + 2; k <= place_x1 - 2; k=k +2) {if(map[k][place_y1] != '+') break; }
if( k == place_x1)
{if(round==1)printf(" 將方對將 帥方勝利");else if(round==2)printf(" 帥方對將 將方勝利");
printf("按任意鍵返回菜單"); getch( ); *if_return = 1; return; }
}
break;
}
} // for ( i = 0; i < 8; i++)循環結束
if( i == 8) {printf("不合法的走法\n"); Sleep(500); }
}
void xiangqi( ) //象棋主程序
{ char map[100][100]= { "[[===================================]]",
"[| ①將 【象棋】 ②帥 |]",
"[[===================================]]",
"[[-----------------------------------]]",
"[[ 車—-馬—-相—-仕—-帥—-仕—-相—-馬—-車]]",
"[[ | | | | \\ | / | | | | ]]",
"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",
"[[ | | | | / | \\ | | | | ]]",
"[[ +-—-炮—-+-—-+-—-+-—-+-—-+-—-炮—-+-]]",
"[[ | | | | | | | | | ]]",
"[[ 卒—-+-—-卒—-+-—-卒—-+-—-卒—-+-—-卒]]",
"[[ | | | | | | | | | ]]",
"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",
"[[===================================]]",
"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",
"[[ | | | | | | | | | ]]",
"[[ 兵—-+-—-兵—-+-—-兵—-+-—-兵—-+-—-兵]]",
"[[ | | | | | | | | | ]]",
"[[ +-—-炮—-+-—-+-—-+-—-+-—-+-—-炮—-+-]]",
"[[ | | | | \\ | / | | | | ]]",
"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",
"[[ | | | | / | \\ | | | | ]]",
"[[ 車—-馬—-象—-士—-將—-士—-象—-馬—-車]]",
"[[-----------------------------------]]",
"[[===================================]]"};
int if_return = 0;
system("mode con cols=45 lines=32"); //迷你界面
system("color 70");
printf("[[==================================]]\n");
printf("[[ -------------------------------- ]]\n");
printf("[[ | | ]]\n");
printf("[[ | 【<>】 | ]]\n");
printf("[[ | | ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ | 控制wasd雙方輪流控制指針下棋| ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ | 鍵盤輸入大小寫 ' M ' | ]]\n");
printf("[[ | 都視為確認下棋 | ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ | 為了方便區分棋子 | ]]\n");
printf("[[ | 後手方全設為繁體復雜字體 | ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ | 我已閱讀規則,按任意鍵繼續 | ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[==================================]]\n");
getch( ); system("mode con cols=45 lines=32"); //迷你界面
system("color 70");
for ( i = 0; i < 27; i++){ puts(map[i]); Sleep(100); }
x = 6, y = 19; temp = map[x][y]; temp1 = map[x][y+1];
while(num)
{ if (num % 2 == 1 &&num / 2 % 2 == 0){ printf(" 現在是'將'的回合\n");round = 1; }
else if( num %2 == 1){ printf(" 現在輪到'帥'的回合了\n");round = 2; }
ch = getch( );
if ( ch == 's') //下移
{ if ( map[x+1][y]!= '-')
{map[x][y] =temp; map[x][y+1] = temp1; x = x + 2;
temp = map[x][y]; temp1 = map[x][y+1]; map[x][y] = turn; map[x][y+1] = turn1; }
}
else if ( ch == 'a') //左移
{ if (map[x][y-1]!=' ')
{map[x][y] =temp; map[x][y+1] = temp1; y = y - 4;
temp = map[x][y]; temp1 = map[x][y+1]; map[x][y] = turn; map[x][y+1] = turn1; }
}
else if ( ch == 'w') //上移
{ if ( map[x-1][y]!= '-')
{map[x][y] =temp; map[x][y+1] = temp1; x = x - 2; temp = map[x][y];
temp1 = map[x][y+1]; map[x][y] = turn; map[x][y+1] = turn1; }
}
else if ( ch == 'd') //右移
{ if (map[x][y+2]!=']')
{map[x][y] =temp; map[x][y+1] = temp1; y = y + 4; temp = map[x][y];
temp1 = map[x][y+1]; map[x][y] = turn; map[x][y+1] = turn1; }
}
else if( ch == 'm' || ch =='M') //M確認要移動的棋子,或確認要移到的目的地
{ if (num % 2 == 1 && temp != '+' && temp1 != '-') //取棋
{check[0] = temp; check[1] = temp1; check[2] = '\0';
if ( round == 1)
{ for ( i = 0; i < 7; i++) //將方
{ if ( strcmp(check_1[i],check) == 0)
{turn = temp; turn1 = temp1; temp = '+'; temp1 = '-';
check_x = x; check_y = y; check_turn = 10 + i; num++; break; }
}
if( i == 7){ printf("這不是你的棋子\n"); Sleep(500); }
}
else if( round == 2)
{for ( i = 0; i < 7; i++) //帥方
{ if( strcmp(check_2[i],check) == 0)
{turn = temp; turn1 = temp1; temp = '+'; temp1 = '-';
check_x = x; check_y = y; check_turn = 20 + i; num++; break; }
}
if( i == 7){ printf("這不是你的棋子\n"); Sleep(500); }
}
}
else if( num % 2 == 0) //放棋
{ char check_1[8][3] ={"車","馬","象","士","將","炮","卒","+-"};
char check_2[8][3] ={"俥","馬","相","仕","帥","軳","兵","+-"};
//中界 楚河上下坐標 12 15 往下2 往右4
if( check_turn < 20) //將方
{if( check_turn == 10) //車的走法規范
{ if((x == check_x && y == check_y))
{temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;
printf("三思而後行\n"); printf("還是你的回合"); Sleep(500); }
else if( y == check_y )
{ if( x > check_x)
{ for(j = check_x + 2; j < x;j = j + 2)
{ if(map[j][y] == '+'); else{printf("不合法的下法\n"); Sleep(500); break; } }
if( j >= x) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);
}
if( x check_x){ for(j = check_x - 2; j > x;j = j - 2)
{ if(map[j][y] == '+'); else{printf("不合法的下法\n"); Sleep(500); break; }
}
if( j <= x)check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);
}
}
else if( x == check_x )
{if( y > check_y)
{for(j = check_y + 4; j < y;j = j + 4)
{if(map[x][j] == '+'); else {printf("不合法的下法\n"); Sleep(500); break; }
}
if( j >= y) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);
}
if( y < check_y)
{for(j = check_y - 4; j > y;j = j - 4)
{ if(map[x][j] == '+'); else { printf("不合法的下法\n"); Sleep(500); break; }
}
if( j <= y) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);
}
}
else { printf("不合法的下法\n"); Sleep(500); }
}
if( check_turn == 11) //馬的走法規范
{if((x == check_x && y == check_y))
{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;
printf("三思而後行\n"); printf("還是你的回合"); Sleep(500); }
else if( (abs( x - check_x) == 2&& abs( y - check_y) == 8)&& map[check_x][(y+check_y)/2] =='+')
{check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map); }
else if( (abs( x - check_x) == 4&& abs( y - check_y) == 4)&& map[(x + check_x)/2][check_y] == '+' )
{check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map); }
else { printf("不合法的下法\n");Sleep(500); }
} //其餘代碼在後續

㈧ C語言做的中國象棋,分享,問怎麼在WIN7下運行

#include <stdio.h>
#include <memory.h>
typedef struct
{
int x, y;
}item;
item move[4] = {{-2,1}, {-1,2}, {1,2}, {2,1}};
int map[5][9], sx, sy;
//判斷是否在地圖中,防止越孝姿界
int checkIn(int x, int y)
{
if(x >= 0 && x <= 4 && y >= 0 && y <= 8)
return 1;
return 0;
}
//輸出最終結果
void output()
{
int i, j;
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 8; j++)
printf("%d ",map[i][j]);
putchar('\n');
}
printf("共計%d步。", map[0][8]);
}
//從x y開始尋兆鋒找能否到達終點,能返回1,不能返回0,step表示當前的步數。
int findPath(int x, int y, int step)
{
int i;
map[x][y] = step;
//終點
if(x == 0 && y == 8)
{
output();
return 1;
}
//向右邊4個方向前進
for(i = 0; i < 4; i++)
if(checkIn(x+move[i].x, y+move[i].y))
if(findPath(x+move[i].x, y+move[i].y, step+1) == 1)
return 1;
//四個方向均無法到達終點,回溯並返回0
map[x][y] = 0;
return 0;
}
int main(int argc, char ** argv)
{
memset(map, 0, sizeof(map));
printf("請輸入起點<x,y>\n"族慎晌);
scanf("%d,%d", &sx, &sy);
findPath(sx, sy, 1);
return 0;
}

㈨ 用C語言輸出中國象棋棋盤的源代碼!!急!急!急!

密碼:yq12

㈩ 急求:C語言編寫的中國象棋游戲一個

先弄明白數據的結構:

MantisChessDef.h里的東西一定要先看一下, 否則會摸不到頭腦的。
還有棋盤坐標:
象棋棋盤大小9x10,為了便於編程,規定棋盤每條邊留有一個元素的邊界。
這樣局衡棋盤大小(包括邊界)變成11x12。棋盤x坐標軸向右,y軸向下。
黑棋永遠在上方,在標准開局時左上角的黑車坐標是(1,1)。

局面用這三個變數表示:

static POINT g_pointChessman[32]; //棋子坐標
static int g_iChessmanMap[11][12]; //棋位狀態
static int g_iSide; //輪到哪方走

智能部分有幾個函數的前三個參數就是這個東西, 應該不難理解吧?

---------------------------------------------------------------------------------------
search函數:

先說明一下, 經常有朋友問我要原理, 但我公開源代碼是給大家一個參考, 而不是什麼教程,所以我不想說那些理論的東西。
基本原理是春臘磨α-β搜索, 很多人工智慧的教科書上都有講到, 沒看過的的趕快去找一本來啃一啃;
雖然這些書上的文字大多晦澀難懂,但畢竟講得明明白白。
沒有書的朋友請發揮一下主觀能動性, 去找一找,不要來問我要, 因為我也沒有。

我在這里只分析一下search函數:

弄懂α-β搜索後來看看這個博弈樹, 看怎麼編程實現它。

先規定一下, 我們用一個整數表示局面的好壞.
這個數越大說明局面對 "走棋方" 越有利,0表示雙方實力相等。

1a( 1) ┬ 2a(-1) ┬ 3a(-1)
│ └ 3b( 1)
└ 2b(-5) ┬ 3c( 2)
├ 3d(-4)
└ 3e( 5)

分析一下這棵樹,有這么個特點: 父結點的值 = -MAX(子結點的值)

我們還知道1、每個結點對應一個局面。2、底層的結點的值是"估"出來的。

於是我們可以寫出偽代碼了:

偽代碼: 搜索一個結點下的分支, 得到這個結點的值。

參數: 局面,搜索深度

返回值:結點的值

int search(局面,int depth)
{
if(depth!=0)//不是底層結點
{

枚舉出所有子結點(列出所有走法);

int count=子結點數;
int maxvalue= -∞;
for(int i=0;i<count;i++)//遍歷所有結點
{
算出子結點局面;

maxvalue=max(maxvalue,search(子結點局面,depth-1));
}
return -maxvalue;
}
else //是底層結點
{
return 估計值;
}
}

這就是搜索演算法的框架, 用到了遞歸。
MantisChess的智能部分函數都在MantisChessThink.cpp里, 其中search是搜索扒斗, 跟上面的這個search差不多,我把它出來注釋一下:

int Search(int tmap[11][12],POINT tmanposition[32],int &tside,int man, POINT point,int upmax,int depth)
{

//前面的三個參數就是局面。
//man 和point 是走法,用來計算本結點的局面。 這里是把計算局面放在函數的開頭,跟上面的偽代碼不太一樣。
//upmax: up - 上一層, max - 最大值, 這是α-β的剪枝用到的東西, 後面再講。
//depth: 搜索深度

int ate,cur,maxvalue,curvalue,xs,ys;
int count;

//#####################這一段是計算本結點的局面#########################################
ate=32;
//移動棋子:
xs=tmanposition[man].x;ys=tmanposition[man].y; //原坐標
if (SideOfMan[tmap[point.x][point.y]]==!tside) //目標點有對方的棋子
{
ate=tmap[point.x][point.y]; //記錄下被吃掉的棋子
if(ate==0 || ate==16)
{
return 9999;
}
tmanposition[ate].x=0; //目標點的棋子被吃掉
}

tmap[point.x][point.y]=man; //這兩行是:
tmap[xs][ys]=32; //在map上的移動
tmanposition[man]=point;
tside=!tside;
//####################################################################################

depth--;

if(depth>0) //不是底層結點
{
int chessman[125];
POINT targetpoint[125];
if(EnumList(tmap,tmanposition,tside,chessman,targetpoint,count)) //枚舉出所有子結點(列出所有走法)
{
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//這里是剪枝(不是α-β剪枝), 原理是在正式搜索之前先用較淺的搜索來得到誤差較大的值
//然後根據這些值來對子結點排序, 只保留最好的S_WIDTH個結點進行正式搜索。
//顯然,這個剪枝有一定的風險

if(depth>=2 && count>S_WIDTH+2)
{
int value[125];
cur=0;
maxvalue=-10000;
while(cur< count)
{
curvalue=Search(tmap,tmanposition,tside,chessman[cur],targetpoint[cur],-10000,depth-2);
value[cur]=curvalue;
if(curvalue>maxvalue)maxvalue=curvalue;
cur ++;
}
::Mantis_QuickSort(value,chessman,targetpoint,0,count-1); //排序
count=S_WIDTH;//剪枝
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

maxvalue=-10000;
cur=0;
while(cur< count)
{
curvalue=Search(tmap,tmanposition,tside,chessman[cur],targetpoint[cur],maxvalue,depth);
if(curvalue>maxvalue)maxvalue=curvalue;
if(curvalue>=-upmax)goto _ENDSUB; //α-β剪枝, 符合剪枝條件的就Cut掉。 這里用了goto語句了, 千萬別學我。
cur ++;
}
}
else maxvalue=9800;
}
else //是底層結點
{
maxvalue=Value(tmap,tmanposition,tside); //估值
}

_ENDSUB:

//返回之前要恢復父結點的局面
//####################################################################################
tmanposition[man].x=xs; //這兩行是:
tmanposition[man].y=ys; //在face上的恢復
tmap[xs][ys]=man; //在map上的恢復
if(ate!=32)
{
tmanposition[ate]=point;
tmap[point.x][point.y]=ate;
}
else tmap[point.x][point.y]=32;

tside=!tside;
//####################################################################################

return -maxvalue;
}

上面的代碼用到了α-β剪枝, 舉個例子就明白了:

還是這個博弈樹,從上往下遍歷。

1a( 1) ┳ 2a(-1) ┳ 3a(-1)
┃ ┗ 3b( 1)
┗ 2b(-5) ┯ 3c( 2)
├ 3d(-4)
└ 3e( 5)

2a遍歷完後 upmax=-1, 繼續遍歷完3c後返回2b, 發現3c=2>-upmax, 這時就不用管3d和3e了, 因為無論他們的值是多少 2b=-max(3c,3d,3e)<2a 一定成立,
也就是說2b可以安全地剪掉。這就是α-β剪枝。

從上面的代碼來看我的MantisChess演算法與標準的α-β剪枝搜索並沒有什麼不同, 只不過加了排序和剪枝而已。

熱點內容
得力文件夾5302 發布:2024-04-26 00:21:32 瀏覽:90
您的個人文件夾 發布:2024-04-26 00:03:12 瀏覽:67
睿雲伺服器功能介紹 發布:2024-04-25 23:59:51 瀏覽:570
標致5008怎麼連接安卓 發布:2024-04-25 23:25:08 瀏覽:793
安卓下載管理器哪個好 發布:2024-04-25 23:22:48 瀏覽:442
考試系統源碼php 發布:2024-04-25 23:09:46 瀏覽:136
磁碟禁止訪問 發布:2024-04-25 22:53:48 瀏覽:288
多線程ftp上傳 發布:2024-04-25 22:41:36 瀏覽:115
phpqrcode 發布:2024-04-25 22:41:36 瀏覽:33
桂平上網密碼是多少 發布:2024-04-25 22:32:10 瀏覽:575