Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to understand this C++ code ? (it\'s a tic tac toe game ) #include <iostream

ID: 3719012 • Letter: H

Question

How to understand this C++ code ? (it's a tic tac toe game )

#include <iostream>

using namespace std;

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

char player = 'X';

void Draw()

{

system("cls");

cout << "Tic Tac Toe v1.0" << endl;

for (int i = 0; i < 3; i++)

{

for (int j = 0; j < 3; j++)

{

cout << matrix[i][j] << " ";

}

cout << endl;

}

}

void Input()

{

int a;

cout << "Press the number of the field: ";

cin >> a;

if (a == 1)

matrix[0][0] = player;

else if (a == 2)

matrix[0][1] = player;

else if (a == 3)

matrix[0][2] = player;

else if (a == 4)

matrix[1][0] = player;

else if (a == 5)

matrix[1][1] = player;

else if (a == 6)

matrix[1][2] = player;

else if (a == 7)

matrix[2][0] = player;

else if (a == 8)

matrix[2][1] = player;

else if (a == 9)

matrix[2][2] = player;

}

void TogglePlayer()

{

if (player == 'X')

player = 'O';

else

player = 'X';

}

char Win()

{

//first player

if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X')

return 'X';

if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X')

return 'X';

if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X')

return 'X';

if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')

return 'X';

if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')

return 'X';

if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')

return 'X';

if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')

return 'X';

if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')

return 'X';

//second player

if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')

return 'O';

if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')

return 'O';

if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')

return 'O';

if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')

return 'O';

if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')

return 'O';

if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')

return 'O';

if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')

return 'O';

if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')

return 'O';

return '/';

}

int main()

{

Draw();

while (1)

{

Input();

Draw();

if (Win() == 'X')

{

cout << "X wins!" << endl;

break;

}

else if (Win() == 'O')

{

cout << "O wins!" << endl;

break;

}

TogglePlayer();

}

system("pause");

return 0;

}

Explanation / Answer

Okay! So i have inserted the comments in the program to help you understand. If anything else remains doubtful, feel free to ask in the comments.

#include <iostream>

using namespace std;

char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' }; // This 3X3 array is our tic tac board with initially numbers written on the board. These numbers will be replaced by "X" or "O" as the game progresses.

char player = 'X'; // This is the global variable which will either have the value as "X" or "O" depending on whose chance it is to play now. Since "X" starts the game, the variable is initially "X".

void Draw() // Function to display our tic-tac board

{

system("cls");

cout << "Tic Tac Toe v1.0" << endl;

for (int i = 0; i < 3; i++) // The loop just displays the contents of the board. Simple for loops used

{

for (int j = 0; j < 3; j++)

{

cout << matrix[i][j] << " ";

}

cout << endl;

}

}

void Input() // This function takes in the input from the player

{

int a;

cout << "Press the number of the field: ";

cin >> a; // The variable 'a' stores the number on the board where the player needs to put his "X" or "O"

// Now we put the value of variable player(X orO) on the number on the board which the user has just given us

if (a == 1)   

matrix[0][0] = player;

else if (a == 2)

matrix[0][1] = player;

else if (a == 3)

matrix[0][2] = player;

else if (a == 4)

matrix[1][0] = player;

else if (a == 5)

matrix[1][1] = player;

else if (a == 6)

matrix[1][2] = player;

else if (a == 7)

matrix[2][0] = player;

else if (a == 8)

matrix[2][1] = player;

else if (a == 9)

matrix[2][2] = player;

}

void TogglePlayer() // This function is used to switch player

{

// If the last turn was of X , the variable changes to O . Or else it the other way round

if (player == 'X')

player = 'O';

else

player = 'X';

}

char Win() // Function to check if the game is over and if someone has won already

{

//first player (Checks whether first player has won)

if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X') // Check whether the first row is completely filled with X's. If yes then return X indicating the first player has won

return 'X';

if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X') // Check whether the second row is completely filled with X's. If yes then return X indicating the first player has won

return 'X';

if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X') // Check whether the third row is completely filled with X's. If yes then return X indicating the first player has won

return 'X';

if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X') // Check whether the first column is completely filled with X's. If yes then return X indicating the first player has won

return 'X';

if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X') //Check whether the second column is completely filled with X's. If yes then return X indicating the first player has won

return 'X';

if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X') // Check whether the third column is completely filled with X's. If yes then return X indicating the first player has won

return 'X';

if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X') // Check whether the first diagonal ( from top left to bottom right) is completely filled with X's. If yes then return X indicating the first player has won

return 'X';

if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X') // Check whether the second diagonal (from top right to bottom left) is completely filled with X's. If yes then return X indicating the first player has won

return 'X';

// Mow whatever we did to check for winning of player one , we do the same for player two. Just instead of checking whether the complete row or column or diagonal is filled with 3 'X''s , we check for 3 "O''s.

//second player

if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')

return 'O';

if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')

return 'O';

if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')

return 'O';

if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')

return 'O';

if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')

return 'O';

if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')

return 'O';

if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')

return 'O';

if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')

return 'O';

return '/';

}

int main() // The main function. This is from where the execution starts

{

Draw(); // The program starts off with displaying the playing board. Initially it will only contain numbers 1-9 written on it

while (1)

{

Input(); // Take the input from the player

Draw(); // Display the result of what move the player made

if (Win() == 'X') // check if the first player wins

{

cout << "X wins!" << endl;

break;

}

else if (Win() == 'O') // Checks if the second player has won

{

cout << "O wins!" << endl;

break;

}

TogglePlayer(); // The control comes here if none of the players have won already. So, we toggle the players and the next player gets the chance to make a move instead of the one which already made a move.

}

system("pause");

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote