简体   繁体   English

运行代码时,c++终止进程

[英]When running code, c++ terminates the process

I'm trying to write code for the game tictactoe, i'm almost finished, I haven't yet written the part of the code that ends the game.我正在尝试为游戏tictactoe编写代码,我快完成了,我还没有编写结束游戏的代码部分。 My only problem is that c++ stops running, "Process terminated with status 0".我唯一的问题是 c++ 停止运行,“进程以状态 0 终止”。 Other than that the code runs fine.除此之外,代码运行良好。 In fact it works perfectly until it gets terminated, can anyone help me?事实上,在它被终止之前它工作得很好,任何人都可以帮助我吗? Indentation messed up the graphical part of the board function so don't mind that.压痕弄乱了电路板 function 的图形部分,所以不要介意。 The function board() creates a 3 by 3 grid labelled 1-9, the function game() replaces each number with an O or X depending on which number the plpayer picks, all done by the switch and while statmements. function board() 创建一个 3 x 3 的网格,标记为 1-9,function game() 根据玩家选择的数字将每个数字替换为 O 或 X,所有这些都由 switch 和 while 语句完成。 Here is my code, thank you.这是我的代码,谢谢。

using namespace std;
string a = "1";
string b = "2";
string c = "3";
string d = "4";
string e = "5";
string f = "6";
string g = "7";
string h = "8";
string i = "9";
string s1;
string s2;
string o = "O";
string x = "X";
string k = "Tic-Tac-Toe\n";
void ClearScreen() {
  cout << string(100, '\n');
}
void board() {
  std::cout << "             " << k;
  std::cout << "           |           |           \n";
  std::cout << "     " << a << "     |     " << b << "     |     " << c << "     \
  n ";
  std::cout << "___________|___________|___________\n";
  std::cout << "           |           |           \n";
  std::cout << "     " << d << "     |     " << e << "     |    " << f << "     \
  n ";
  std::cout << "___________|___________|___________\n";
  std::cout << "           |           |           \n";
  std::cout << "     " << g << "     |     " << h << "     |     " << i << "     \
  n ";
  std::cout << "           |           |           \n";
  std::cout << "      Player 1:O, Player 2:X\n";
}
void game() {
  board();
  int z = 1;
  while (z <= 9 && z % 2 == 1) {
    std::cin >> s1;
    int n1 = stoi(s1);
    switch (n1) {
    case 1:
      a = o;
      z++;
      break;
    case 2:
      b = o;
      z++;
      break;
    case 3:
      c = o;
      z++;
      break;
    case 4:
      d = o;
      z++;
      break;
    case 5:
      e = o;
      z++;
      break;
    case 6:
      f = o;
      z++;
      break;
    case 7:
      g = o;
      z++;
      break;
    case 8:
      h = o;
      z++;
      break;
    case 9:
      i = o;
      z++;
      break;
    }
    board();

  }
  while (z <= 9 && z % 2 == 0) {
    std::cin >> s2;
    int n2 = stoi(s2);
    switch (n2) {
    case 1:
      a = x;
      z++;
      break;
    case 2:
      b = x;
      z++;
      break;
    case 3:
      c = x;
      z++;
      break;
    case 4:
      d = x;
      z++;
      break;
    case 5:
      e = x;
      z++;
      break;
    case 6:
      f = x;
      z++;
      break;
    case 7:
      g = x;
      z++;
      break;
    case 8:
      h = x;
      z++;
      break;
    case 9:
      i = x;
      z++;
      break;
    }
    board();
  }
}

int main() {
  game();

}

The problem is that you're executing one round for player O. One round for player X. And then exiting the game function.问题是您正在为玩家 O 执行一轮。为玩家 X 执行一轮。然后退出game function。 You never tell your code to loop back up to the turn for player O.你永远不会告诉你的代码循环回到玩家 O 的回合。

In your code:在您的代码中:

void game() {
  board();
  int z = 1;
  while (z <= 9 && z % 2 == 1) {
    std::cin >> s1;
    int n1 = stoi(s1);
    switch (n1) {
    case 1:
      a = o;
      z++;
      break;
    ...
    }
    board();

  }
  while (z <= 9 && z % 2 == 0) {
    std::cin >> s2;
    int n2 = stoi(s2);
    switch (n2) {
    case 1:
      a = x;
      z++;
      break;
    ...
    }
    board();
  }
}

You enter the first while loop (player O) while z==1 , and both parts of the loop conditional are true.您在z==1时进入第一个 while 循环(玩家 O),循环的两个部分条件都为真。 Then you make a move, increment z by 1, and the second part of the loop conditional ( z % 2 == 1 ) is no longer true.然后你采取行动,将z增加 1,循环条件的第二部分( z % 2 == 1 )不再为真。 That loop ends.该循环结束。 You move onto the next loop (player X) and do something similar, then exit entirely.你进入下一个循环(玩家 X)并做类似的事情,然后完全退出。

It looks like you're using z to keep track of which turn it is?看起来您正在使用z来跟踪它是哪个转弯? In that case, I suggest having one OUTER loop to keep track of the turns, two INNER SWITCHES to handle each player's turn.在这种情况下,我建议使用一个 OUTER 循环来跟踪转弯,使用两个 INNER SWITCHES 来处理每个玩家的转弯。 You can clean it up and it will look something like this:你可以清理它,它看起来像这样:

void game() {
  board();
  // executes 9 rounds of the game
  // each player gets one turn per round
  for (int z = 0; z < 9; ++z)
  {
    // player O
    std::cin >> s;
    int n = stoi(s);
    switch (n)
    {
      case 1: a = o; break;
      case 2: b = o; break;
      case 3: c = o; break;
      ...
    }
    board();

    // player X
    std::cin >> s;
    int n = stoi(s);
    switch (n)
    {
      case 1: a = x; break;
      case 2: b = x; break;
      case 3: c = x; break;
      ...
    }
    board();
  }
}

There are other changes I would make to encapsulate code into functions and reuse code and increase readability, but I don't want to completely rewrite your code and make it unrecognizable to you.为了将代码封装到函数中并重用代码并提高可读性,我还会进行其他更改,但我不想完全重写您的代码并使其无法识别。 Baby steps.婴儿步。 Also, any changes I make would just be, like, my opinion.此外,我所做的任何更改都只是我的意见。 And you know what they say about opinions.你知道他们对意见的看法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM