Gomoku对角线获胜条件
gomoku diagonal winning condition
目前正在开发用于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;
}
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 在没有太多条件句的情况下,我如何避免被零除
- 基于多个条件处理地图中的所有元素
- 条件constexpr函数
- 无论条件是否为true,if总是在c++中执行
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 基于模板值的条件变量
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 将按位if条件转换为普通if条件
- 条件断点在不应该触发时触发
- 为什么简单的算术减法在"if"条件下不起作用?
- 如何在for循环中包含两个索引值的测试条件
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 我提出什么条件才能再加5%的折扣
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 如何编写一个使用n倍三元条件语句的C++布尔函数
- Gomoku对角线获胜条件
- C 控制台Tictactoe:检查获胜条件
- 用于检查Gomoku获胜条件的C++正则表达式