bool loop problem

hello,
I have to create a tictactoe game for my programming class. I have everything working execpt for my while loop to check to see if a space is already taken.

This is the code in the body.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        char number = '0';
        int turn = 1;
        for(int i = 0; i < ROW; i++)
        {       for(int j = 0; j < COL; j++)
                {       game[i][j] = number;
                        number++;
                }
        }
        print_game(game, COL, ROW);
     while(turn != 15)
     {
        bool filled = true;
        bool valid = false;
        if(turn == 1 || turn == 3 || turn == 5 || turn == 7 || turn == 9)
        {
                cout << "Player One" << endl;
                cout << "Please select your postion: ";
                cin >> selection;
                while(!valid)
                {       cout << "You're selection is " << selection << endl;
                        if(selection < 0 || selection > 8)
                        {       cout << "Please select a valid location: " << endl;
                                cin >> selection;
                        }
                        else
                                valid = true;
                }
                while(filled)
                {       if(game[selection/3][selection%3] = 'X' || game[selection/3][selection%3] == 'O')
                        {       cout << "That spot is already taken.  Please select again." << endl;
                                cin >> selection;
                        }
                        else
                                filled = false;
                }
        }

then the else statement is the same except everything applies to player two.
1
2
3
4
5
6
        if(turn == 1 || turn == 3 || turn == 5 || turn == 7 || turn == 9)
                game[selection/3][selection%3] = 'X';
        else
                game[selection/3][selection%3] = 'O';
        print_game(game, ROW, COL);
        turn++;


The valid bool statement works but when it gets to the filled statement it always pops up the please select again.

Any ideas? sorry this was so long.
That was actually quite short, relative to what I've had to read more recently.

I have helped someone else map a grid to a 1-d array. Hopefully, this will work for you too.

There are nine spaces in a tic-tac-toe game. Every three spaces, there's a newline. You need an integer array of length nine. 0 for empty, 1 for X, 2 for O.

To convert between the co-ordinate system (the top left corner is (0,0)), then the forumla for what character on the linear array should be is (note that the array starts from zero) rownumber*3 + columnnumber;

EDIT: When checking, have the program retrieve that data in the section specified by that formula, and optionally write to that space as well.

This way, you don't need a while loop, and it's so much easier to manage the data.

Hope that helps.

-Albatross
Last edited on
thanks i appreciate the help but i'd really like to get the boolean working because the rest of the program works the way it should when i // out the section.
Okay. If you want to keep that method of doing stuff, it's fine with me.

Your code is:
1
2
3
4
5
6
7
if(game[selection/3][selection%3] = 'X' || game[selection/3][selection%3] == 'O')
{      
    cout << "That spot is already taken.  Please select again." << endl;
    cin >> selection;
}
else
    filled = false;

...
[selection/3][selection%3] = 'X'
Wrong equals symbol there. :)

-Albatross
Last edited on
i got it working, i'm stupid
i was missing a = in if(game[selection/3][selection%3] = 'X'

thanks for working with me though!
Topic archived. No new replies allowed.