【问题标题】:Struggling with Tic tac toe C++ [closed]在井字游戏 C++ 中苦苦挣扎 [关闭]
【发布时间】:2017-03-24 23:50:36
【问题描述】:

我对编码比较陌生,我的任务是用 C++ 创建一个井字游戏,我以为我已经完成了所有代码,但是存在各种问题,例如不允许用户输入,只允许输入一个玩家的名字而不是两个,如果你能给我关于如何完成这项工作的任何帮助/建议,我们将不胜感激。

#include <iostream>
using namespace std;
int menumain;
int oneplayer = 'X';

char square1('1');
char square2('2');
char square3('3');
char square4('4');
char square5('5');
char square6('6');
char square7('7');
char square8('8');
char square9('9');

void toggleplayers() {
  if (oneplayer == 'X')
    oneplayer = 'O';
  else
    oneplayer = 'X';
}

char win()

{
  if (square1 == 'X' && square2 == 'X' && square3 == 'X') return 'X';
  if (square4 == 'X' && square5 == 'X' && square6 == 'X') return 'X';
  if (square7 == 'X' && square8 == 'X' && square9 == 'X') return 'X';
  if (square1 == 'X' && square5 == 'X' && square9 == 'X') return 'X';
  if (square3 == 'X' && square5 == 'X' && square7 == 'X') return 'X';
  if (square3 == 'X' && square6 == 'X' && square9 == 'X') return 'X';
  if (square1 == 'X' && square4 == 'X' && square7 == 'X') return 'X';
  if (square2 == 'X' && square5 == 'X' && square8 == 'X') return 'X';
  if (square1 == 'O' && square2 == 'O' && square3 == 'O') return 'O';
  if (square4 == 'O' && square5 == 'O' && square6 == 'O') return 'O';
  if (square7 == 'X' && square8 == 'X' && square9 == 'X') return 'X';
  if (square1 == 'O' && square5 == 'O' && square9 == 'O') return 'O';
  if (square3 == 'O' && square5 == 'O' && square7 == 'O') return 'O';
  if (square3 == 'O' && square6 == 'O' && square9 == 'O') return 'O';
  if (square1 == 'O' && square4 == 'O' && square7 == 'O') return 'O';
  if (square2 == 'O' && square5 == 'O' && square8 == 'O') return 'O';
  return '/';
}

int main() {
  int playerone, playertwo;
  system("cls");

  cout << "tic tac toe" << endl;
  cout << "-----------" << endl;
  cout << "Start game (1)" << endl;
  cout << "Quit game (2)" << endl;

  cout << "Press 1 or 2 to proceed." << endl;
  cin >> menumain;
  if (menumain == 2)

  {
    return 0;
  } else {
    cout << "Player One, please enter your name: " << endl;
    cin >> playerone;
    system("cls");

    cout << "Player Two, please enter your name: " << endl;
    cin >> playertwo;
    system("cls");

    int playermove;
    cout << "Choose a number between 1-9 to place!" << endl;
    cin >> playermove;

    if (playermove == 1)
      square1 = oneplayer;
    else if (playermove == 2)
      square2 = oneplayer;
    else if (playermove == 3)
      square3 = oneplayer;
    else if (playermove == 4)
      square4 = oneplayer;
    else if (playermove == 5)
      square5 = oneplayer;
    else if (playermove == 6)
      square6 = oneplayer;
    else if (playermove == 7)
      square7 = oneplayer;
    else if (playermove == 8)
      square8 = oneplayer;
    else if (playermove == 9)
      square9 = oneplayer;

    cout << "Tic tac toe!" << endl;

    cout << "------------" << endl;

    cout << " " << square1 << " | " << square2 << " | " << square3 << " "
         << endl;

    cout << " " << square4 << " | " << square5 << " | " << square6 << " "
         << endl;

    cout << " " << square7 << " | " << square8 << " | " << square9 << " "
         << endl;

    cout << " " << endl;
    cout << playerone << endl;
    cout << playertwo << endl;

    while (1) {
      if (win() == 'X') {
        cout << playerone << " wins!" << endl;
        break;
      }
      else if (win() == 'O') {
        cout << playertwo << " wins!" << endl;
        break;
      }
      toggleplayers();
    }

    system("pause");
    return 0;
  }
}

【问题讨论】:

  • '不允许用户输入'?是否允许,就看你自己了
  • 您可以通过对板子使用二维数组来简化和缩短代码。
  • 按照您编写代码的方式,您很幸运井字游戏只是一个 3 x 3 的棋盘。如果它是 10 x 10,你的代码会是什么样子?识别代码中的模式,看看是否有一种概括的方法——二维矩阵的建议只是缩短代码的一步。
  • 我的建议是使用 调试器 来帮助分别执行每条语句。继续执行时查看变量的值。
  • 为什么要将字符分配给整数。我建议您选择不同的课程或将书扔进垃圾桶。 char 类型用于字符(字母)。

标签: c++ string if-statement input while-loop


【解决方案1】:

我做了一个快速修复。复制、粘贴、编译和测试。它将在游戏方面完成工作。有用。这是井字游戏。 :D。祝您顺利进行进一步的调整。

这些 }; }; };只是我自己的标记,因为我在文本编辑器而不是 IDE 中编辑了它。我的建议是,为 Draw 添加一个子句。因为如果没有赢家,它什么也做不了。

  #include <iostream>
  using namespace std;
  int menumain;
  int oneplayer = 'X';


  char square1('1');
  char square2('2');
  char square3('3');
  char square4('4');
  char square5('5');
  char square6('6');
  char square7('7');
  char square8('8');
  char square9('9');

  void toggleplayers()
  {
  if (oneplayer == 'X')
  oneplayer = 'O';
  else
  oneplayer = 'X';
  };

  char win()
  {
  if (square1 == 'X' && square2 == 'X' &&square3 == 'X')
  return 'X';
  if (square4 == 'X' && square5 == 'X' &&square6 == 'X')
  return 'X';

  if (square7 == 'X' && square8 == 'X' &&square9 == 'X')
  return 'X';

  if (square1 == 'X' && square5 == 'X' && square9 == 'X')
  return 'X';
  if (square3 == 'X' && square5 == 'X' &&square7 == 'X')
  return 'X';

  if (square3 == 'X' && square6 == 'X' &&square9 == 'X')
  return 'X';
  if (square1 == 'X' && square4 == 'X' &&square7 == 'X')
  return 'X';

  if (square2 == 'X' && square5 == 'X' &&square8 == 'X')
  return 'X';

  if (square1 == 'O' && square2 == 'O' &&square3 == 'O')
  return 'O';
  if (square4 == 'O' && square5 == 'O' &&square6 == 'O')
  return 'O';

  if (square7 == 'X' && square8 == 'X' &&square9 == 'X')
  return 'X';

  if (square1 == 'O' && square5 == 'O' && square9 == 'O')
  return 'O';
  if (square3 == 'O' && square5 == 'O' &&square7 == 'O')
  return 'O';

  if (square3 == 'O' && square6 == 'O' &&square9 == 'O')
  return 'O';
  if (square1 == 'O' && square4 == 'O' &&square7 == 'O')
  return 'O';

  if (square2 == 'O' && square5 == 'O' &&square8 == 'O')
  return 'O';

  return '/';
  };

  int main()
  {
  int playerone, playertwo;


  cout << "tic tac toe" << endl;
  cout << "-----------" << endl;
  cout << "Start game (1)" << endl;
  cout << "Quit game (2)" << endl;

  cout << "Press 1 or 2 to proceed." << endl;
  cin >> menumain;
  if (menumain == 2)

  {
  return 0;
  }
  else
  {

   cout << "Player One, please enter your name: " << endl;
    // cin >> playerone; create char array or string and ask for an input;


   cout << "Player Two, please enter your name: " << endl;
   // cin >> playertwo;  create char array or string and ask for an input;


   cout << "Tic tac toe!" << endl;

   cout << "------------" << endl;

   cout << " " << square1 << " | " << square2 << " | " << square3 << " " << endl;

   cout << " " << square4 << " | " << square5 << " | " << square6 << " " << endl;

   cout << " " << square7 << " | " << square8 << " | " << square9 << " " <<     endl;

   cout << " " << endl;
   cout << playerone << endl; //here replace playerone with char array variable or string;
   cout << playertwo << endl; //here replace playerone with char array variable or string;








    while(1)
    {


    int playermove;
    cout << "Choose a number between 1-9 to place!" << endl;
    cin >> playermove;

    if (playermove == 1)
    square1 = oneplayer;

    else if (playermove == 2)
    square2 = oneplayer;
    else if (playermove == 3)
    square3 = oneplayer;

    else if (playermove == 4)
    square4 = oneplayer;
    else if (playermove == 5)
    square5 = oneplayer;
    else if (playermove == 6)
    square6 = oneplayer;

    else if (playermove == 7)
    square7 = oneplayer;
    else if (playermove == 8)
    square8 = oneplayer;
    else if (playermove == 9)
    square9 = oneplayer;

    cout << "Tic tac toe!" << endl;

    cout << "------------" << endl;

    cout << " " << square1 << " | " << square2 << " | " << square3 << " " << endl;

    cout << " " << square4 << " | " << square5 << " | " << square6 << " " << endl;

    cout << " " << square7 << " | " << square8 << " | " << square9 << " " <<     endl;

    cout << " " << endl;
    cout << playerone << endl;
    cout << playertwo << endl;





    if (win() == 'X')
    {

     cout << playerone << " wins!" << endl;
        break;
    }

    else if (win() == 'O')
    {

     cout << playertwo << " wins!" << endl;
        break;

     };
    toggleplayers();
     };



     return 0;
     };


     };

【讨论】:

    【解决方案2】:

    您的代码存在许多问题。

    不允许用户输入

    实际上,您确实获得了用户输入,在开始时,但不是在之后。

    切换玩家后,您需要提示玩家移动:

    toggleplayers();
    cout << "Player " << oneplayer << ", enter your move (1 - 9): ";
    cout.flush();
    cin >> playermove;
    

    【讨论】:

      猜你喜欢
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多