Question:
I am trying to make Tic-Tac Toe in C++. I am now working on making the AI play (through randomness) and I want to see if a value is already in the place
array I have (if it is you/the AI will have to change your/it’s move). I heard you have to use the find()
function but I have tried it and don’t understand how to use it.
Repl link:
https://replit.com/@SalladShooter/C-Plus-Plus#main.cpp
#include <iostream>
#include <algorithm>
#include <array>
using namespace std;
int main() {
char choice;
int placeCurrent;
int place[0];
string boardTopLeft{"1"};
string boardTopMiddle{"2"};
string boardTopRight{"3"};
string boardMidLeft{"4"};
string boardMidMiddle{"5"};
string boardMidRight{"6"};
string boardBotLeft{"7"};
string boardBotMiddle{"8"};
string boardBotRight{"9"};
cout << "Tic-Tac Toe!" << endl;
for (int i = 0; i < 9; i++) {
// Print Board
cout << boardTopLeft << " " << boardTopMiddle << " " << boardTopRight << endl;
cout << boardMidLeft << " " << boardMidMiddle << " " << boardMidRight << endl;
cout << boardBotLeft << " " << boardBotMiddle << " " << boardBotRight << "\n" << endl;
choice = 'X';
cout << "Where do you want to place X?" << endl;
cin >> placeCurrent;
place[i] = placeCurrent;
cout << "\n" << endl;
if (placeCurrent == 1) {
boardTopLeft = choice;
} else if (placeCurrent == 2) {
boardTopMiddle = choice;
} else if (placeCurrent == 3) {
boardTopRight = choice;
} else if (placeCurrent == 4) {
boardMidLeft = choice;
} else if (placeCurrent == 5) {
boardMidMiddle = choice;
} else if (placeCurrent == 6) {
boardMidRight = choice;
} else if (placeCurrent == 7) {
boardBotLeft = choice;
} else if (placeCurrent == 8) {
boardBotMiddle = choice;
} else if (placeCurrent == 9) {
boardBotRight = choice;
}
// Print Board
cout << boardTopLeft << " " << boardTopMiddle << " " << boardTopRight << endl;
cout << boardMidLeft << " " << boardMidMiddle << " " << boardMidRight << endl;
cout << boardBotLeft << " " << boardBotMiddle << " " << boardBotRight << endl;
return 0;
}
}
1 Like
Hi @SalladShooter !
The following code was corrected by ChatGPT, an AI:
#include <iostream>
#include <algorithm>
#include <array>
using namespace std;
int main() {
char choice = 'X';
int placeCurrent;
int place[9] = {0}; // Array to store the positions where X or O is placed
string boardTopLeft{"1"};
string boardTopMiddle{"2"};
string boardTopRight{"3"};
string boardMidLeft{"4"};
string boardMidMiddle{"5"};
string boardMidRight{"6"};
string boardBotLeft{"7"};
string boardBotMiddle{"8"};
string boardBotRight{"9"};
cout << "Tic-Tac-Toe!" << endl;
for (int i = 0; i < 9; i++) {
// Print Board
cout << boardTopLeft << " " << boardTopMiddle << " " << boardTopRight << endl;
cout << boardMidLeft << " " << boardMidMiddle << " " << boardMidRight << endl;
cout << boardBotLeft << " " << boardBotMiddle << " " << boardBotRight << endl;
// Determine player's turn
if (i % 2 == 0) {
choice = 'X';
} else {
choice = 'O';
}
cout << "Where do you want to place " << choice << "?" << endl;
cin >> placeCurrent;
place[i] = placeCurrent - 1; // Subtract 1 to store the correct index in the place array
cout << "\n";
if (placeCurrent == 1) {
boardTopLeft = choice;
} else if (placeCurrent == 2) {
boardTopMiddle = choice;
} else if (placeCurrent == 3) {
boardTopRight = choice;
} else if (placeCurrent == 4) {
boardMidLeft = choice;
} else if (placeCurrent == 5) {
boardMidMiddle = choice;
} else if (placeCurrent == 6) {
boardMidRight = choice;
} else if (placeCurrent == 7) {
boardBotLeft = choice;
} else if (placeCurrent == 8) {
boardBotMiddle = choice;
} else if (placeCurrent == 9) {
boardBotRight = choice;
}
}
// Print final board
cout << "Final Board:" << endl;
cout << boardTopLeft << " " << boardTopMiddle << " " << boardTopRight << endl;
cout << boardMidLeft << " " << boardMidMiddle << " " << boardMidRight << endl;
cout << boardBotLeft << " " << boardBotMiddle << " " << boardBotRight << endl;
// Check for a win or draw (Tic-Tac-Toe)
bool win = false;
for (int i = 0; i < 9; i += 3) {
if (place[i] == place[i + 1] && place[i] == place[i + 2]) {
win = true;
break;
}
}
for (int i = 0; i < 3; i++) {
if (place[i] == place[i + 3] && place[i] == place[i + 6]) {
win = true;
break;
}
}
if ((place[0] == place[4] && place[0] == place[8]) || (place[2] == place[4] && place[2] == place[6])) {
win = true;
}
if (win) {
cout << "Congratulations! Player " << choice << " wins!" << endl;
} else {
cout << "It's a draw!" << endl;
}
return 0;
}
P.S. ChatGPT should get the solution if this works.
1 Like
@NateDhaliwal but it doesn’t include the bot to play against just PvP. It also is checking for winning not for taken places (this means players can replace current moves).
Again, the following code was written by ChatGPT, an AI:
#include <iostream>
#include <algorithm>
#include <array>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
char choice = 'X';
int placeCurrent;
int place[9] = {0}; // Array to store the positions where X or O is placed
string boardTopLeft{"1"};
string boardTopMiddle{"2"};
string boardTopRight{"3"};
string boardMidLeft{"4"};
string boardMidMiddle{"5"};
string boardMidRight{"6"};
string boardBotLeft{"7"};
string boardBotMiddle{"8"};
string boardBotRight{"9"};
cout << "Tic-Tac-Toe!" << endl;
for (int i = 0; i < 9; i++) {
// Print Board
cout << boardTopLeft << " " << boardTopMiddle << " " << boardTopRight << endl;
cout << boardMidLeft << " " << boardMidMiddle << " " << boardMidRight << endl;
cout << boardBotLeft << " " << boardBotMiddle << " " << boardBotRight << endl;
// Determine player's turn
if (i % 2 == 0) {
choice = 'X';
cout << "Where do you want to place " << choice << "?" << endl;
cin >> placeCurrent;
placeCurrent--; // Adjust to 0-based index
// Check if the position is already taken
while (place[placeCurrent] != 0) {
cout << "Position " << placeCurrent + 1 << " is already taken. Choose another position: ";
cin >> placeCurrent;
placeCurrent--; // Adjust to 0-based index
}
} else {
choice = 'O';
// Computer randomly places 'O'
srand(static_cast<unsigned>(time(0)));
do {
placeCurrent = rand() % 9;
} while (place[placeCurrent] != 0);
}
place[i] = placeCurrent;
// Update the board based on the chosen position
if (placeCurrent == 0) {
boardTopLeft = choice;
} else if (placeCurrent == 1) {
boardTopMiddle = choice;
} else if (placeCurrent == 2) {
boardTopRight = choice;
} else if (placeCurrent == 3) {
boardMidLeft = choice;
} else if (placeCurrent == 4) {
boardMidMiddle = choice;
} else if (placeCurrent == 5) {
boardMidRight = choice;
} else if (placeCurrent == 6) {
boardBotLeft = choice;
} else if (placeCurrent == 7) {
boardBotMiddle = choice;
} else if (placeCurrent == 8) {
boardBotRight = choice;
}
}
// Print final board
cout << "Final Board:" << endl;
cout << boardTopLeft << " " << boardTopMiddle << " " << boardTopRight << endl;
cout << boardMidLeft << " " << boardMidMiddle << " " << boardMidRight << endl;
cout << boardBotLeft << " " << boardBotMiddle << " " << boardBotRight << endl;
// Check for a win or draw (Tic-Tac-Toe)
bool win = false;
for (int i = 0; i < 9; i += 3) {
if (place[i] == place[i + 1] && place[i] == place[i + 2]) {
win = true;
break;
}
}
for (int i = 0; i < 3; i++) {
if (place[i] == place[i + 3] && place[i] == place[i + 6]) {
win = true;
break;
}
}
if ((place[0] == place[4] && place[0] == place[8]) || (place[2] == place[4] && place[2] == place[6])) {
win = true;
}
if (win) {
cout << "Congratulations! Player " << choice << " wins!" << endl;
} else {
cout << "It's a draw!" << endl;
}
return 0;
}
@NateDhaliwal the positions reset after every play, and I can’t see the moves (only the current move I made).
Wdym? I ran it and it shows moves from both the player and the computer.
Ok, our dear friend ChatGPT has admitted his mistake and has “fixed” it.
The following code was written by ChatGPT, an AI:
#include <iostream>
#include <algorithm>
#include <array>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
char choice = 'X';
int placeCurrent;
int place[9] = {0}; // Array to store the positions where X or O is placed
string boardTopLeft{"1"};
string boardTopMiddle{"2"};
string boardTopRight{"3"};
string boardMidLeft{"4"};
string boardMidMiddle{"5"};
string boardMidRight{"6"};
string boardBotLeft{"7"};
string boardBotMiddle{"8"};
string boardBotRight{"9"};
cout << "Tic-Tac-Toe!" << endl;
for (int i = 0; i < 9; i++) {
// Print Board
cout << boardTopLeft << " " << boardTopMiddle << " " << boardTopRight << endl;
cout << boardMidLeft << " " << boardMidMiddle << " " << boardMidRight << endl;
cout << boardBotLeft << " " << boardBotMiddle << " " << boardBotRight << endl;
// Determine player's turn
if (i % 2 == 0) {
choice = 'X';
cout << "Where do you want to place " << choice << "?" << endl;
cin >> placeCurrent;
placeCurrent--; // Adjust to 0-based index
// Check if the position is already taken
while (place[placeCurrent] != 0) {
cout << "Position " << placeCurrent + 1 << " is already taken. Choose another position: ";
cin >> placeCurrent;
placeCurrent--; // Adjust to 0-based index
}
} else {
choice = 'O';
// Computer randomly places 'O'
srand(static_cast<unsigned>(time(0)));
do {
placeCurrent = rand() % 9;
} while (place[placeCurrent] != 0);
place[placeCurrent] = placeCurrent + 1; // Update the place array
}
place[i] = placeCurrent;
// Update the board based on the chosen position
if (placeCurrent == 0) {
boardTopLeft = choice;
} else if (placeCurrent == 1) {
boardTopMiddle = choice;
} else if (placeCurrent == 2) {
boardTopRight = choice;
} else if (placeCurrent == 3) {
boardMidLeft = choice;
} else if (placeCurrent == 4) {
boardMidMiddle = choice;
} else if (placeCurrent == 5) {
boardMidRight = choice;
} else if (placeCurrent == 6) {
boardBotLeft = choice;
} else if (placeCurrent == 7) {
boardBotMiddle = choice;
} else if (placeCurrent == 8) {
boardBotRight = choice;
}
}
// Print final board
cout << "Final Board:" << endl;
cout << boardTopLeft << " " << boardTopMiddle << " " << boardTopRight << endl;
cout << boardMidLeft << " " << boardMidMiddle << " " << boardMidRight << endl;
cout << boardBotLeft << " " << boardBotMiddle << " " << boardBotRight << endl;
// Check for a win or draw (Tic-Tac-Toe)
bool win = false;
for (int i = 0; i < 9; i += 3) {
if (place[i] == place[i + 1] && place[i] == place[i + 2]) {
win = true;
break;
}
}
for (int i = 0; i < 3; i++) {
if (place[i] == place[i + 3] && place[i] == place[i + 6]) {
win = true;
break;
}
}
if ((place[0] == place[4] && place[0] == place[8]) || (place[2] == place[4] && place[2] == place[6])) {
win = true;
}
if (win) {
cout << "Congratulations! Player " << choice << " wins!" << endl;
} else {
cout << "It's a draw!" << endl;
}
return 0;
}
@NateDhaliwal still can’t see the bot’s moves. I also keep seeing to boards one is correct (without the bot) and the second is incorrect with just showing my first move.
I tried and the problem is that the computer overrides your move; it can place O in your X box. Give me a sec.
Like the previous issue, ChatGPT admits his mistake and has apologised.
The following code is written by ChatGPT, an AI:
#include <iostream>
#include <algorithm>
#include <array>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
char choice = 'X';
int placeCurrent;
int place[9] = {0}; // Array to store the positions where X or O is placed
char board[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
cout << "Tic-Tac-Toe!" << endl;
for (int i = 0; i < 9; i++) {
// Print Board
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
cout << board[row][col] << " ";
}
cout << endl;
}
// Determine player's turn
if (i % 2 == 0) {
choice = 'X';
cout << "Where do you want to place " << choice << "?" << endl;
cin >> placeCurrent;
placeCurrent--; // Adjust to 0-based index
// Check if the position is already taken
while (place[placeCurrent] != 0) {
cout << "Position " << placeCurrent + 1 << " is already taken. Choose another position: ";
cin >> placeCurrent;
placeCurrent--; // Adjust to 0-based index
}
} else {
choice = 'O';
// Computer randomly places 'O'
srand(static_cast<unsigned>(time(0)));
do {
placeCurrent = rand() % 9;
} while (place[placeCurrent] != 0);
place[placeCurrent] = placeCurrent + 1; // Update the place array
}
place[i] = placeCurrent;
// Update the board based on the chosen position
int row = placeCurrent / 3;
int col = placeCurrent % 3;
board[row][col] = choice;
}
// Print final board
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
cout << board[row][col] << " ";
}
cout << endl;
}
// Check for a win or draw (Tic-Tac-Toe)
bool win = false;
for (int i = 0; i < 9; i += 3) {
if (place[i] == place[i + 1] && place[i] == place[i + 2]) {
win = true;
break;
}
}
for (int i = 0; i < 3; i++) {
if (place[i] == place[i + 3] && place[i] == place[i + 6]) {
win = true;
break;
}
}
if ((place[0] == place[4] && place[0] == place[8]) || (place[2] == place[4] && place[2] == place[6])) {
win = true;
}
if (win) {
cout << "Congratulations! Player " << choice << " wins!" << endl;
} else {
cout << "It's a draw!" << endl;
}
return 0;
}
@SalladShooter edited it.
@SalladShooter Did the above post help you?
Here is the code again, it was fixed.
#include <iostream>
#include <algorithm>
#include <array>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
char choice = 'X';
int placeCurrent;
int place[9] = {0}; // Array to store the positions where X or O is placed
char board[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
cout << "Tic-Tac-Toe!" << endl;
for (int i = 0; i < 9; i++) {
// Print Board
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
cout << board[row][col] << " ";
}
cout << endl;
}
// Determine player's turn
if (i % 2 == 0) {
choice = 'X';
cout << "Where do you want to place " << choice << "?" << endl;
cin >> placeCurrent;
placeCurrent--; // Adjust to 0-based index
// Check if the position is already taken
while (place[placeCurrent] != 0) {
cout << "Position " << placeCurrent + 1 << " is already taken. Choose another position: ";
cin >> placeCurrent;
placeCurrent--; // Adjust to 0-based index
}
} else {
choice = 'O';
// Computer randomly places 'O'
srand(static_cast<unsigned>(time(0)));
do {
placeCurrent = rand() % 9;
} while (place[placeCurrent] != 0);
place[placeCurrent] = placeCurrent + 1; // Update the place array
}
place[i] = placeCurrent;
// Update the board based on the chosen position
int row = placeCurrent / 3;
int col = placeCurrent % 3;
board[row][col] = choice;
}
// Print final board
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
cout << board[row][col] << " ";
}
cout << endl;
}
// Check for a win or draw (Tic-Tac-Toe)
bool win = false;
for (int i = 0; i < 9; i += 3) {
if (place[i] == place[i + 1] && place[i] == place[i + 2]) {
win = true;
break;
}
}
for (int i = 0; i < 3; i++) {
if (place[i] == place[i + 3] && place[i] == place[i + 6]) {
win = true;
break;
}
}
if ((place[0] == place[4] && place[0] == place[8]) || (place[2] == place[4] && place[2] == place[6])) {
win = true;
}
if (win) {
cout << "Congratulations! Player " << choice << " wins!" << endl;
} else {
cout << "It's a draw!" << endl;
}
return 0;
}
@NateDhaliwal sorry I had to do something, the bot can replace my moves and the board is still printed twice in a row, also I can never pick 3 even if it is open.
Ok, try this even more updated code.
The following code was written by ChatGPT, an AI:
#include <iostream>
#include <algorithm>
#include <array>
#include <cstdlib>
#include <ctime>
using namespace std;
bool checkWin(const char board[3][3], char player) {
// Check rows and columns
for (int i = 0; i < 3; i++) {
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) ||
(board[0][i] == player && board[1][i] == player && board[2][i] == player)) {
return true;
}
}
// Check diagonals
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return true;
}
return false;
}
int main() {
char choice = 'X';
int placeCurrent;
int place[9] = {0}; // Array to store the positions where X or O is placed
char board[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
cout << "Tic-Tac-Toe!" << endl;
for (int i = 0; i < 9; i++) {
// Print Board
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
cout << board[row][col] << " ";
}
cout << endl;
}
// Determine player's turn
if (i % 2 == 0) {
choice = 'X';
cout << "Where do you want to place " << choice << "?" << endl;
cin >> placeCurrent;
placeCurrent--; // Adjust to 0-based index
// Check if the position is already taken
while (place[placeCurrent] != 0) {
cout << "Position " << placeCurrent + 1 << " is already taken. Choose another position: ";
cin >> placeCurrent;
placeCurrent--; // Adjust to 0-based index
}
} else {
choice = 'O';
// Computer randomly places 'O'
srand(static_cast<unsigned>(time(0)));
do {
placeCurrent = rand() % 9;
} while (place[placeCurrent] != 0);
place[placeCurrent] = placeCurrent + 1; // Update the place array
}
place[i] = placeCurrent;
// Update the board based on the chosen position
int row = placeCurrent / 3;
int col = placeCurrent % 3;
board[row][col] = choice;
// Check for a win
if (i >= 4 && checkWin(board, choice)) {
// Print the final board
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
cout << board[row][col] << " ";
}
cout << endl;
}
cout << "Congratulations! Player " << choice << " wins!" << endl;
return 0;
}
}
// Print final board
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
cout << board[row][col] << " ";
}
cout << endl;
}
cout << "It's a draw!" << endl;
return 0;
}
P.S. Why can’t ChatGPT solve the issues all at once!?
@NateDhaliwal same errors still (I can’t place an X in positions 1, 2, 3, 4, or 6). Try to not use ChatGPT this time (it says it self that it can be incorrect).
Strange, because I can place them.
Before using std::find
function, you need to initialize your place
array correctly. In your code, int place[0];
initializes an array of size 0, which means it can’t hold any values. For a Tic-Tac-Toe game, you’ll need an array of size 9, like int place[9];
.
int main() {
int place[9] = {0}; // initialize with all zeros
int placeCurrent;
As for the std::find
function, it is used to find an element in a container. The function needs a start and an end iterator, and a value to find. If the value is found, it returns an iterator to the first match. If not, it returns the end iterator.
I took a hint from what @NateDhaliwal provided it just need some adjustments about how to use std::find
:
for (int i = 0; i < 9; i++) {
cout << "Where do you want to place X?" << endl;
cin >> placeCurrent;
// You need to check if the value is already in the array
if (find(begin(place), end(place), placeCurrent) != end(place)) {
cout << "This place is already taken." << endl;
i--; // don't forget to decrement the counter to retry this turn
continue; // the continue skip the rest of the iteration and start the next one
}
place[i] = placeCurrent;
Another thing is, the return 0;
is inside the loop which means the loop will execute only once. Make sure to place return 0;
after the loop, not inside it.
return 0; //before the last curly bracket
}
It did something like this:

So now you just have to add a function to check if there’s a winner after each move.
Edit: I kinda hyped doing the game and did something after too, you can check it, maybe it can bring you new ideas too
https://replit.com/@WindLother/C-Plus-Plus-help
1 Like
@WindLother how would I incorporate this code into my program? I tried and it throws errors of unknown identifiers.
Oh, I used the code you post in your first example.
If you want to do this with your actual code hmmm I think it’s better if I wrote the full code and give you commentary for each piece
#include <iostream>
#include <algorithm>
#include <array>
#include <cstdlib> // For rand()
#include <ctime> // For time()
#include <thread> // For std::this_thread::sleep_for
#include <chrono> // For std::chrono::seconds
using namespace std;
// Function to check if the current player has won the game
bool checkWin(const char board[3][3], char player) {
// Check rows and columns
for (int i = 0; i < 3; i++) {
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) ||
(board[0][i] == player && board[1][i] == player && board[2][i] == player)) {
return true; // A win was found
}
}
// Check diagonals
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return true; // A win was found
}
return false; // No win was found
}
// Function to print the current state of the game board
void printBoard(const char board[3][3]) {
for (int i = 0; i < 3; i++) {
if (i != 0) // Adds a line separator between rows, not before the first row
cout << "\n---------\n";
cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << " ";
}
cout << "\n\n";
}
// Main game logic
int main() {
srand(time(NULL)); // Seed for random number generation
char choice = 'X'; // The character for the current player
int place[9] = {0}; // Array to store the locations where the players have played
int placeCurrent; // The current location the player is attempting to play in
// The game board, a 3x3 array of characters
char board[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
// Instructions and initial game message
cout << "Welcome to Tic-Tac-Toe!\n";
cout << "The board is numbered as follows:\n";
cout << "1 | 2 | 3\n";
cout << "---------\n";
cout << "4 | 5 | 6\n";
cout << "---------\n";
cout << "7 | 8 | 9\n\n";
cout << "You are X, the AI is O. You go first!\n\n";
// Main game loop. It will iterate 9 times, once for each space on the board
for (int i = 0; i < 9; i++) {
printBoard(board); // Print the current state of the board
if(i % 2 == 0){ // If the iteration is even, it's the player's turn
choice = 'X';
cout << "Where do you want to place X? ";
cin >> placeCurrent;
placeCurrent--; // Adjust to 0-based index
// Check if the chosen space is already occupied or out of range
if (find(begin(place), end(place), placeCurrent) != end(place) || placeCurrent < 0 || placeCurrent > 8) {
cout << "Invalid move. Choose another position.\n";
i--; // Decrement the counter to retry this turn
continue; // Skip the rest of this iteration and start the next one
}
} else { // If the iteration is odd, it's the AI's turn
choice = 'O';
// Computer randomly chooses a location for 'O'
do {
placeCurrent = rand() % 9;
} while (find(begin(place), end(place), placeCurrent) != end(place));
cout << "AI is thinking...\n";
this_thread::sleep_for(chrono::seconds(1)); // AI "thinks" for 1 second
cout << "AI placed O at position " << placeCurrent + 1 << "\n\n";
}
place[i] = placeCurrent; // Store the chosen location in the place array
// Update the board with the new character
int row = placeCurrent / 3;
int col = placeCurrent % 3;
board[row][col] = choice;
// Check if the current player has won, but only after the 5th move
if (i >= 4 && checkWin(board, choice)) {
printBoard(board);
cout << "Congratulations! Player " << choice << " wins!\n";
return 0; // End the game
}
}
// If the game loop has ended without a winner, it's a draw
printBoard(board);
cout << "It's a draw!\n";
return 0;
}
2 Likes