看到一个超级棒的五子棋检测方法

bool CheckWin(int xIndex, int yIndex) {

    int max = 0;

      int tempXIndex = xIndex;

    int tempYIndex = yIndex;

    // 三维数组记录横向,纵向,左斜,右斜的移动

    int dir [4][2][2]  =

    {
        // 横向

        { { -1, 0 }, { 1, 0 } },

        // 竖着

        { { 0, -1 }, { 0, 1 } },

        // 左斜

        { { -1, -1 }, { 1, 1 } },

        // 右斜

        { { 1, -1 }, { -1, 1 } }

    };

    bool flag;

    for(int i = 0; i < 4; i++){

        int  count = 1;

        //j为0,1分别为棋子的两边方向,比如对于横向的时候,j=0,表示下棋位子的左边,j=1的时候表示右边

        for (int j = 0; j< 2; j++) {

            flag = true;

            /**

             while语句中为一直向某一个方向遍历

             有相同颜色的棋子的时候,Count++

             否则置flag为false,结束该该方向的遍历

             **/

            while (flag){

                tempXIndex = tempXIndex + dir[i][j][0];

                tempYIndex = tempYIndex + dir[i][j][1];

                if (tempXIndex >= 0 && tempXIndex < 15 && tempYIndex >= 0 && tempYIndex < 15) {

//                    printf("=======\n");

                    if (((a[tempXIndex][tempYIndex])->getType() == (a[xIndex][yIndex])->getType())

                        && (a[tempXIndex][tempYIndex])->getType() !=0 )

                    {

                        count++;

                        log("----count:%d\n",count);

                    } else{

                        flag = false;

                    }

                }else{

                    break;

                }

            }

            tempXIndex = xIndex;

            tempYIndex = yIndex;

        }

        if (count >= 5) {

            max = 1;

            break;

        } else

            max = 0;

    }

    if (max == 1)

        return true;

    else

        return false;

}

猜你喜欢

转载自blog.csdn.net/ChangeNew/article/details/82462304