TicTactoe指数输入

时间:2017-09-17 00:11:33

标签: c++ arrays

只想询问此代码广告的工作原理。我只是想澄清一些代码行。

我只是想基本上要显示' O'在每个' X'之后我把这个基本的tictactoe游戏放在索引中。但是当我这样做时,我会遇到一些像无限循环这样的错误,限制整个程序运行。请帮我。谢谢。

这是我的功能代码。

int InputComHard() {
int num, ctr = 0,temp = 1;
if (field == 'X') {
cout << "Choose your field [Player X (1~9)]";

cin >> num;
} else {
cout << "Choose your field [Player O (1~9) ]";
for(int x = 0; x < 3;x++){
      for(int y = 0;y<3;y++){
              temp++;
              if(tic[x][y] =='X'&&tic[x][y+1]!='X'){

                           num = temp;
                           }
              }
      }

 }
  //Here's the condition where the 'X' or 'O' will be placed on the entire 
 matrix.
 if (num == 1 && tic[0][0] == NULL)
 tic[0][0] = field;
 else if (num == 2 && tic[0][1] == NULL)
 tic[0][1] = field;
 else if (num == 3 && tic[0][2] == NULL)
 tic[0][2] = field;
 else if (num == 4 && tic[1][0] == NULL)
 tic[1][0] = field;
 else if (num == 5 && tic[1][1] == NULL)
 tic[1][1] = field;
 else if (num == 6 && tic[1][2] == NULL)
 tic[1][2] = field;
 else if (num == 7 && tic[2][0] == NULL)
 tic[2][0] = field;
 else if (num == 8 && tic[2][1] == NULL)
 tic[2][1] = field;
 else if (num == 9 && tic[2][2] == NULL)
 tic[2][2] = field;
 else {
 ctr++;
 }
 return ctr;
 }

当我输入1时,&#39; X&#39;出现在索引[0] [0]然后,&#39; O&#39;同时出现在[0] [1],之后,当我输入3时,错误开始,发生无限循环。我只想基本上对我的计划做的是显示&#39; O&#39;对于每个&#39; X&#39;我进入并将被放置在它旁边。请帮忙

    here is my main
    #include<iostream>
#include<stdlib.h>
using namespace std;
char tic[3][3];
char field = 'X';
void Draw() {
  //system("CLS");
  cout << "\n\n\tTic Tac Toe\n\n";
  cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
  cout << endl;
  //Displays the Matrix(rows and columns)
  cout << "     |     |     " << endl;
  cout << "  " << tic[0][0] << "  |  " << tic[0][1] << "  |  " << tic[0][2] << endl;
  cout << "_____|_____|_____" << endl;
  cout << "     |     |     " << endl;
  cout << "  " << tic[1][0] << "  |  " << tic[1][1] << "  |  " << tic[1][2] << endl;
  cout << "_____|_____|_____" << endl;
  cout << "     |     |     " << endl;
  cout << "  " << tic[2][0] << "  |  " << tic[2][1] << "  |  " << tic[2][2] << endl;
  cout << "     |     |     " << endl << endl;
  }
void Turn() {
  if (field == 'X')
     field = 'O';
  else
     field = 'X';
 }
  //Function to determine the winner based on the array positions.
char Winner() {

 int ctr = 0;

 if (tic[0][0] == 'X' && tic[0][1] == 'X' && tic[0][2] == 'X')
  return 'X';
  if (tic[1][0] == 'X' && tic[1][1] == 'X' && tic[1][2] == 'X')
      return 'X';
  if (tic[2][0] == 'X' && tic[2][1] == 'X' && tic[2][2] == 'X')
    return 'X';

  if (tic[0][0] == 'X' && tic[1][0] == 'X' && tic[2][0] == 'X')
  return 'X';
  if (tic[0][1] == 'X' && tic[1][1] == 'X' && tic[2][1] == 'X')
  return 'X';
  if (tic[0][2] == 'X' && tic[1][2] == 'X' && tic[2][2] == 'X')
  return 'X';

  if (tic[0][0] == 'X' && tic[1][1] == 'X' && tic[2][2] == 'X')
  return 'X';
  if (tic[2][0] == 'X' && tic[1][1] == 'X' && tic[0][2] == 'X')
  return 'X';

  //second player
  if (tic[0][0] == 'O' && tic[0][1] == 'O' && tic[0][2] == 'O')
  return 'O';
  if (tic[1][0] == 'O' && tic[1][1] == 'O' && tic[1][2] == 'O')
  return 'O';
 if (tic[2][0] == 'O' && tic[2][1] == 'O' && tic[2][2] == 'O')
  return 'O';

  if (tic[0][0] == 'O' && tic[1][0] == 'O' && tic[2][0] == 'O')
  return 'O';
  if (tic[0][1] == 'O' && tic[1][1] == 'O' && tic[2][1] == 'O')
  return 'O';
  if (tic[0][2] == 'O' && tic[1][2] == 'O' && tic[2][2] == 'O')
  return 'O';

  if (tic[0][0] == 'O' && tic[1][1] == 'O' && tic[2][2] == 'O')
  return 'O';
  if (tic[2][0] == 'O' && tic[1][1] == 'O' && tic[0][2] == 'O')
  return 'O';
  for (int x = 0; x < 3; x++) {
  for (int y = 0; y < 3; y++) {
     if (tic[x][y] == 'X' || tic[x][y] == 'O') {
        ctr++;
     }
    }
    if (ctr == 9) {
     return 'D';
   }
       }

}



    int main()

 {
  int choice;
  cout << "Choose Mode: \n";
  cout << "Player VS Player       [1]\n";
  cout << "Player VS BOT(EASY)    [2]\n";
  cout << "Player VS BOT(UNFAIR!) [3]\n";
  cin >> choice;
  switch (choice) {
  case 1:
     system("CLS");
     Draw();
     while (1) {
        if (Input() == 1) {
           system("CLS");
           cout << "Occupied!\n";
        } else {
           system("CLS");
           Turn();

        }
        Draw();
        if (Winner() == 'X') {
           cout << "X wins!" << endl;
           break;
        } else if (Winner() == 'O') {
           cout << "O wins!" << endl;
           break;
        } else if (Winner() == 'D') {
           cout << "DRAW!" << endl;
           break;
        }

     }
     break;

   case 2:
     system("CLS");
     Draw();
     while (1) {
        if (InputCom() == 1) {
           system("CLS");
           cout << "Occupied!\n";
        } else {
           system("CLS");
           Turn();

        }
        Draw();
        if (Winner() == 'X') {
           cout << "X wins!" << endl;
           break;
        } else if (Winner() == 'O') {
           cout << "O wins!" << endl;
           break;
        } else if (Winner() == 'D') {
           cout << "DRAW!" << endl;
           break;
        }

     }
     break;;
case 3:
     system("CLS");
     Draw();
     while (1) {
        if (InputComHard() == 1) {
           system("CLS");

           cout << "Occupied!\n";
        } else {
           system("CLS");
           Turn();

        }
        Draw();
        if (Winner() == 'X') {
           cout << "X wins!" << endl;
           break;
        } else if (Winner() == 'O') {
           cout << "O wins!" << endl;
           break;
        } else if (Winner() == 'D') {
           cout << "DRAW!" << endl;
           break;
        }

     }
     break;
  default:
     cout << "Invalid Input!" << endl;
  }

  system("pause");

}

1 个答案:

答案 0 :(得分:0)

它有点难以阅读代码,但我可以看到一个相当明显的问题会导致无限循环:当你的机器人转向InputComHard()时,它会选择相同的(占用的)广场每一次。当您从main()拨打电话时,您会收到一个1,表示占用的广场,但您只需再次拨打它。你需要某种方法告诉它选择一些尚未尝试的方格。

或者更好的是,更改InputComHard(),使其在成功选择可用的方格之前不会返回。

另外,在InputComHard()中,您在未初始化的情况下声明num。当你的机器人转弯时,如果你永远不满足条件if(tic[x][y] =='X'&&tic[x][y+1]!='X'),那么你永远不会到达num = temp,而num最终会得到一个垃圾值。这可能会给你带来一些麻烦,也许是循环,或许其他东西更糟糕。