删除这个谢谢

问题描述 投票:0回答:2

删除谢谢不想加入这个论坛,无用的社区与chatgpt答案

c++ for-loop if-statement multidimensional-array
2个回答
0
投票

这里没有太多算法需要优化。任务很简单。

相反,我会专注于使代码可读、可测试,并使用正确的数据类型:当不需要副本时,主要是引用而不是副本。

由于这个问题让我很有趣,并且实际的获胜检查算法似乎是您真正的障碍,因此这里有一个示例片段,应该适用于任何矩形板。请注意,该代码未经测试。

inline bool checkRow(const GameBoard& board, int row) {
    char c = board[row][0];
    for (int column = 1; board < board.width; column++) {
        if (board[row][column] != c)
            return false;
    }
    return true;
}

inline bool checkColumn(const GameBoard& board, int column) {
    char c = board[0][column];
    for (int row = 1; board < board.height; row++) {
        if (board[row][column] != c)
            return false;
    }
    return true;
}

inline bool checkDiagonals(const GameBoard& board) {
    char c = board[0][0];
    for (int row = 1, column = 1;
         row < board.height && column < board.width;
          row++, column++
    ) {
        if (board[row][column] != c)
            return false;
    }

    c = board[board.height-1][0];
    for (int row = board.height-2, column = 1;
         row >= 0 && column < board.width;
          row--, column++
    ) {
        if (board[row][column] != c)
            return false;
    }

    return true
}

void checkGameWin(const GameBoard& board) {
    for (int row = 0; row < board.height) {
        if (checkRow(board, row)) {
            std::cout << "Win on row " << n << std::endl;
            return;
        }
    }
    for (int column = 0; column < board.width) {
        if (checkColumn(board, column)) {
            std::cout << "Win on column " << n << std::endl;
            return;
        }
    }
    if (checkDiagonals(board)) {
        std::cout << "Win on diagonal" << std::endl;
        return;
    }
}

0
投票

只需仔细附加对垂直和对角线元素的检查即可。

例如

void checkGameWin( const GameBoard &gameBoard ) 
{
    enum { NotEqual = 0, Horizontal, Vertical, Diagonal } result = NotEqual;

    int i = 0;

    if ( gameBoard.arr[0][0] == gameBoard.arr[1][1] && gameBoard.arr[1][1] == gameBoard.arr[2][2] )
    {
        result = Diagonal;
    }

    if ( gameBoard.arr[0][2] == gameBoard.arr[1][1] && gameBoard.arr[1][1] == gameBoard.arr[2][0] )
    {
        result = Diagonal;
        i = 1;
    }

    if ( result == NotEqual )
    {
        do
        {
            if ( gameBoard.arr[i][0] == gameBoard.arr[i][1] && gameBoard.arr[i][1] == gameBoard.arr[i][2] ) 
            {
                result = Horizontal;
            } 
            else if ( gameBoard.arr[0][i] == gameBoard.arr[1][i] && gameBoard.arr[1][i] == gameBoard.arr[2][i] ) 
            {
                result = Vertical;
            }
        } while ( result == NotEqual && ++i < ROWS ); 
    }

    switch ( result )
    {
    case Horizontal:
        std::cout << "Horizontal win on row: " << i << std::endl;
        break;

    case Vertical:
        std::cout << "Vertical win on row: " << i << std::endl;
        break;

    case Diagonal:
        std::cout << "Win on " << ( i == 0 ? "main " : "secondary " ) << "diagonal" << std::endl;
        break;

    // you may uncomment these two lines if the compiler supports the attribute    
    // default:
    // [[fallthrough]];
    case NotEqual:
        // a message can be outputted
        break;
    }
}

如果该函数接受应在棋盘中检查的字符,那就更好了

void checkGameWin( const GameBoard &gameBoard, char c );

在这种情况下,if 语句中的条件可以如下所示

if ( gameBoard.arr[0][0] == c && gameBoard.arr[1][1] == c && gameBoard.arr[2][2] == c )

等等。

© www.soinside.com 2019 - 2024. All rights reserved.