Player not changing: Player vs CPU tic tac toe

Hey, this program is a game where you try to beat CPU at a Tic Tac Toe game. Everything is compiling, but there seems to be a problem because the turn never goes to CPU, instead the program keeps outputting Player 1's turn. Any suggestions?

Sorry btw if the code is a little too long, I'm still in the process of simplifying it.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <cstdlib>
using namespace std;

char box[10] = {'v','1','2','3','4','5','6','7','8','9'};

int checkwin();
void board();
int CPUmove();


int main()
{
    int player = 1, i, choice;
    char mark;

    do
        {
            board();
            player = (player % 2) ? 1 : 2;

            if (player = 1){
            cout << "Enter the number of the box you want to choose:  ";
            cin >> choice;
            }

            else if (player = 2) choice == CPUmove();

            mark = (player == 1) ? 'X' : 'O';

            if (box[choice] == (char)(((int)'0')+choice))
                box[choice] = mark;

            else
                {
                    cout << "Invalid move ";
                    player--;
                    cin.ignore();
                    cin.get();
                }

            i = checkwin();
            player++;

        } while(i == -1);

    board();
    if (i == 1) cout << "==>\aPlayer " << --player << " wins!";
    else cout << "==>\aTie game, you both stink.";
    cin.ignore();
    cin.get();
    return 0;
}


int checkwin()
{
        if (box[1] == box[2] && box[2] == box[3]) return 1;

        else if (box[4] == box[5] && box[5] == box[6]) return 1;

        else if (box[7] == box[8] && box[8] == box[9]) return 1;

        else if (box[1] == box[4] && box[4] == box[7]) return 1;

        else if (box[2] == box[5] && box[5] == box[8]) return 1;

        else if (box[3] == box[6] && box[6] == box[9]) return 1;

        else if (box[1] == box[5] && box[5] == box[9]) return 1;

        else if (box[3] == box[5] && box[5] == box[7]) return 1;

        else if (box[1] != '1' && box[2] != '2' && box[3] != '3' &&

                 box[4] != '4' && box[5] != '5' && box[6] != '6' &&

                 box[7] != '7' && box[8] != '8' && box[9] != '9')

            return 0;

        else return -1;
}


void board()
{
        cout << "\n\n\tTic Tac Toe\n\n";

        cout << "Can you beat CPU?" << endl << endl;
        cout << endl;

        cout << "     |     |     " << endl;
        cout << "  " << box[1] << "  |  " << box[2] << "  |  " << box[3] << end\
l;

        cout << "_ _ _|_ _ _|_ _ _" << endl;
        cout << "     |     |     " << endl;

        cout << "  " << box[4] << "  |  " << box[5] << "  |  " << box[6] << end\
l;

        cout << "_ _ _|_ _ _|_ _ _" << endl;
        cout << "     |     |     " << endl;

        cout << "  " << box[7] << "  |  " << box[8] << "  |  " << box[9] << end\
l;

        cout << "     |     |     " << endl << endl;
}

int CPUmove()
{

    cout << "CPU takes its turn...\n";

    if (box[2] == box[3] || box[4] == box[7] ||
        box[5] == box[9]) return box[1];

    if (box[1] == box[3] || box[5] == box[8]) return box[2];

    if (box[1] == box[2] || box[6] == box[9]) return box[3];

    if (box[1] == box[7] || box[5] == box[6]) return box[4];

    if (box[1] == box[9] || box[4] == box[6] ||
        box[2] == box[8] || box[3] == box[7]) return box[5];

    if (box[3] == box[9] || box[4] == box[5]) return box[6];

    if (box[1] == box[4] || box[8] == box[9] ||
        box[3] == box[5]) return box[7];

    if (box[7] == box[9] || box[2] == box[5]) return box[8];

    if (box[1] == box[5] || box[7] == box[8] ||
        box[3] == box[6]) return box[9];

    if (box[1] != 'X' && box[1] != 'O') return box[1];

    else if (box[3] != 'X' && box[3] != 'O') return box[3];

    else if (box[7] != 'X' && box[7] != 'O') return box[7];

    else if (box[9] != 'X' && box[9] != 'O') return box[9];

    else {
        srand(time(0));
        return rand() % 9 + 1;
    }
}
Line 22 & 27, you are using = instead of == in your IF statement.

Also on line 27 you are using == instead of = where you have choice == CPUMove()

Lines 93-107 you seem to have a \1 at the end of your cout statements.
> Lines 93-107 you seem to have a \1 at the end of your cout statements.

I don't understand? Those are just endl; statements.

And that error is fixed, but now every CPU turn it just outputs "invalid move."

I didn't compile it, I just checked what you posted online - those errors stood out to me.


nvm, think the \l was the forum formatting your post.
Last edited on
Do you know what the problem could be on the program outputting "invalid move" on every CPU turn?

When CPUMove is run, it does the checks on the board and returns the value at an index position of box, in which case it would return the character code of either 1, 2, 3, 4, 5, 6, 7, 8, 9

For example, I took my move, set a breakpoint after the CPUMove call and this was the contents of your variables...

‡		CPUmove returned	51	int
		choice	51	int
		mark	88 'X'	char
		player	2	int



Shouldn't you be returning the position number and not the contents at that position?

I fixed CPUmove so that it returns the correct thing, and the same problem is coming up again where it doesn't switch turns. What do you think?
line 148 - you only need to call srand to seed the random number once at the beginning of the main function.
Post your changes.

Edit: as wildblue said you should only run srand once at the start of your program.
Last edited on
@Softrix Sorry that was a dumb mistake, it compiles fine now but there's just some bugs I need to fix. I'll post again if I need help.

@wildblue I'll check that out and see how it works. I think that's more of an efficiency issue rather than an issue that would cause bugs, no?

You will end up with the same random numbers pretty much if you keep calling it.
The program compiles but it bugs out after around 5 moves, I can't seem to figure out what the problem is. Could you guys do me a huge favor and give it a few runs to see what's going on?
Post your changed code, I didn't save my changes ;-)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <cstdlib>
using namespace std;

char box[10] = {'v','1','2','3','4','5','6','7','8','9'};

int checkwin();
void board();
int CPUmove();


int main()
{
    int player = 1, i, choice;
    char mark;
    srand(time(0));

    do
        {
            board();
            player = (player % 2) ? 1 : 2;

            if (player == 1){
            cout << "Enter the number of the box you want to choose:  ";
            cin >> choice;
            }

            else if (player == 2) choice = CPUmove();

            mark = (player == 1) ? 'X' : 'O';

            if (box[choice] == (char)(((int)'0')+choice))
                box[choice] = mark;

            else
                {
                    cout << "Invalid move ";
                    player--;
                    cin.ignore();
                    cin.get();
                }

            i = checkwin();
            player++;

        } while(i == -1);

    board();
    if (i == 1) cout << "==>\aPlayer " << --player << " wins!";
    else cout << "==>\aTie game, you both stink.";
    cin.ignore();
    cin.get();
    return 0;
}


int checkwin()
{
        if (box[1] == box[2] && box[2] == box[3]) return 1;

        else if (box[4] == box[5] && box[5] == box[6]) return 1;

        else if (box[7] == box[8] && box[8] == box[9]) return 1;

        else if (box[1] == box[4] && box[4] == box[7]) return 1;

        else if (box[2] == box[5] && box[5] == box[8]) return 1;

        else if (box[3] == box[6] && box[6] == box[9]) return 1;

        else if (box[1] == box[5] && box[5] == box[9]) return 1;

        else if (box[3] == box[5] && box[5] == box[7]) return 1;

        else if (box[1] != '1' && box[2] != '2' && box[3] != '3' &&

                 box[4] != '4' && box[5] != '5' && box[6] != '6' &&

                 box[7] != '7' && box[8] != '8' && box[9] != '9')

            return 0;

        else return -1;
}


void board()
{
        cout << "\n\n\tTic Tac Toe\n\n";

        cout << "Can you beat CPU?" << endl << endl;
        cout << endl;

        cout << "     |     |     " << endl;
        cout << "  " << box[1] << "  |  " << box[2] << "  |  " << box[3] << end\
l;

        cout << "_ _ _|_ _ _|_ _ _" << endl;
        cout << "     |     |     " << endl;

        cout << "  " << box[4] << "  |  " << box[5] << "  |  " << box[6] << end\
l;

        cout << "_ _ _|_ _ _|_ _ _" << endl;
        cout << "     |     |     " << endl;

        cout << "  " << box[7] << "  |  " << box[8] << "  |  " << box[9] << end\
l;

        cout << "     |     |     " << endl << endl;
}

int CPUmove()
{

    cout << "CPU takes its turn...\n";

    if (box[2] == box[3] || box[4] == box[7] ||
        box[5] == box[9]) return 1;

    if (box[1] == box[3] || box[5] == box[8]) return 2;

    if (box[1] == box[2] || box[6] == box[9]) return 3;

    if (box[1] == box[7] || box[5] == box[6]) return 4;

    if (box[1] == box[9] || box[4] == box[6] ||
        box[2] == box[8] || box[3] == box[7]) return 5;

    if (box[3] == box[9] || box[4] == box[5]) return 6;

    if (box[1] == box[4] || box[8] == box[9] ||
        box[3] == box[5]) return 7;

    if (box[7] == box[9] || box[2] == box[5]) return 8;

    if (box[1] == box[5] || box[7] == box[8] ||
        box[3] == box[6]) return 9;

    if (box[1] != 'X' && box[1] != 'O') return 1;

    if (box[3] != 'X' && box[3] != 'O') return 3;

    if (box[7] != 'X' && box[7] != 'O') return 7;

    if (box[9] != 'X' && box[9] != 'O') return 9;

    else {
        return rand() % 9 + 1;
    }
}

I hope you follow as I try to explain..

If I were to put my first move in 2 (X), then computer puts in 1 (0) then I put my 2nd move in 3(X), the CPUMove() function will see 2 and 3 are the same and attempt to put O in position 1 regardless if its taken or not.

Just try making your first move on 2, then 3, computer will see 2 and 3 filled regardless of which player put them there and try to put X in position 1 which was filled by player 2 (CPU).
Okay, thanks a lot for the help! I fixed all the problems and the program seems to be running really smoothly.

Great, nice to see :)
closed account (z1CpDjzh)
i would point out many flaws but the codes to ugly... tip INCLUDE CTIME
TheGentlmen wrote:
i would point out many flaws but the codes to ugly...


@TheGentlmen Was that even necessary? - everyone starts somewhere, and its all part of the learning process! - What is important is he knows this and is working on it...

fizanimtiaz93 wrote:
Sorry btw if the code is a little too long, I'm still in the process of simplifying it.
Topic archived. No new replies allowed.