C++ gomoku对角获胜条件
我目前正在为windows开发一款Gomoku游戏,并且使用MFC。我目前正在研究对角线的获胜算法。我的水平和垂直工作都很好。我希望有人能帮我解释一下我的逻辑错在哪里。代码如下:C++ gomoku对角获胜条件,c++,visual-c++,mfc,C++,Visual C++,Mfc,我目前正在为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[ro
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;
}
}
}
}
boolcmainframe::isWinner(int播放器){
对于(int row=0;row
它仅适用于连接到左上角的对角线。对编程来说是相当陌生的,因此任何帮助都将不胜感激。这个游戏的规则是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;
}
bool-won(标准::矢量和数据,int-color)
{
//检查每一行:
对于(int行=0;行<15;行++)
for(int col=0;col<10;col++)
{
布尔匹配=真;
对于(int i=0;i<5;i++)
如果(颜色!=数据[行][col+i].color)
匹配=假;
如果(匹配)返回true;
}
//检查每一列:
for(int col=0;col<10;col++)
对于(int行=0;行<15;行++)
{
布尔匹配=真;
对于(int i=0;i<5;i++)
if(color==数据[行+i][列].color)
匹配=假;
如果(匹配)返回true;
}
//检查从左上到右下的对角线
for(int col=0;col<10;col++)
对于(int行=0;行<10;行++)
{
布尔匹配=真;
对于(int i=0;i<5;i++)
if(color==数据[row+i][col+i].color)
匹配=假;
如果(匹配)返回true;
}
//最后检查从右上到左下的对角线
返回false;
}
这个游戏的规则是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;
}
bool-won(标准::矢量和数据,int-color)
{
//检查每一行:
对于(int行=0;行<15;行++)
for(int col=0;col<10;col++)
{
布尔匹配=真;
对于(int i=0;i<5;i++)
如果(颜色!=数据[行][col+i].color)
匹配=假;
如果(匹配)返回true;
}
//检查每一列:
for(int col=0;col<10;col++)
对于(int行=0;行<15;行++)
{
布尔匹配=真;
对于(int i=0;i<5;i++)
if(color==数据[行+i][列].color)
匹配=假;
如果(匹配)返回true;
}
//检查从左上到右下的对角线
for(int col=0;col<10;col++)
对于(int行=0;行<10;行++)
{
布尔匹配=真;
对于(int i=0;i<5;i++)
if(color==数据[row+i][col+i].color)
匹配=假;
如果(匹配)返回true;
}
//最后检查从右上到左下的对角线
返回false;
}
不要在循环中使用像15
这样的数字,使用有意义的命名常量,或者从数据的大小推导值。围棋板是19 x 19,所以我不确定你的测试是否在范围内。谢谢你的回复。我的板子是15x15。哦,我刚刚编辑了我的数据。size()。我在代码中使用了它,但它似乎仍然没有改变任何东西。那么你肯定是越界了。可能是什么原因导致唯一有效的对角线是从左上角开始的?抱歉,我不明白它是如何越界的。你介意给我解释一下吗?不要在循环中使用像15
这样的数字,不要使用有意义的命名常量,也不要从数据的大小推导值。围棋板是19 x 19,所以我不确定你的测试是否在范围内。谢谢你的回复。我的板子是15x15。哦,我刚刚编辑了我的数据。size()。我在代码中使用了它,但它似乎仍然没有改变任何东西。那么你肯定是越界了。可能是什么原因导致唯一有效的对角线是从左上角开始的?抱歉,我不明白它是如何越界的。你介意给我解释一下吗?