本文目录一览:
c语言编程题 关于国际象棋的问题
我帮你想算法,如果你写不出来……我建议你再好好看书,要不然就放弃学这个吧……
王:计算起始与目标位置的纵坐标之差和横坐标之差,取较大值即为步数;
后:判断起始与目标位置如果在同一行、同一列或者同一斜线,如果是则步数为1,如果不是则为2;
车:判断起始与目标位置是否在同一行或者同一列,是则为1,否则为2;
象:稍微复杂,先判断是否在同色格(颜色可以通过纵横坐标之和的奇偶性判断),如果否,则输出Inf;如果是,则判断起始与目标位置是否在同一斜线,是则为1,否为2。
以上,如果不明白或者需要解释原因请补充或者hi我。
p.s.怎么没有马呢……那个比较有挑战性……
c语言国际象棋等比求解
c语言国际象棋等比求解解答如下:
/*象棋盘:"1"代表黑格,""代表白格*/
#includeintmain(){
for(inti=1;ifor(intj=1;jif((i+j)%2==0)//当行数+列数为偶数时打印“1”printf("1");
elseprintf("");//当行数+列数为奇数时打印“”
printf("\n");
returno;
等比数列是指从第二项起,每一项与它的前一项的比值等于同一个常数的一种数列,常用G、P表示。这个常数叫做等比数列的公比,公比通常用字母q表示(q≠0),等比数列a1≠0。其中{an}中的每一项均不为0。注:q=1时,an为常数列。
棋盘为正方形,由64个黑白(深色与浅色)相间的格子组成;棋子分黑白(深色与浅色)两方共32枚,每方各16枚。虽然汉语称之为西洋棋或国际象棋,但是实际上它起源于亚洲,后由阿拉伯人传入欧洲,成为国际通行棋种,也是一项智力竞技运动,曾一度被列为奥林匹克运动会正式比赛项目。
C语言,中国象棋 马走日 问题,求解,急!!!
本着乐于助人以及自我锻炼的原则,帮楼主敲了敲代码,还有疑问的话请追问。
#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语言写中国象棋的代码,只要红色方布局和走棋
接上面中国象棋代码:
if( check_turn == 12) //相的走法规范
{ if((x == check_x y == check_y))
{temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;
printf("三思而后行\n"); 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("不合法的下法\n");Sleep(500); }
}
else {printf("不合法的下法\n"); Sleep(500); }
}
if( check_turn == 13) //士的走法规范
{ 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) == 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("不合法的下法\n"); Sleep(500); } }
if( check_turn == 14) //将的走法规范
{ 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) == 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("不合法的下法\n"); Sleep(500); } }
if( check_turn == 15) //炮的走法规范
{ 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 )
{ 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("不合法的下法\n"); 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("不合法的下法\n"); 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("不合法的下法\n"); 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("不合法的下法\n"); Sleep(500); } }
}
else { printf("不合法的下法\n");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("三思而后行\n"); printf("还是你的回合"); Sleep(500); }
else if( x == check_x - 2 y == check_y) check_main1(temp,temp1,turn,turn1,num,if_return,map);
else { printf("不合法的下法\n"); Sleep(500); }
}
else{ if((x == check_x y == check_y))
{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;
printf("三思而后行\n"); 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("不合法的下法\n"); Sleep(500); } }
}
}
else { if( check_turn == 20) //车的走法规范 (帅方)
{ 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_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("不合法的下法\n"); 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("不合法的下法\n"); 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("不合法的下法\n");Sleep(500); break; } }
if( j = y) check_main2(temp,temp1,turn,turn1,num,if_return,map);
}
}
else { printf("不合法的下法\n"); Sleep(500); }
}
if( check_turn == 21) //马的走法规范
{ 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_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("不合法的下法\n");Sleep(500); } }
if( check_turn == 22) //相的走法规范
{ if((x == check_x y == check_y))
{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;
printf("三思而后行\n");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("不合法的下法\n");Sleep(500); }
}
else { printf("不合法的下法\n");Sleep(500); } }
if( check_turn == 23) //士的走法规范
{ 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) == 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("不合法的下法\n");Sleep(500); } }
if( check_turn == 24) //将的走法规范
{ 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) == 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("不合法的下法\n");Sleep(500); } }
if( check_turn == 25) //炮的走法规范
{ 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 )
{ 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("不合法的下法\n");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("不合法的下法\n");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("不合法的下法\n");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("不合法的下法\n");Sleep(500); } }
}
else { printf("不合法的下法\n");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("三思而后行\n"); printf("还是你的回合"); Sleep(500); }
else if( x == check_x + 2 y == check_y) check_main2(temp,temp1,turn,turn1,num,if_return,map);
else { printf("不合法的下法\n");Sleep(500); } }
else{ if((x == check_x y == check_y))
{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;
printf("三思而后行\n");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("不合法的下法\n");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("\n 重来,请按键.\n");
getch( );
}
return 0;
}