2017-12-02 6 views
-4

これは初めての質問です。私がここで間違いを犯して修正するべきことを教えてくれたらあまりにも怒ってはいけません。ありがとうございました:DC++ Tic-Tac-Toe AIが動作しない

CPUに対してTic-Tac-ToeをプレイできるC++コードを記述しようとしています。 CPUは勝ち、少なくとも同調しなければならない。 CPUが最初に動く。プレイヤーとして、行と列にそれぞれ2つの数字を入力するよう求められます。

問題は、私がプログラムを実行すると、32行目から始まるforループの3番目のサイクルでエラーメッセージなしで停止するという問題です。私が間違っていたのは不思議です。

#include <iostream> 

using namespace std; 

void displayBoard(char []);     //displays current board 
bool isValidInput(char [], int, int, int); //checks the user input 
bool isItAScore(char [], char);    //determines if current player won or not 
int getValue(char [], char, int);    //get value of the board by player and number marks 

int main() 
{ 
    char tBoard[9], computer = 'X', player = 'O', empty = '.'; 
    int board[3][3], row = -1, col = -1, temp = 0; 
    bool didSomeoneWin = false; 

    //initializes the board with numbers 0 to 8 
    for(int i = 0; i < 3; i++) 
    { 
     for(int j = 0; j < 3; j++) 
     { 
      board[i][j] = temp; 
      temp++; 
     } 
    } 


    //initialize the actual board with '.'  
    for(int i= 0; i < 9; i++) 
    { 
     tBoard[i] = empty; 
    } 

    //starts the game 
    for(int k = 0; k < 9; k++) //it lasts only 9 turns max 
    { 
     displayBoard(tBoard); 

     if(k % 2 == 1) //player's turn 
     { 
      cout << "Player, row and column: "; 
      cin >> row >> col; //takes user input range of 1 to 3 

      //decreases each value by 1 to work with the array index 0 ~ 2 
      row--; 
      col--; 

      if(isValidInput(tBoard, row, col, board[row][col])) //iff the input is valid 
      { 
       tBoard[board[row][col]] = player; //puts the mark on the position 

       if(k > 4 && isItAScore(tBoard, player)) //iff the current player wins 
       { 
        displayBoard(tBoard); 
        cout << "Player wins."; 
        k = 8; //skips to the end of the loop 
        didSomeoneWin = true; //no tie 
       } 
      } 
      else //if the input is invalid 
      { 
       cout << "Invalid row or column number. Try again.\n"; 
       k--; //redo the loop with same player 
      } 
     } 
     else //computer's turn 
     { 
      cout << "Computer's move\n"; 

      if(k == 0) //first move 
      { 
       tBoard[5] = computer; 
      } 
      else 
      { 
       int value = -100, int position = -1; 

       for(int i = 0; i < 9; i++) 
       { 
        if(tBoard[i] == empty) 
        { 
         tBoard[i] = computer; 

         if(isItAScore(tBoard, computer)) 
         { 
          displayBoard(tBoard); 
          cout << "Computer wins."; 
          i = 8; 
          k = 8; 
          didSomeoneWin = true; 
         } 
         else 
         { 
          int x1 = getValue(tBoard, computer, 1); 
          int x2 = getValue(tBoard, computer, 2); 
          int o1 = getValue(tBoard, player, 1); 
          int o2 = getValue(tBoard, player, 2); 

          if(value < 3 * x2 + x1 - (3 * o2 + o1)) 
          { 
           value = 3 * x2 + x1 - (3 * o2 + o1); 
           position = i; 
          } 
         } 

         if(!didSomeoneWin) 
         { 
          tBoard[i] = empty; 
         } 
        } 
       } 

       tBoard[position] = computer; 
      }  
     } 
    } 

    if(!didSomeoneWin) //in case of tie 
    { 
     displayBoard(tBoard); 
     cout << "The cat wins"; 
    } 

    return 0; 
} 

//display the given board 
void displayBoard(char brd[]) 
{ 
    for(int i = 0; i < 9; i++) 
    { 
     cout << brd[i] << " "; 

     if((i + 1) % 3 == 0) 
     { 
      cout << endl; 
     } 
    } 
} 

//checks the input 
bool isValidInput(char brd[], int i, int j, int k) 
{ 
    if(((i >= 0 && i <= 2) && (j >= 0 && j <= 2)) && brd[k] == '.')  //number between 0 and 2, and not taken by any players 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

//checks if the given player or computer won or not 
bool isItAScore(char brd[], char c) 
{ 
    //chekcs rows 
    if((brd[0] == c && brd[1] == c && brd[2] == c) || (brd[3] == c && brd[4] == c && brd[5] == c) || (brd[6] == c && brd[7] == c && brd[8] == c)) 
    { 
     return true; 
    } 
    else 
    { 
     //checks columns 
     if((brd[0] == c && brd[3] == c && brd[6] == c) || (brd[1] == c && brd[4] == c && brd[7] == c) || (brd[2] == c && brd[5] == c && brd[8] == c)) 
     { 
      return true; 
     } 
     else 
     { 
      //checks diagonals 
      if((brd[0] == c && brd[4] == c && brd[8] == c) || (brd[2] == c && brd[4] == c && brd[6] == c)) 
      { 
       return true; 
      } 
      //if none of them fails 
      else 
      { 
       return false; 
      } 
     } 
    } 
} 

int getValue(char brd[], char c, int n) 
{ 
    int temp = 0, mark = 0; 
    bool eligible = true; 

    for(int i = 0; i < 9; i + 3) //check rows 
    { 
     for(int j = i; j < i + 3; j++) 
     { 
      if(brd[j] != c) 
      { 
       j = 10; 
       eligible = false; 
      } 
      else 
      { 
       if(brd[j] == c) 
       { 
        temp++; 
       } 
      } 
     } 

     if(eligible && temp == n) 
     { 
      mark++; 
     } 

     eligible = true; 
     temp = 0; 
    } 

    for(int i = 0; i < 3; i++) //check columes 
    { 
     for(int j = i; j < i + 7; j + 3) 
     { 
      if(brd[j] != c) 
      { 
       j = 10; 
       eligible = false; 
      } 
      else 
      { 
       if(brd[j] == c) 
       { 
        temp++; 
       } 
      } 
     } 

     if(eligible && temp == n) 
     { 
      mark++; 
     } 

     eligible = true; 
     temp = 0; 
    } 

    for(int i = 0; i < 9; i + 4) //check '\' diagonal 
    { 
     if(brd[i] != c) 
     { 
      i = 10; 
      eligible = false; 
     } 
     else 
     { 
      if(brd[i] == c) 
      { 
       temp++; 
      } 
     } 
    } 

    if(eligible && temp == n) 
    { 
     mark++; 
    } 

    eligible = true; 
    temp = 0; 

    for(int i = 2; i < 9; i + 2) //check '/' diagonal 
    { 
     if(brd[i] != c) 
     { 
      i = 10; 
      eligible = false; 
     } 
     else 
     { 
      if(brd[i] == c) 
      { 
       temp++; 
      } 
     } 
    } 

    if(eligible && temp == n) 
    { 
     mark++; 
    } 

    eligible = true; 
    temp = 0; 

    return mark; 
} 

私はミニマックス(式)と呼ばれるものを使用しようとしました。私はそれが意図したとおりに正しく動作したいと思っています。それは人間に対抗して最善の結果を生むべきです(勝ち分け、ネクタイ、失うことはありません)。

エラーメッセージなしで停止します。私は珍しいものをたくさん見つけ

http://cpp.sh/

+8

デバッガを使用します。 –

+0

コードにコンパイルエラーがあります。 –

+2

http://idownvotedbecau.se/nodebugging/(@ manni66)これについてもっと建設的な方法があります。 – user0042

答えて

0

を次のリンクは、私が使用しているオンラインのコンパイラです。あなたの最初の試みはcpp?

int value = -100, int position = -1; 

この

int value = -100; 
int position = -1; 

又は

int value = -100, position = -1; 

コンピュータの最初の動きは、中央の右側にトークンを設定する必要がありますコードの最初のエラーです。私はあなたが、あなたは無限ループ持つ中間位置

tBoard[5] = computer;// should be tBorad[4] 

次の問題にそれを設定したいと思う:あなただけそれを読んでいるので、

for(int i = 0; i < 9; i + 3) // right would be i+=3 

私は決して変化しない変数を、とそうでありません書いてください。これは5回発生します。警告が有効になっているコンパイラは、このようなバグを見つけるのに役立ちます。

次のもの、の改行continueいくつかのクレイジーループステートメントの代わりに使用します。これに代えて、

for(int i = 2; i < 9; i += 2) //check '/' diagonal 
    { 
     if(brd[i] != c) 
     { 
      i = 10; 
      eligible = false; 
     } 
    } 

使用ブレーク:離れている

for(int i = 2; i < 9; i += 2) //check '/' diagonal 
    { 
     if(brd[i] != c) 
     { 
      eligible = false; 
      break; 
     } 
    } 

すべて、このエラーの後には、プログラムが正常に動作するようです。 gccのような実際のコンパイラを使うべきだと思います。 gccでは、フラグを使って警告を見ることができます。

+0

このような質の低い質問に答えるのはあなたの時間を有効に活用するものではありません。このような質問は下落して閉鎖される傾向があり、最終的にはあなたが受け取ったかもしれない代理人と共に削除されます。 – dandan78

関連する問題