-2

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. My only problem is that c++ stops running, "Process terminated with status 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. 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. 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();

}
JohnFilleau
  • 4,045
  • 1
  • 15
  • 22
Noob
  • 1
  • 2
  • Please indent your code and please include a [example]. This program has no `main` function, and therefore can't compile. Please use meaningful variable names. This is very difficult for somebody to read. Help us help you. – JohnFilleau Jun 28 '20 at 21:59
  • I didn't add that bit, I didnt think it was relevant – Noob Jun 28 '20 at 22:01
  • I will get to it anyways, thanks. – Noob Jun 28 '20 at 22:01
  • You don't need to include your actual program, but the code you include should 1. compile or at least encapsulate the entire problem you're describing (since your problem is the program exiting, we need to see `main`), 2. actually reproduce the problem. An example where `1.` wouldn't need `main` would be if your issue was something like, *"The function `foo()` always return `1` but I expect it to return `2`. What's going on?"* There you'd only need to include `foo()`. – JohnFilleau Jun 28 '20 at 22:02
  • 1
    This is a style issue, but affects the readability of your code: Why do you name the `string` that holds the value `"1"` `a`? It appears to be a constant, so caps is standard. I'd suggest naming those things like `std::string ONE = "1";` or `std::string TIC_TAC_TOE = "Tic-Tac-Toe\n";` – JohnFilleau Jun 28 '20 at 22:06
  • Never mind, just saw how you're using those to hold state. That's not a bad first attempt. – JohnFilleau Jun 28 '20 at 22:08
  • I originally used std::string a ="1" instead of string a ="1", i thought that using the 2nd would help with my issue, I really don't know the difference between the two. – Noob Jun 28 '20 at 22:10
  • would it be worth just showing you the game() function, that would be a lot easier for me to indent. as well as main() like you asked. – Noob Jun 28 '20 at 22:11
  • If the issue is the `game` function you need to describe what the expected behavior of the function is, and you need to describe what the observed behavior of the function is. Otherwise I don't know what to look for. – JohnFilleau Jun 28 '20 at 22:12
  • The code does not compile. Hence, it does not reproduce the problem. – Eljay Jun 28 '20 at 22:13
  • The difference between `std::string` and `string` is in your case nothing. They're the same. C++ uses *namespaces* which allows you to encapsulate all of your identifiers (variables, functions, classes, etc.) in a higher level namespace so that your `foo` function doesn't collide with the `foo` function in the popular third party library you're using. The C++ standard library includes many many many utility classes and objects. `std::string` is one of them. But you could easily write your own `noob::string` if you wanted. – JohnFilleau Jun 28 '20 at 22:15
  • Bookmark this website: https://en.cppreference.com/w/cpp/language/namespace it's archaic the first time you see it, but it's one of the best C++ references on the internet. isocpp.org is what you want to use for higher level conceptual references. And nothing replaces a good book. You should get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – JohnFilleau Jun 28 '20 at 22:17
  • when i run the function, it shows the 3 by 3 grid labelled 1 to 9, and it asks the user to enter a number, when a number is entered it replaces that number with O, it then asks for another number, and it replaces the second number with a X. Afterwards the program terminates, even though the while statement lasts 9 goes. – Noob Jun 28 '20 at 22:17
  • I see why you thought `main` wasn't important to include, but understand that without you showing us, there's no way we could have known what was in it. You could have had some code in there like `exit_program_with_code_0();` and we wouldn't have known. – JohnFilleau Jun 28 '20 at 22:18
  • could i email you the file? It would be a lot easier to follow, if you're willing to do so. – Noob Jun 28 '20 at 22:19
  • Why do you need to email the file? It's all right here in the question isn't it? – JohnFilleau Jun 28 '20 at 22:20
  • so you could run it, and im not entirely sure on how to make it any clearer – Noob Jun 28 '20 at 22:21
  • No, if I want to run it I can just copy-paste. – JohnFilleau Jun 28 '20 at 22:22
  • Also, as a beginner to the language, I understand there are probably a lot of "magic" parts to this code and to the stackoverflow process that you're just taking on faith. I appreciate that, and I'm not trying to bust your chops. Just trying to get more info. I hope I don't come across as crabby. – JohnFilleau Jun 28 '20 at 22:23
  • you've been very helpful, i'll probably have to redo it completely with a fresh brain – Noob Jun 28 '20 at 22:25

1 Answers1

0

The problem is that you're executing one round for player O. One round for player X. And then exiting the game function. You never tell your code to loop back up to the turn for player 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. Then you make a move, increment z by 1, and the second part of the loop conditional (z % 2 == 1) is no longer true. That loop ends. You move onto the next loop (player X) and do something similar, then exit entirely.

It looks like you're using z to keep track of which turn it is? In that case, I suggest having one OUTER loop to keep track of the turns, two INNER SWITCHES to handle each player's turn. 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.

JohnFilleau
  • 4,045
  • 1
  • 15
  • 22
  • I have a question doesn't program terminated with status 0 usually indicate success?? – Yunfei Chen Jun 29 '20 at 00:46
  • @YunfeiChen normally yeah. Of course, if the program shouldn't be exiting and should instead be doing something, then it's a bug (here I use bug to mean mismatch between requirements and implementation) – JohnFilleau Jun 29 '20 at 00:48