1

我正在用 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");
//      }
//  }
//}

有人能帮忙吗?

提前致谢。

4

1 回答 1

1

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

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 类似,但在递归调用中除外

于 2013-03-06T16:21:45.120 回答