2017-04-13 98 views
1

我試圖查看已存在的Tic Tac Toe代碼,發現有一個叫做MiniMax的算法,雖然我已經理解它是如何工作的,但我似乎無法使用5 * 5表格爲Tic Tac Toe工作。C++編程Tic Tac Toe AI嘗試

我對如何做到這一點有了一個想法,但我似乎無法找到一個方法。

我試圖在每列4行中檢查1列/行/對角線,以便在那裏有一個贏,或者如果它是另一個玩家有一個塊。但我似乎無法找到如何做到這一點,我在過去的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; 
} 
+0

「取勝的唯一辦法就是不玩。」 –

+0

@好吧謝謝這些有用的信息。 –

+0

使用遞歸例程使功能更容易。你也可以使用alpha beta修剪來縮小搜索範圍並增加搜索深度。一些最終的獲勝動作可以用於葉節點的比較。 – ark1974

回答

-1

我曾經做過類似的事情,雖然沒有使用極小算法。我使用了xkcd的漫畫「指南」,講述了最佳的tic tac toe移動。 如果你的程序遵循這一點,你永遠不會輸,只能贏或平局。

https://xkcd.com/832/