Search code examples
c++arraysclasswhile-looptic-tac-toe

While loop in C++ ends, but doesn't continue onto the remaining statements


This is the while loop that I am working with for a TicTacToe game I am trying to make,

while (result == 0)
{
    game.player_move();
    result = game.check_draw();
    game.computer_move();
} 

if (result == 1)
    cout << "Tie.";

and these are the conditions for game.check_draw().

int TicTacToe::check_draw()
{
    if (move_count == 9)
        return 1;
    else
        return 0;
}

Furthermore, this code does work the way that I want it to work. X is set with a value of 88 and 0 is set with a value of 79. Both the user and computer move functions increment the move_count value by one like this:

void TicTacToe::player_move()
{
    int position;

    draw_table();
    cout << endl << "Pick position[1-9]: ";
    cin >> position;

    while (pos[position - 1] == 79 || pos[position - 1] == 88)
    {
        system("CLS");
        draw_table();
        cout << endl << "Position taken.";
        Sleep(500);
        system("CLS");
        draw_table();
        cout << endl << "Pick position[1-9]: ";
        cin >> position;
        system("CLS");
    }

    position[pos - 1] = 88;
    move_count++; // <-------------
    system("CLS");
}

and

void TicTacToe::computer_move()
{
    int position;

    srand((unsigned int)time(0));
    position = (rand() % 9) + 1;

    while (pos[position - 1] == 79 || pos[position - 1] == 88)
        position = (rand() % 9) + 1;

    position[pos - 1] = 79;
    move_count++; // <-------------

}

Since the player goes first, after the 9th turn the move_count is set to 9. Then the program continues onto the next statement.

if (result == 1)
    cout << "Tie.";

This code works perfectly, but ONLY if the following code from game.computer_move() is removed,

while (pos[position - 1] == 79 || pos[position - 1] == 88)
     position = (rand() % 9) + 1;

when this code is removed, the while loop ends and it continues onto the next if statement. However, when I add this code it does end the while loop but doesn't continue onto the if statement and displays a blinking cursor thing. All this code does, is check an array to see if X a.k.a (88) or O a.k.a (79) have already been placed in the position where 0 is to be placed.

I am profoundly confused because this doesn't affect the move_count at all, and furthermore after the 9th player move the move_count is set to 9 which means that the loop should terminate correctly and not even move onto game.computer_move()?

My understanding is that once the while condition is not met, the rest of the code in the while loop is ignored and the loop should end.

I have done a

cout << game.move_count;

in the loop, right before game.check_draw() to check if it has a value of 9 and indeed it does.

Also, a side comment. Is my code readable? Are there any glaring issues with it?

I can post the entirety of the code if requested and thank you for any replies in advance.


Solution

  • My understanding is that once the while condition is not met, the rest of the code in the while loop is ignored and the loop should end.

    This is incorrect. The while loop only checks to see if the condition is met before the next iteration (At the end) of the loop. If the condition isn't met in the middle of the loop, it will continue until the end of the current iteration of the loop, and then break out of the loop. If you wish to end the loop immediately once a condition is met you can use a break statement to break out of the loop.

    From the cppreference:

    This expression is evaluated before each iteration, and if it yields false, the loop is exited.