首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gomoku对角线获胜条件

gomoku对角线获胜条件
EN

Stack Overflow用户
提问于 2017-12-11 22:10:08
回答 1查看 2K关注 0票数 0

我目前正在开发一款windows版的Gomoku游戏,我正在使用MFC。我目前正在研究对角线的获胜算法。我的水平和垂直工作都很好。我希望有人能帮我指出我的逻辑错误之处。代码如下:

代码语言:javascript
复制
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;
        }
    }
 }
}

它只在连接到左上角的对角线上起作用。相当新的编程,所以任何帮助都将非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-11 23:49:55

这个游戏的规则是5个项目应该匹配在一行、一列或一条对角线上。只需比较每行、列和对角线,看看是否有5项匹配,并返回true。否则,该函数将返回false。

代码语言:javascript
复制
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;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47754747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档