TicTacToe代码不会用O和X替换数字

TicTacToe code is not replacing numbers with O and X

本文关键字:替换 数字 代码 TicTacToe      更新时间:2023-10-16
#include <iostream>

using namespace std;
int square[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
void board();
int check_win();
int main() 
{
  char mark;//player 1 x player 2 0
  int player = 1, choice;
  int i = check_win();
  while(i == -1){
    board();
    player = (player % 2) ? 1 : 2;
    cout << "Player " << player << " enter a number";
    if(player  == 1)
    mark = 'X';
    else
    mark = 'O';
    cin >> choice;
    switch (choice) {
      case 1:
        square[1] = (char)(int)mark;
        break;
      case 2:
        square[2] = (char)(int)mark;
        break;
      case 3:
        square[3] = (char)(int)mark;
        break;
      case 4:
        square[4] = (char)(int)mark;
        break;
      case 5:
        square[5] = (char)(int)mark;
        break;
      case 6:
        square[6] = (char)(int)mark;
        break;
      case 7:
        square[7] = (char)(int)mark;
        break;
      case 8:
        square[8] = (char)(int)mark;
        break;
      case 9:
        square[9] = (char)(int)mark;
        break;
      default:
        cout << "None of these";
        break;
    }
    i = check_win();
    player++;
  }
  board();
  if(i == 1) cout << "player " << --player << "win";
  else{
    cout << "Game DRAW";
  }
  cin.get();
  return 0;
}
int check_win()
{
  if (square[1] == square[2] && square[2] == square[3])
  return 1;
  else if (square[4] == square[5] && square[5] == square[6])
  return 1;
  else if (square[7] == square[8] && square[8] == square[9])
  return 1;
  else if (square[1] == square[4] && square[4] == square[7])
  return 1;
  else if (square[2] == square[5] && square[5] == square[8])
  return 1;
  else if (square[3] == square[6] && square[6] == square[9])
  return 1;
  else if (square[1] == square[5] && square[5] == square[9])
  return 1;
  else if (square[3] == square[5] && square[5] == square[7])
  return 1;
  else
  return -1;
}

void board()
{
  cout << "nntTic Tac Toenn";
  cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
  cout << endl;
  cout << "     |     |     " << endl;
  cout << "  " << square[1] << "  |  " << square[2] << "  |  "
  << square[3] << endl;
  cout << "_____|_____|_____" << endl;
  cout << "     |     |     " << endl;
  cout << "  " << square[4] << "  |  " << square[5] << "  |  " <<
  square[6] << endl;
  cout << "_____|_____|_____" << endl;
  cout << "     |     |     " << endl;
  cout << "  " << square[7] << "  |  " << square[8] << "  |  "
  << square[9] << endl;
  cout << "     |     |     " << endl << endl;
}

如您所见,我完成了游戏本身的代码,但有一个小问题。当我选择一个像5这样的数字时,它不会随着XO而改变5,而是8879使用,我该如何解决?Mark 是保存 XO 内存的 char 变量,但我认为它被这里的函数对话了。

问题是你记住了数字

int square[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

不是char

char square[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

以及当您将X(或O(保存在square

square[5] = (char)(int)mark;

您首先将char转换为int(X转换为88(,然后转换为char(88转换为X(,最后,您将它记住在int中(X再次88(。

建议:使用square数组chars并简单地编写

square[5] = mark;