Global Variables and Includes
C++
#include <iostream>
using namespace std;
• This includes the iostream library which is required for
input and output operations in C++.
• The using namespace std; line allows us to use the std
namespace without prefixing the standard library functions
and objects with std:: .
C++
char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
• This initializes the Tic-Tac-Toe board as a 3x3 array of
characters, with each cell labeled from '1' to '9'.
C++
char turn = 'X';
• This variable keeps track of whose turn it is, starting with
player 'X'.
C++
int row, column;
• These variables store the row and column indices of the
cell where the current player wants to place their mark.
C++
bool draw = false;
• This boolean variable tracks whether the game is a
draw.
Function Definitions
Display Board Function
C++
void display_board(){
system("cls");
cout << "\n\n Tick Cross Game" << endl;
cout << "\tplayer1 [X] \n\tplayer2 [O]\n\n";
cout << "\t\t | | \n";
cout << "\t\t " << board[0][0] << " | " << board[0][1]
<< " | " << board[0][2] << " \n";
cout << "\t\t_____|_____|_____\n";
cout << "\t\t | | \n";
cout << "\t\t " << board[1][0] << " | " << board[1][1]
<< " | " << board[1][2] << " \n";
cout << "\t\t_____|_____|_____\n";
cout << "\t\t | | \n";
cout << "\t\t " << board[2][0] << " | " << board[2][1]
<< " | " << board[2][2] << " \n";
cout << "\t\t | | \n";
}
• The display_board function clears the console screen
(using system("cls"), which is platform-specific) and then
prints out the current state of the Tic-Tac-Toe board.
• It uses cout to display the board with labels for player1
and player2.
Player Turn Function
C++
void player_turn(){
int choice;
if(turn == 'X')
cout << "\n\tplayer1 [X] turn:";
else if(turn == 'O')
cout << "\n\tplayer2 [O] turn:";
cin >> choice;
switch(choice){
case 1: row = 0; column = 0; break;
case 2: row = 0; column = 1; break;
case 3: row = 0; column = 2; break;
case 4: row = 1; column = 0; break;
case 5: row = 1; column = 1; break;
case 6: row = 1; column = 2; break;
case 7: row = 2; column = 0; break;
case 8: row = 2; column = 1; break;
case 9: row = 2; column = 2; break;
default:
cout << "Invalid choice\n";
player_turn();
return;
}
if(turn == 'X' && board[row][column] != 'X' && board[row]
[column] != 'O'){
board[row][column] = 'X';
turn = 'O';
} else if(turn == 'O' && board[row][column] != 'X' &&
board[row][column] != 'O'){
board[row][column] = 'O';
turn = 'X';
} else {
cout << "Box already filled!\n Please try again!!\n\n";
player_turn();
}
display_board();
}
• The player_turn function handles the player's move.
• It prompts the current player to enter their move.
• It maps the player's numeric choice to the corresponding
row and column in the board array using a switch statement.
• It checks if the chosen cell is already occupied. If not, it
updates the board with the player's mark ('X' or 'O') and
switches the turn to the other player. If the cell is occupied, it
asks the player to choose again.
• Finally, it calls display_board to show the updated board.
Game Over Function
C++
bool gameover(){
// check win
for(int i = 0; i < 3; i++)
if((board[i][0] == board[i][1] && board[i][0] == board[i]
[2]) ||
(board[0][i] == board[1][i] && board[0][i] == board[2]
[i]))
return false;
if((board[0][0] == board[1][1] && board[0][0] == board[2]
[2]) ||
(board[0][2] == board[1][1] && board[0][2] == board[2]
[0]))
return false;
// check if any box is not filled
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
if(board[i][j] != 'X' && board[i][j] != 'O')
return true;
// draw
draw = true;
return false;
}
• The gameover function checks if the game is over.
• It first checks for a win condition by evaluating if any
row, column, or diagonal has the same mark ('X' or 'O'). If a
win is detected, it returns false.
• If no win is found, it checks if any cell is still empty (not
marked with 'X' or 'O'). If so, it returns true, meaning the
game continues.
• If all cells are filled and no win is detected, it sets draw
to true and returns false.
Main Function
C++
int main(){
while(gameover()){
display_board();
player_turn();
gameover();
}
if(turn == 'X' && draw == false)
cout << "player2[O] Wins!! Congratulations\n";
else if(turn == 'O' && draw == false)
cout << "player1[X] Wins!! Congratulations\n";
else
cout << "Game Draw!!\n";
return 0;
}
• The main function contains the main game loop.
• It repeatedly calls display_board, player_turn, and
gameover as long as gameover returns true (indicating the
game is not over).
• After the loop ends, it checks the turn and draw
variables to determine the outcome of the game and prints
the appropriate message.
Summary
• The game initializes a 3x3 board and alternates turns
between two players.
• Players input their moves, and the game updates the
board accordingly.
• The game checks for win conditions or a draw after each
move.
• The game loop continues until a player wins or the game
ends in a draw, displaying the final result.