# 每次点到白棋的左边和右边都会出现程序异常运行的情况,不知道是不是落子规则的问题
//吃子规则
int ChessForm::judgeRule(int x,int y,void *chess,Chess::ChessType currentRole,bool eatChess )
{
int dir[8][2]={{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};//8个方向
int temp_x=x,temp_y=y; //临时保存棋盘数组坐标位置
int i=0,eatNum=0;
typedef int(*p)[8]; //自定义类型
p chessFlag=p(chess); //类型转换
if(chessFlag[temp_x][temp_y]!=Chess::Empty) //如果当前位置已有子,返回
return 0;
for(i=0;i<8;i++)
{
temp_x+=dir[i][0];
temp_x+=dir[i][1]; //当前坐标的相邻坐标
if((temp_x<8&&temp_x>=0&&temp_y<8&&temp_y>=0)&&(chessFlag[temp_x][temp_y]!=Chess::Empty)
&&(chessFlag[temp_x][temp_y]!=currentRole))//相邻位置在界内,并且是对方棋子才可能吃子
{
temp_x+=dir[i][0];
temp_y+=dir[i][1]; //向前走一步,继续判断下一个
while((temp_x<8&&temp_x>=0&&temp_y<8&&temp_y>=0)) //在界内
{
if(chessFlag[temp_x][temp_y]==Chess::Empty)
break; //遇到空位则跳出循环
if(chessFlag[temp_x][temp_y]==currentRole)//遇到自己的棋子,代表可以吃子
{
if (eatChess==true)//确定吃子
{
chessFlag[x][y]=currentRole; //点击处标记为自己的棋子
temp_x-=dir[i][0];
temp_y-=dir[i][1];//后退一步
while((temp_x!=x)||(temp_y!=y))//只要没有后退到点击处
{
chessFlag[temp_x][temp_y]=currentRole;//把原来的棋子变为自己的棋子
temp_x-=dir[i][0];
temp_y=dir[i][1];//再后退一步
eatNum++;
}
}
break;
}
temp_x+=dir[i][0];
temp_y+=dir[i][1]; //如果没有找到自己的棋子,就向前走一步
}
}
temp_x=x;
temp_y=y;//如果该方向不能吃子,则判断下一个方向
}
return eatNum;
}