代码介绍
/*************************************************
* 功能
* 黑白棋吃子规则
* 参数:
* x,y:棋盘数组坐标位置
* chess:棋子状态
* currentRole:Widget类中枚举变量
* eatChess:true代表吃子,false代表有子可吃,默认为true
* 返回值:
* 能吃子的个数
* ***********************************************/
int chessForm::judgeRule(int x, int y, void *chess, Widget::chessType currentRole, bool eatChess)
{
int dir[8][2] = {{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
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] != Widget::Empty)
return 0;
for(i = 0;i<8;i++)
{
temp_x +=dir[i][0];temp_y += dir[i][1];
if((temp_x <8 && temp_x >= 0 && temp_y<8 && temp_y >= 0)
&&(chessFlag[temp_x][temp_y] != currentRole) && (chessFlag[temp_x][temp_y] !=Widget::Empty))
{
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] == Widget::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++;
}
}
else
{
temp_x = dir[i][0];temp_y = dir[i][1];
while((temp_x != x)|(temp_y != y))
{
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;
}