Tic Tac Toe Works, but not well

I recently made this tic tac toe game and im happy that i finished it. It works like a charm, but now i want to improve on it cause it does have problems. One is when it prompts the user to enter a number, if the user enters anything but a number it acts all crazy. Can you help me figure out why? i thought my default in my switch-case would handle it but it didnt. it probably has to do something with the loop but not sure. Another problem is, when one user enters 3 for example and the board changes it to X, if the other player enters a 3, it then changes it to an O. I want it to use my default in my switch-case when that happens but it doesnt. Please help. Would like to learn whats going on.

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
#include <iostream>

using namespace std;



void TTTxANDo(char array [][3]);
int WIN (char array [][3]);

void TTTBoard (char array [][3])
{

        cout << "Player 1 (X)  -  Player 2 (O)" << endl;
        cout << endl;
        cout << "     |     |     " << endl;
        cout << "  " << array[0][0] << "  |  " << array[0][1] << "  |  " << array[0][2] << endl;
        cout << "_____|_____|_____" << endl;
        cout << "     |     |     " << endl;
        cout << "  " << array[1][0] << "  |  " << array[1][1] << "  |  " << array[1][2] << endl;
        cout << "_____|_____|_____" << endl;
        cout << "     |     |     " << endl;
        cout << "  " << array[2][0] << "  |  " << array[2][1] << "  |  " << array[2][2] << endl;
        cout << "     |     |     " << endl;

}

int i = 0;
int player = 1;
int winning_number = 0;
int main()
{

    char TTTBarray [3][3] = {'1','2','3','4','5','6','7','8','9'};
    char letter_mark;
    player = (player % 2) ? 1:2;
    letter_mark = (player == 1) ? 'X' : 'O';
    for (; i < 10; ++i)
        {

            TTTBoard(TTTBarray);
            winning_number = WIN(TTTBarray);
            if ( winning_number == 1)
                {
                    break;
                }
            if (i == 9)
            {
                cout << "Cats Game. No One Wins." << endl;
                break;
            }
            TTTxANDo(TTTBarray);
            ++player;
        }

}

void TTTxANDo (char array [][3])
{
    player = (player % 2) ? 1:2;
    char letter_mark;
    letter_mark = (player == 1) ? 'X' : 'O';
    int number;
    cout << "Player " << player << " Choose a number on the board." << endl;
    cin >> number;
        switch (number)
            {
                case 1:
                array [0][0] = letter_mark;
                break;
                case 2:
                array [0][1] = letter_mark;
                break;
                case 3:
                array [0][2] = letter_mark;
                break;
                case 4:
                array [1][0] = letter_mark;
                break;
                case 5:
                array [1][1] = letter_mark;
                break;
                case 6:
                array [1][2] = letter_mark;
                break;
                case 7:
                array [2][0] = letter_mark;
                break;
                case 8:
                array [2][1] = letter_mark;
                break;
                case 9:
                array [2][2] = letter_mark;
                break;
                default:
                cout << "You did not choose a correct number. Try Again." << endl;
                --player;
                --i;
                break;
            }
}

int WIN (char array[][3])
{
    int result = 0;
        if (array[0][0] == array[0][1] && array [0][1] == array [0][2])
            {
                cout << "PLAYER " << --player << " WINS!!!!" << endl;
                ++result;
            }
        else if (array[1][0] == array[1][1] && array [1][1] == array [1][2])
            {
                cout << "PLAYER " << --player << " WINS!!!!" << endl;
                ++result;
            }
        else if (array[2][0] == array[2][1] && array [2][1] == array [2][2])
            {
                cout << "PLAYER " << --player << " WINS!!!!" << endl;
                ++result;
            }
        else if (array[0][0] == array[1][0] && array [1][0] == array [2][0])
            {
                cout << "PLAYER " << --player << " WINS!!!!" << endl;
                ++result;
            }
        else if (array[0][1] == array [1][1] && array [1][1] == array [2][1])
            {
                cout << "PLAYER " << --player << " WINS!!!!" << endl;
                ++result;
            }
        else if (array[0][2] == array[1][2] && array [1][2] == array [2][2])
            {
                cout << "PLAYER " << --player << " WINS!!!!" << endl;
                ++result;
            }
        else if (array[0][0] == array[1][1] && array [1][1] == array [2][2])
            {
                cout << "PLAYER " << --player << " WINS!!!!" << endl;
                ++result;
            }
        else if (array[0][2] == array[1][1] && array [1][1] == array [2][0])
            {
                cout << "PLAYER " << --player << " WINS!!!!" << endl;
                ++result;
            }
    return result;

}
Last edited on
You need a loop here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "Player " << player << " Choose a number on the board." << endl;
    cin >> number;
        switch (number)
            {
                case 1:
                array [0][0] = letter_mark;
                break;
                // ...
                default:
                cout << "You did not choose a correct number. Try Again." << endl;
                --player;
                --i;
                break;
            }

It should test to make sure they entered a valid number. You should also get an error when they select a position that's already been selected, as in it overrides the previous. Also, what is the purpose of --player and --i?

To make sure the user can't override another user, before entering the players X or O on the board, make sure there isn't already a value there. Also, why are you using a 2D array, but using a switch case for values 1 - 9? That makes no sense to me. Keep it simple on yourself.
how would i test to see if they did enter a valid number?

the --player is if the player enters a number other than 1 - 9, that way it doesnt go to the next player.

the --i is for the same thing, that way the for loop end early before the game is finished.

I didnt know about the 2D array and switch case, i figured since the board is 3 by 3, i thought i make an array like that.
That makes sense, but you're asking for one value from the user, not two. I made a tic tac toe game a while back, and used a 2D array for it, but my user input was a lot different than yours. What I'd suggest is that you do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    player = (player % 2) ? 1:2;
    char letter_mark = (player == 1) ? 'X' : 'O';
    int number;
    do {
        cout << "Player " << player << " Choose a number on the board." << endl;
        cin >> number;
    } while (number < 1 || number > 9);
    // Checks to make sure it's a valid move...
    if (array[number / 3][number % 3] != 'X' && array[number / 3][number % 3] != 'O')
        array[number / 3][number % 3] = letter_mark;
    else {
        // User Selected an invalid spot...
        cout << "You did not choose a correct number. Try Again." << endl;
        --player;
        --i;
    }
Last edited on
Ok, your code gave me an idea on how to put my array in the loop using if-else statements instead of switch-case, and it now WORKS!!!! thank you. When using your if statment, it wouldnt put the X or the O in the number i pressed, but everything else worked fine. Here is what I did

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
void TTTxANDo (char array [][3])
{
    player = (player % 2) ? 1:2;
    char letter_mark = (player == 1) ? 'X' : 'O';
    int number;
    do {
        cout << "Player " << player << " Choose a number on the board." << endl;
        cin >> number;
    } while (number < 1 || number > 9);
        {
            if (number == 1 && array[0][0] == '1')
                {
                    array[0][0] = letter_mark;
                }
            else if (number == 2 && array[0][1] == '2')
                {
                    array[0][1] = letter_mark;
                }
            else if (number == 3 && array[0][2] == '3')
                {
                    array[0][2] = letter_mark;
                }
           else if (number == 4 && array[1][0] == '4')
                {
                    array[1][0] = letter_mark;
                }
           else if (number == 5 && array[1][1] == '5')
                {
                    array[1][1] = letter_mark;
                }
           else if (number == 6 && array[1][2] == '6')
                {
                    array[1][2] = letter_mark;
                }
            else if (number == 7 && array[2][0] == '7')
                {
                    array[2][0] = letter_mark;
                }
           else if (number == 8 && array[2][1] == '8')
                {
                    array[2][1] = letter_mark;
                }
           else if (number == 9 && array[2][2] == '9')
                {
                    array[2][2] = letter_mark;
                }

            else
                {
                    cout << "You did not choose a correct number. Try Again." << endl;
                    --player;
                    --i;
                }
        }
}
Here, I missed this one line:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    player = (player % 2) ? 1:2;
    char letter_mark = (player == 1) ? 'X' : 'O';
    int number;
    do {
        cout << "Player " << player << " Choose a number on the board." << endl;
        cin >> number;
    } while (number < 1 || number > 9);
    --number; // This Line
    // Checks to make sure it's a valid move...
    if (array[number / 3][number % 3] != 'X' && array[number / 3][number % 3] != 'O')
        array[number / 3][number % 3] = letter_mark;
    else {
        // User Selected an invalid spot...
        cout << "You did not choose a correct number. Try Again." << endl;
        --player;
        --i;
    }
Last edited on
yes works perfect now. Thank you.
Topic archived. No new replies allowed.