C++编程井字棋AI尝试

标签 c++ algorithm artificial-intelligence tic-tac-toe minimax

我试图查看现有的井字游戏代码,发现有一种名为 MiniMax 的算法,虽然我已经了解它是如何工作的,但我似乎无法使用 5*5 使其工作井字游戏表。

我知道我将如何做,但我似乎无法找到一种方法。

我正在尝试检查 1 列/行/对角线中的每 4 步移动,以便在那里获胜,或者如果是其他玩家则有障碍。但我似乎找不到如何做到这一点,过去 6 个小时我一直在研究它。

#include <iostream>
#include <cstdlib>
#include <time.h>

  char arr[5][5];
char player1 = 'X';
char player2 = 'O';
char player = player1;
using namespace std;
void display() {
  cout << "     1  " << "2  " << "3  "<<"4  "<<"5  " << endl;
  cout << "    ----------------" << endl;
  for (int row = 0; row < 5; row++) {
    cout << row + 1 << " |  ";
    for (int col = 0; col < 5; col++) {
      cout << arr[row][col] << "  ";
    }
    cout << endl;
  }
}
void new_turn();
int firstEmptyRow(int c){
    int i;
    for(i=0;i<5;i++){
        if(arr[i][c])
            return i;
    }
}
int firstEmptyCol(int r){
    int i;
    for(i=0;i<5;i++){
        if(arr[r][i])
            return i;
    }
}
bool canWin(int mat[5][5]){

    int row,col;
    int countSteps;

    // FOR VERTICAL |
    //trying to find a count of 4 moves in 1 row, so It can be won.
    for(row=0;row<5;row++){
            countSteps=0;
        for(col=0;col<5;col++){
            if( (arr[row][col] == arr[row+1][col])
               &&(arr[row][col] ==player2)){
                countSteps++;
            }
        }
        if(countSteps==4){ cout << "MOVE IS WIN-ABLE" << endl; return true;}
    }

    return false;


}
int computer_move(){
    char temp;
    int test[5][5], tempo[5][5];
    int row, col;
    for(row=0;row<5;row++){
        for(col=0;col<5;col++){
                test[row][col] = arr[row][col];
                 tempo[row][col] = arr[row][col];
        }
    }
    for(row=0;row<5;row++){
        for(col=0;col<5;col++){
            if(arr[row][col] == '-'){
                temp = arr[row][col];
                if(canWin(test)){ // an attempt to test the move


                }
            }
        }
    }
}
void player_move() {
if(player==player1){
  int his_moveRow, his_moveCol;
  cout << "please enter your move row player " << player << endl;
  cin >> his_moveRow;
  cout << "please enter your move col player " << player << endl;
  cin >> his_moveCol;

  if (his_moveRow < 0 || his_moveRow > 5 || his_moveCol < 0 || his_moveCol > 5) {
    cout << "please enter a number from 1 to 5 player " << player << endl;
    player_move();
  }
  --his_moveRow;
  --his_moveCol;
  if (arr[his_moveRow][his_moveCol] == '-') {
    arr[his_moveRow][his_moveCol] = player;
  } else {
    cout << "please try again player " << player << endl;
    player_move();
  }
}else{
//computer move!!
cout <<"Computer Move!"<< endl;
computer_move();
}
  if (player == player1) {
    player = player2;
  } else {
    player = player1;

  }
  new_turn();
}
bool check_win();
void new_turn() {
  display();
  if (check_win() == true) {
    cout << "congratulation player " << player << " you won!" << endl;
    return;
  } else {
    int row, col, count = 0;
    for (row = 0; row < 5; row++) {
      for (col = 0; col < 5; col++) {
        if (arr[row][col] != '-') count++;
      }
    }
    if (count == 25) {
      cout << "No one won. That's a draw." << endl;
      return;
    } else {
      cout << "next turn" << endl;
      player_move();
    }
  }

}
bool check_win() {
  //Vertical |
  int row, col;
  for (row = 0; row < 5; row++) {
    for (col = 0; col < 5; col++) {
      if ((arr[row][col] == arr[row + 1][col] && arr[row + 1][col] == arr[row + 2][col])
          &&(arr[row+2][col] == arr[row + 3][col] && arr[row + 3][col] == arr[row + 4][col])
          && arr[row][col] != '-') {
        cout << "player won" << endl;
        player = arr[row][col];
        return true;
      }
    }
  }
  //Horizontal -
  for (row = 0; row < 5; row++) {
    for (col = 0; col < 5; col++) {
      if ((arr[row][col] == arr[row][col + 1] && arr[row][col + 1] == arr[row][col + 2])
         &&(arr[row][col+2] == arr[row][col + 3] && arr[row][col + 3] == arr[row][col + 4])
          && arr[row][col] != '-') {
        cout << "player won" << endl;
        player = arr[row][col];
        return true;
      }
    }
  }
      //Diagonal "\"
        row=0,col=0;
    if((arr[row][col] == arr[row+1][col+1] && arr[row+1][col+1] == arr[row+2][col+2]
        && arr[row+2][col+2] == arr[row+3][col+3])
    &&(arr[row+3][col+3] == arr[row+4][col+4]) && arr[row][col] != '-'){
         cout << "player won" << endl;
        player = arr[row][col];
        return true;
    }


  // Diagonal " / "
  for (row = 4; row >= 0; row--) {
    for (col = 0; col < 5; col++) {
      if ((arr[row][col] == arr[row - 1][col + 1] && arr[row - 1][col + 1] == arr[row - 2][col + 2]
           && arr[row - 2][col + 2] == arr[row - 3][col + 3]
           && arr[row - 3][col + 3] == arr[row - 4][col + 4]) && arr[row][col] != '-') {
                cout << "player won" << endl;
                player = arr[row][col];
                return true;
      }
    }
  }

  return false;
}
void game_start() {
  for (int row = 0; row < 5; row++) {
    for (int col = 0; col < 5; col++) {
      arr[row][col] = '-';
    }
  }
  display();
  player_move();

}
int main() {
  srand(time(NULL));
  game_start();
  return 0;
}

最佳答案

我曾经做过类似的事情,尽管没有使用那个 MiniMax 算法。我使用了 xkcd 的漫画“指南”,它讲述了井字游戏的最佳 Action 。 如果你的程序遵循这一点,你永远不会输,只会赢或平。

https://xkcd.com/832/

关于C++编程井字棋AI尝试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43381926/

相关文章:

c++ - 错误导入 hdf5 文件以在 caffe 中训练 hdf5_classification

c++ - 如何从采样率和单极点低通滤波器的alpha系数计算截止频率?

c++ - 如何阻止它无限重复,但仍保持循环?

algorithm - 射线-三角形相交

arrays - 在排序的 3d 数组中搜索元素

c# - 如何根据 g-score 修正路径?

c++ - 实时操作系统 : windows ce : Real mode and protected mode memory accessibility overhead

algorithm - 按准对数标度压缩

artificial-intelligence - 最佳优先搜索是最优和完整的吗?

algorithm - 将英语语句转换为问题