我目前正在开发一款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;
}
}
}
}它只在连接到左上角的对角线上起作用。相当新的编程,所以任何帮助都将非常感谢。
发布于 2017-12-11 23:49:55
这个游戏的规则是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;
}https://stackoverflow.com/questions/47754747
复制相似问题