接上面中国象棋代码:
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 + 2j<= 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 - 2j>= xj = 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 + 4j<= 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 - 4j>= yj = 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 = turn1turn = 'O'turn1 = 'N' num--
printf("三思而后行\n")printf("还是你的回合")Sleep(500) }
else if( y == check_y )
{ if( x >check_x)
{ for(j = check_x + 2j <xj = 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 - 2j >xj = 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 + 4j <yj = 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 - 4j >yj = 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 = turntemp1 = turn1turn = '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 = turntemp1 = turn1turn = '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 = turntemp1 = turn1turn = '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 = turntemp1 = turn1turn = '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 = turn1turn = '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 + 2j<= 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 - 2j>= xj = 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 + 4j<= 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>= yj = 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 = turntemp1 = turn1turn = '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 = turntemp1 = turn1turn = '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 = 0i <27i++)puts(map[i])
}
Sleep(5000)
}
int main( )
{ while(1)
{ xiangqi( )
printf("\n 重来,请按键.\n")
getch( )
}
return 0
}
记得以前上学的时候做过的,个人推荐:先用线条画出棋盘,然后在棋盘的90的点都画好棋子(可以用image控件画圆,lable控件显示棋子内容),并编成10*9的二维数组,利用控件image的visible属性控制是否显示,控件lable控制显示的内容。
个人认为这个应该比利用坐标让控件移动的方法简单
#include "stdio.h"#define N 8
int w=0
int way1[8]={-2,-1,1,2, 2, 1,-1,-2}
int way2[8]={ 1,2, 2,1,-1,-2,-2,-1}
int ch[N*N]={0}
int a[N*N+1][3]={0}
int dir[N][N][8]
int st=1
char c=y
int weight[N][N]
void caculate()
void dirctions()
void print()
int check(int i,int j)
void caculate() /*计算各点的权值*/
{
int i,j,k
for(i=1i<=Ni++)
for(j=1j<=Nj++)
for(k=0k<Nk++)
{
int x,y
x=i+way1[k]
y=j+way2[k]
if(x>=1&&x<=N&&y>=1&&y<=N)
weight[i-1][j-1]++
}
}
int check(int i,int j) /*检查(i,j)是否在棋盘内*/
{
if(i<1||i>8||j<1||j>8)
return 0
return 1
}
void directions() /*求出各点的最佳方向序列,即优先向权值小的方向*/
{
int i,j,k,m,n1,n2,x1,y1,x2,y2,way_1,way_2
for(i=0i<Ni++)
for(j=0j<Nj++)
{
for(k=0k<8k++)
dir[i][j][k]=k
for(k=0k<8k++)
{
for(m=k+1m<8m++) /*对每个方向考察看有没有更好的*/
{
way_1=dir[i][j][k]
x1=i+way1[way_1]
y1=j+way2[way_1]
way_2=dir[i][j][m]
x2=i+way1[way_2]
y2=j+way2[way_2]
n1=check(x1+1,y1+1)
n2=check(x2+1,y2+1)
if(
( n1==0 && n2 ) || /*k方向不可达到,而m方向可达到*/
( n1 && n2&&weight[x1][y1]>weight[x2][y2] )/*都可达到但m方向权值小*/
)
{
dir[i][j][k]=way_2
dir[i][j][m]=way_1/*交换两个方向值*/
}
}
}
}
}
void print()
{
int x,y
printf("\n------%d answer----\n",++w)
for(x=1x<N+1x++)
{
printf("\n")
for(y=1y<N+1y++)
printf("%2d ",ch[(x-1)*N+y-1])
printf("\n")
}
printf("\nPress n to quit ,press any other key to continue.\n")
c=getchar()/*询问是否继续输出结果*/
}
main()
{
int x,y,way,way0
caculate()
directions()
printf("Please enter the row and column of the starting point.\n")
scanf("%d,%d",&a[1][0],&a[1][1])/*输入行数和列数*/
getchar() /*接收回车符*/
x=a[1][0],y=a[1][1]
ch[(x-1)*N+y-1]=1 /*在ch数组中对相应点赋值*/
while(1)
{
if(a[1][2]>=8) /*出发点的八个方向都已走过,表示所有的方法均已找出*/
break
if(a[st][2]>=8) /*此点的八个方向都已走过,应该退回到上一次走的点*/
{
x=a[st][0]
y=a[st][1]
ch[(x-1)*N+y-1]=0/*将这一点被走过的痕迹抹去*/
a[st][0]=a[st][1]=a[st][2]=0
a[st-1][2]++ /*使上一次走的点走的方向发生变化*/
st-- /*步数减一*/
}
else /*此点的八个方向未全走过,应走此方向*/
{
way0=a[st][2]
a[st][2]++/*确定下次应走的方向*/
x=a[st][0]
y=a[st][1]
way=dir[x-1][y-1][way0]
x=a[st][0]+way1[way]
y=a[st][1]+way2[way] /*确定按这次的方向走应走到的x,y坐标*/
if(x<1||y<1||x>N||y>N||ch[(x-1)*N+y-1]!=0)/*此点不满足要求*/
continue
ch[(x-1)*N+y-1]=++st/*走到这一点 */
a[st][0]=x
a[st][1]=y
a[st][2]=0 /*标记这一步*/
if(st==N*N) /*步数已满*/
{
print()/*输出结果*/
if(c==n)
break
ch[(x-1)*N+y-1]=0
a[st][0]=a[st][1]=a[st][2]=0
a[st-1][2]++
st--/*退回前一步*/
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)