Tic Tac Toe Using 2D arrays

Hey guys, we're supposed to create a tic tac toe program using a 2D array. I have this so far but can't seem to switch players (the game will ask player X to enter a position 3 times in a row). What should I do?

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>

using namespace std;

void displayBoard(char b[][3], int size);
void grabInput(char player);
void nextPlayer (char activePlayer, char player, char player1);
void updateBoard(char b[3][3], int /*size*/, char player, char currentMove);
void checkWin();
void checkDraw();
bool isPlaying = true; //Not necessary but I'll keep it for future changes
bool hasWon = false;
bool hasDrawn = false;

char player = 'X';
char player2 = 'O';
char activePlayer = player;
char currentMove;

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

int main()
{
        displayBoard(b,3);

        while (isPlaying && !hasWon && !hasDrawn)
        {
                grabInput(activePlayer);
                updateBoard(b,3,activePlayer, currentMove);
                checkWin();
                checkDraw();
                nextPlayer (activePlayer, player, player2);
        }

                if (hasWon)
                        cout << "Congrats!";
                else
                        displayBoard(b,3);

}

void displayBoard(char b[3][3], int size)
{

        cout<<"::Tic Tac Toe::\n\n";

        for (int x = 0; x < size; x++)
        {
                for (int y = 0; y < size; y++)
                {
                        cout<<b[x][y];
                        cout<<" ";
                }
                cout<<endl;
        }
        cout<<endl;
}

void grabInput(char player)
{
        cout<<"Enter your move player "<<player<<": ";
        cin>>currentMove;
}

void nextPlayer (char activePlayer, char player, char player2) {
        if (activePlayer == player)
                activePlayer = player2;
        else activePlayer = player;
}

void updateBoard(char b[3][3], int /*size*/, char player, char currentMove)
{
        if (currentMove > '0' && currentMove <= '9')
                b[0][currentMove - '1'] = player;
}

void checkWin()
{
        if (b[0][0] == 'X' && b[0][1] == 'X' && b[0][2] == 'X')
        {
                        cout<<"\nPlayer 1 Wins!\n\n";
                        hasWon = true;
        }
        if (b[1][0] == 'X' && b[1][1] == 'X' && b[1][2] == 'X')
        {
                        cout<<"\nPlayer 1 Wins!\n\n";
                        hasWon = true;
        }
        if (b[2][0] == 'X' && b[2][1] == 'X' && b[2][2] == 'X')
        {
                        cout<<"\nPlayer 1 Wins!\n\n";
                        hasWon = true;
        }

        if (b[0][0] == 'X' && b[1][0] == 'X' && b[2][0] == 'X')
        {
                        cout<<"\nPlayer 1 Wins!\n\n";
                        hasWon = true;
        }
        if (b[0][1] == 'X' && b[1][1] == 'X' && b[2][1] == 'X')
        {
                        cout<<"\nPlayer 1 Wins!\n\n";
                        hasWon = true;
        }
        if (b[0][2] == 'X' && b[1][2] == 'X' && b[2][2] == 'X')
        {
                        cout<<"\nPlayer 1 Wins!\n\n";
                        hasWon = true;
        }

        if (b[0][0] == 'X' && b[1][1] == 'X' && b[2][2] == 'X')
        {
                        cout<<"\nPlayer 1 Wins!\n\n";
                        hasWon = true;
        }
        if (b[0][2] == 'X' && b[1][1] == 'X' && b[2][0] == 'X')
        {
                        cout<<"\nPlayer 1 Wins!\n\n";
                        hasWon = true;
        }
if (b[0][0] == 'O' && b[0][1] == 'O' && b[0][2] == 'O')
        {
                cout<<"\nPlayer Wins!\n\n";
                hasWon = true;
        }
        if (b[1][0] == 'O' && b[1][1] == 'O' && b[1][2] == 'O')
        {
                cout<<"\nPlayer Wins!\n\n";
                hasWon = true;
        }
        if (b[2][0] == 'O' && b[2][1] == 'O' && b[2][2] == 'O')
        {
                cout<<"\nPlayer Wins!\n\n";
                hasWon = true;
        }

        if (b[0][0] == 'O' && b[1][0] == 'O' && b[2][0] == 'O')
        {
                cout<<"\nPlayer Wins!\n\n";
                hasWon = true;
        }
        if (b[0][1] == 'O' && b[1][1] == 'O' && b[2][1] == 'O')
        {
                cout<<"\nPlayer Wins!\n\n";
                hasWon = true;
        }
        if (b[0][2] == 'O' && b[1][2] == 'O' && b[2][2] == 'O')
        {
                cout<<"\nPlayer Wins!\n\n";
                hasWon = true;
        }

        if (b[0][0] == 'O' && b[1][1] == 'O' && b[2][2] == 'O')
        {
                cout<<"\nPlayer Wins!\n\n";
                hasWon = true;
        }
        if (b[0][2] == 'O' && b[1][1] == 'O' && b[2][0] == 'O')
        {
                cout<<"\nPlayer Wins!\n\n";
                hasWon = true;
        }

}
void checkDraw()
{
        if (b[0][0] != '1' && b[0][1] != '2' && b[0][2] != '3' && b[1][0] != '4' && b[1][1] != '5' && b[1][2] != '6' && b[2][0] != '7' && b[2][1] != '8' && b[2][2] != '9' && !hasWon)
        hasDrawn = true;
        if (hasDrawn)
        {
                cout<<"\nThe game is a draw!\n\n";
        }
}
1
2
3
4
5
void nextPlayer (char activePlayer, char player, char player2) {
        if (activePlayer == player)
                activePlayer = player2;
        else activePlayer = player;
}


It's because you are passing char activePlayer by value. Meaning, void nextPlayer will not be using your original activePlayer, but create a copy of it. So it creates a copy, sets the copied active player to player 2, but then nothing changed, because it's just a fake activePlayer right? You want to send it in as a reference, meaning sending in the original activePlayer variable.

void nextPlayer (char& activePlayer, char player, char player2) // added the & before activePlayer

Don't forget to update your function prototype too

https://www.youtube.com/watch?v=fCAaAIYpdA4
Thanks! I'm still struggling to check if a move if valid. Any tips?
Hello, In your Function void updateBoard you can use a few if statements to check if the move is valid. For example
1
2
3
4
5
6
  if (currentMove <= 9 && b[0] [currentMove - '1'] != player1 && b[0] [currentMove - '1'] != player2)
        b[0][currentMove - '1'] = player;
    else{
        cout << "That is not a valid input";
        main();
    }(

But Since you are using a 2D array you need to check if the player's input is located in a different row and if it is change the row of the array. We can do this by modifying the if statement above and adding some more.
1
2
3
4
5
6
7
8
9
10
  if (currentMove >= 3 && b[0] [currentMove - '1'] != player1 && b[0][currentMove - '1'] != player2)
		b[0][currentMove - '1'] = player;
	else if (currentMove < 6 && currentMove > 3 && b[1][currentMove - '1'] != player1 && b[1][currentMove - '1'] != player2)
		b[1][currentMove - '1'] = player;
	else if (currentMove <= 9 && currentMove >= 6 && b[2][currentMove - '1'] != player1 && b[2][currentMove - '1'] != player2)
		b[2][currentMove - '1'] = player;
	else {
		cout << "That is not a valid input\n";
		main();
	}

Also you might want to change the variable char player to char player1 so that your program can check and see if the move is invalid. Hope this can help you.
Last edited on
Topic archived. No new replies allowed.