我正在用 C++ 制作一个连接 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");
// }
// }
//}
有人能帮忙吗?
提前致谢。