gpt4 book ai didi

c++ - Connect 4(C++) 中的对角线胜利

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:08 26 4
gpt4 key购买 nike

我正在用 C++ 制作 connect 4 游戏。我制作的游戏是人类与 AI 的对手,因此对于胜利参数,它会检查是人类获胜还是 AI 获胜。到目前为止,我已经能够为行中的胜利和列中的行执行此操作。我唯一遇到的麻烦是对角连续检测四个。

编辑:我的棋盘是一个 7x6 的网格

这是我检查行的方法:

for (int i = 0; i < board.size(); ++i)
{
if ((board[i][0] == PLAYER_TOKEN && board[i][1] == PLAYER_TOKEN && board[i][2] == PLAYER_TOKEN && board[i][3] == PLAYER_TOKEN) ||
(board[i][1] == PLAYER_TOKEN && board[i][2] == PLAYER_TOKEN && board[i][3] == PLAYER_TOKEN && board[i][4] == PLAYER_TOKEN) ||
(board[i][2] == PLAYER_TOKEN && board[i][3] == PLAYER_TOKEN && board[i][4] == PLAYER_TOKEN && board[i][5] == PLAYER_TOKEN) ||
(board[i][3] == PLAYER_TOKEN && board[i][4] == PLAYER_TOKEN && board[i][5] == PLAYER_TOKEN && board[i][6] == PLAYER_TOKEN))
{
gameover = true;
human.setWins(human.getWins()+1);
cout<<"Congratulations You have Won"<<endl;
system("PAUSE");
}
}

对于专栏我有这个:

for (int j = 0; j < board[0].size(); ++j)
{
if ((board[0][j] == PLAYER_TOKEN && board[1][j] == PLAYER_TOKEN && board[2][j] == PLAYER_TOKEN && board[3][j] == PLAYER_TOKEN) ||
(board[1][j] == PLAYER_TOKEN && board[2][j] == PLAYER_TOKEN && board[3][j] == PLAYER_TOKEN && board[4][j] == PLAYER_TOKEN) ||
(board[2][j] == PLAYER_TOKEN && board[3][j] == PLAYER_TOKEN && board[4][j] == PLAYER_TOKEN && board[5][j] == PLAYER_TOKEN))
{
gameover = true;
human.setWins(human.getWins()+1);
cout<<"Congratulations You have Won"<<endl;
system("PAUSE");
}
}

我一直在尝试找到一种类似的方法来实现对角线胜利,但无法想出任何可行的方法。我所得到的只是这个,坦率地说,它没有任何用处!

//for (int rows = 0; rows<4; ++rows){
// for (int columns = 0; columns<3; ++columns){
// if ((board[rows][columns] == PLAYER_TOKEN && board[rows+1][columns+1] == PLAYER_TOKEN && board[rows+2][columns+2] == PLAYER_TOKEN && board[rows+3][columns+3] == PLAYER_TOKEN) ||
// (board[rows+1][columns+1] == PLAYER_TOKEN && board[rows+2][columns+2] == PLAYER_TOKEN && board[rows+3][columns+3] == PLAYER_TOKEN && board[rows+1][columns+4] == PLAYER_TOKEN) ||
// (board[rows+2][columns+2] == PLAYER_TOKEN && board[rows+3][columns+3] == PLAYER_TOKEN && board[rows+4][columns+4] == PLAYER_TOKEN && board[rows+5][columns+5] == PLAYER_TOKEN))
// {
// gameover = true;
// human.setWins(human.getWins()+1);
// cout<<"Congratulations you have Won"<<endl;
// system("PAUSE");
// }
// }
//}

有人可以帮忙吗?

提前致谢。

最佳答案

您必须分别处理每个单元格,并尝试检查是否有从该单元格开始的获胜对角线。这是一个递归的解决方案

bool isWinnerRight(int row, int column, int count) {
if (row < rows && column < columns && board[row][column] == PLAYERTOKEN) {
if (count == 4) {
return true;
} else {
return isWinnerRight(row+1.column+1, count+1);
}
} else return false;
}

bool isWinnerLeft(int row, int column, int count) {
if (row < rows && column >= 0 && board[row][column] == PLAYERTOKEN) {
if (count == 4) {
return true;
} else {
return isWinnerRight(row+1.column-1, count+1);
}
} else return false;
}

int main() {
//do the initialization here
for (int i = 0; i < rows, i++) {
for (int j = 0; j < columns; j++) {
if (isWinnerRight(i,j,0)){/*WINNER FOUND*/}
if (isWinnerLeft(i,columns-j,0)){/*WINNER FOUND*/}
}
}
}

除了在递归调用中,isWinnerLeft() 与 isWinnerRight 类似

关于c++ - Connect 4(C++) 中的对角线胜利,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15251971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com