Gomoku对角线获胜条件

gomoku diagonal winning condition

本文关键字:条件 获胜 对角线 Gomoku      更新时间:2023-10-16

目前正在开发用于Windows的Gomoku游戏并使用MFC。目前正在研究对角线的获胜算法。我的水平和垂直工作很好。希望有人可以帮助您阐明我的逻辑是错误的位置。这是代码:

bool CMainFrame::isWinner(int player){
for (int row = 0; row < data.size(); row++) {
    for (int col = 0; col < data.size(); col++) {
        if (data[row][col].color == player && data[row + 1][col + 1].color == player && data[row + 2][col + 2].color == player && data[row + 3][col + 3].color == player && data[row + 4][col + 4].color == player) {
            CheckForGameOver(player); //function that simply shows message box of winning piece
            return true;
        }
    }
 }
}

它仅在连接到左上角的对角线中起作用。相当陌生的编程,因此任何帮助都将受到极大的赞赏。

该游戏的规则是5个项目应连续匹配,列或对角线。只需比较每个行,列和对角线即可查看5个项目是否匹配,然后返回true。否则该功能应返回false。

bool won(std::vector<std::vector<data_t>> &data, int color)
{
    //check each row:
    for(int row = 0; row < 15; row++)
        for(int col = 0; col < 10; col++)
        {
            bool match = true;
            for(int i = 0; i < 5; i++)
                if(color != data[row][col + i].color)
                    match = false;
            if(match) return true;
        }
    //check each column:
    for(int col = 0; col < 10; col++)
        for(int row = 0; row < 15; row++)
        {
            bool match = true;
            for(int i = 0; i < 5; i++)
                if(color == data[row + i][col].color)
                    match = false;
            if(match) return true;
        }
    //check diagonal lines from top-left to bottom-right
    for(int col = 0; col < 10; col++)
        for(int row = 0; row < 10; row++)
        {
            bool match = true;
            for(int i = 0; i < 5; i++)
                if(color == data[row + i][col + i].color)
                    match = false;
            if(match) return true;
        }
    //lastly check diagonal lines from top-right to bottom-left
    return false;
}