QT:黑白棋的吃子规则(七)

代码介绍

/*************************************************
 * 功能
 *      黑白棋吃子规则
 * 参数:
 *      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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值