0

我正在创建一个井字游戏作为具有二维数组的项目,并且在检查点以查看是否有赢家时遇到了一些麻烦。有人可以帮我调试吗?这里是checkwinner函数和main函数。

char CheckWinner( char board[3][3])
{
    int i = 0;
    int j = 0;

                    if ( board[i][j] =='X' && board[i][j+1] == 'X' && board[i][j+2]== 'X' )
                    { W = X;}
                    else if (board[i+1][j] && board[i+1][i+1] && board[i+1][j+2]== 'X' )
                    { W = X;}
                    else if (board[i+2][j] && board[i+2][j+1] && board[i+2][j+2]== 'X')
                    { W = X;}
                    else if (board[i][j] && board[i+1][j] && board[i+2][j]== 'X')
                    { W = X;}
                    else if (board[i][j+1] && board[i+1][j+1] && board[i+2][j+1]== 'X')
                    { W = X;}
                    else if (board[i][j+2] && board[i+1][j+2] && board[i+2][j+2]== 'X')
                    { W =X;}
                    else if (board[i][j] && board[i+1][j+1] && board[i+2][j+2]=='X')
                    { W = X;}
                    else if (board[i+2][j] && board[i+1][j+1] && board[i][j+2]== 'X')
                    { W = X;}

                    else if (board[i][j] && board[i][j+1] && board[i][j+2]== 'O' )
                    { W = O;}
                    else if (board[i+1][j] && board[i+1][j+1] && board[i+1][j+2]== 'O')
                    { W = O;}
                    else if (board[i+2][j] && board[i+2][j+1] && board[i+2][j+2]== 'O')
                    { W = O;}
                    else if (board[i][j] && board[i+1][j] && board[i+2][j]== 'O')
                    { W = O;}
                    else if (board[i][j+1] && board[i+1][j+1] && board[i+2][j+1]== 'O')
                    { W = O;}
                    else if (board[i][j+2] && board[i+1][j+2] && board[i+2][j+2]== 'O')
                    { W = O;}
                    else if (board[i][j] && board[i+1][j+1] && board[i+2][j+2]== 'O')
                    { W = O;}
                    else if (board[i+2][j] && board[i+1][j+1] && board[i][j+2]== 'O')
                    { W = O;}



    return W;
}
int main () 
{ 
    char board[3][3];
    char Win = CheckWinner(board);
    int r = 0;


    InitializeBoard(board);

     for (int r = 0; r < 4 ; r++)
     { 
        PlayX(board);
        PlayO(board); 
        PrintBoard(board);
    }
     CheckWinner(board);
     cout << Win ;

    if (Win == X)
    {
        cout << "The winner is Player 1.";
    }
    else if (Win == O)
    {
        cout << "The winner is Player 2.";
    }
    else if (Win == TIE)
    {
        cout << " IT'S A TIE";
    }
    else; 


    system("PAUSE");
return 0; 
4

3 回答 3

3

您可以使用以下内容优化您的条件代码:

typedef char piece;

piece iswin() const
{
    piece ret = 'T';

    // Checks for horizontal win
    for (int i = 0; i < 3; ++i)
        if (*arr[i] == arr[i][1] && arr[i][1] == arr[i][2])
            if ((ret = *arr[i]) != 'T')
                return ret;

    // Checks for vertical win
    for (int i = 0; i < 3; ++i)
        if (arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i])
            if ((ret = arr[0][i]) != 'T')
                return ret;

    // Check for diagonal win (upper left to bottom right)
    if (**arr == arr[1][1] && arr[1][1] == arr[2][2])
        if ((ret = **arr) != 'T')
            return ret;

    // Check for diagonal win (upper right to bottom left)
    if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0])
        if ((ret = arr[0][2]) != 'T')
            return ret;

    return ret;
}

// checking the result:
switch (winner) {
    case 'T': /* tie */ break;
    case 'X': /* X won */ break;
    case 'O': /* O won */ break;
}

这有点干净。它执行 6 个循环而不是 9 个。

于 2013-03-18T19:56:01.490 回答
1

您立即将CheckWinner结果分配给Win,然后进行游戏,然后CheckWinner再次调用而不将其结果分配给Win。因此,当您检查Win以下行时,您会得到板子尚未初始化时的原始结果。

于 2013-03-18T19:36:55.250 回答
0

可能与将文字 O 或 X 分配给应该是 char 的返回值有关。因此,应该是“O”或“X”而不是 O 或 X。除非 O 或 X 在发布的代码之外定义为 char 变量。

于 2013-03-18T19:38:38.893 回答