TicTacToe Game Using Arrays in C++.
All three parts are defined in a code in a comment so first check all these parts of the Tic Tac Toe Game in C++. Now discuss the part by the part code below is a part first explanation.
1. Tic Tac Toe Game Chart Draw
First design a game chart, C++ it is difficult to design graphics on a console screen which is why I choose another option for drawing a Tic Tac Toe Game Environment via the help of COUT(in small) and trying to manage each chart look good when we enter the values on it.
2. Changing the Value of the Game Chart
Before changing the value of the game chart first, we have to put some value in the chart. We can say in a matrix. So I took a string matrix and put some values between 0 to 9 or 1 to 9 and also divide the matrix in 3 * 3 in Tic Tac Toe Game in C++ Chart Draw in the first part. After that when both users enter their names our program clears the first screen and displays the current screen.
- Tic Tac Toe Game Chart draw.
- Changing the Value of the Game Chart.
- Tic Tac Toe Check Win.
All three parts are defined in a code in a comment so first check all these parts of the Tic Tac Toe Game in C++. Now discuss the part by the part code below is a part first explanation.
The standard game is so fun and addictive that millions of people play it for hours around the world, especially the offline version. You can play card games free, Trust us, you will also don't stop until mastering all the MS solitaire online levels and trying all the game modes.
1. Tic Tac Toe Game Chart Draw
First design a game chart, C++ it is difficult to design graphics on a console screen which is why I choose another option for drawing a Tic Tac Toe Game Environment via the help of COUT(in small) and trying to manage each chart look good when we enter the values on it.
2. Changing the Value of the Game Chart
Before changing the value of the game chart first, we have to put some value in the chart. We can say in a matrix. So I took a string matrix and put some values between 0 to 9 or 1 to 9 and also divide the matrix in 3 * 3 in Tic Tac Toe Game in C++ Chart Draw in the first part. After that when both users enter their names our program clears the first screen and displays the current screen.
Now I ask both Gamers to enter the number (a matrix shows 1 to 9 where you want to draw your turn) so if a Player enters the values between 1 to 9 we change the value of that Player in a Tic Tac Toe Game Chart. The Same process is repeated again and again and alternatives for each Player one by one.
3. Tic Tac Toe Check Win
Tic Tac Toe Game is a very famous game in this game everyone wants to win so with each and every input of the user we check the winning condition if our condition is satisfied then it will print the message according to the Game that either the First player win or second Player win or Game is draw No one wins Both Are Skilled Gamer.
3. Tic Tac Toe Check Win
Tic Tac Toe Game is a very famous game in this game everyone wants to win so with each and every input of the user we check the winning condition if our condition is satisfied then it will print the message according to the Game that either the First player win or second Player win or Game is draw No one wins Both Are Skilled Gamer.
Tic Tac Toe Game in C++ Project Report PDF
#include <iostream>
using namespace std;
char CharMatrixDraw[10] = { '0', '1', '2',
'3', '4', '5',
'6', '7', '8', '9' };
int winner();
void GameChart(string, string);
/*Tic Tac Toe Game in C++ Project Report PDF
This program is divided into 3 Parts read a Full Article for understanding the full code
1. Tic Tac Toe Game Chart draw.
2. Changing the Value of Tic Tac Toe Game Chart.
3. Check Win.
*/
int main()
{
int Gamer = 1, i, choice;
string name1;
string name2;
cout << "Enter First Gamer Name: ";
cin >> name1;
cout << "\nEnter Second Gamer Name: ";
cin >> name2;
char mark;
do {
GameChart(name1, name2);
Gamer = (Gamer % 2) ? 1 : 2;
if (Gamer == 1)
{
cout << name1 << " Your Turn, Enter a Number: ";
cin >> choice;
}
else
{
cout << name2 << " Your Turn, Enter a Number: ";
cin >> choice;
}
/*Part 2 Start Here*/
mark = (Gamer == 1) ? 'X' : '0';
if (choice == 1 && CharMatrixDraw[1] == '1')
CharMatrixDraw[1] = mark;
else if (choice == 2 && CharMatrixDraw[2] == '2')
CharMatrixDraw[2] = mark;
else if (choice == 3 && CharMatrixDraw[3] == '3')
CharMatrixDraw[3] = mark;
else if (choice == 4 && CharMatrixDraw[4] == '4')
CharMatrixDraw[4] = mark;
else if (choice == 5 && CharMatrixDraw[5] == '5')
CharMatrixDraw[5] = mark;
else if (choice == 6 && CharMatrixDraw[6] == '6')
CharMatrixDraw[6] = mark;
else if (choice == 7 && CharMatrixDraw[7] == '7')
CharMatrixDraw[7] = mark;
else if (choice == 8 && CharMatrixDraw[8] == '8')
CharMatrixDraw[8] = mark;
else if (choice == 9 && CharMatrixDraw[9] == '9')
CharMatrixDraw[9] = mark;
else
{
cout << "\nInvalid Choice Try Again ";
Gamer--;
cin.ignore();
cin.get();
}
i = winner();
Gamer++;
} while (i == -1);
GameChart(name1, name2);
if (i == 1)
{
cout << "\n=============================\n";
cout << "\a" << name1 << " Is A Winner \n";
cout << "=============================\n";
}
else
{
cout << "\n=============================\n";
cout << "\aGame Is Draw\n";
cout << "=============================\n";
}
cin.ignore();
cin.get();
return 0;
}
/*Part 2 ends Here*/
/*Part 3 Start Here*/
int winner()
{
if (CharMatrixDraw[1] == CharMatrixDraw[2] && CharMatrixDraw[2] == CharMatrixDraw[3])
return 1;
else if (CharMatrixDraw[4] == CharMatrixDraw[5] && CharMatrixDraw[5] == CharMatrixDraw[6])
return 1;
else if (CharMatrixDraw[7] == CharMatrixDraw[8] && CharMatrixDraw[8] == CharMatrixDraw[9])
return 1;
else if (CharMatrixDraw[1] == CharMatrixDraw[4] && CharMatrixDraw[4] == CharMatrixDraw[7])
return 1;
else if (CharMatrixDraw[2] == CharMatrixDraw[5] && CharMatrixDraw[5] == CharMatrixDraw[8])
return 1;
else if (CharMatrixDraw[3] == CharMatrixDraw[6] && CharMatrixDraw[6] == CharMatrixDraw[9])
return 1;
else if (CharMatrixDraw[1] == CharMatrixDraw[5] && CharMatrixDraw[5] == CharMatrixDraw[9])
return 1;
else if (CharMatrixDraw[3] == CharMatrixDraw[5] && CharMatrixDraw[5] == CharMatrixDraw[7])
return 1;
else if (CharMatrixDraw[1] != '1' && CharMatrixDraw[2] != '2' && CharMatrixDraw[3] != '3' &&
CharMatrixDraw[4] != '4' && CharMatrixDraw[5] != '5' && CharMatrixDraw[6] != '6' &&
CharMatrixDraw[7] != '7' && CharMatrixDraw[8] != '8' && CharMatrixDraw[9] != '9')
return 0;
else
return -1;
}
/*Part 3 ends Here*/
/*Part 1 Start Here*/
void GameChart(string name1, string name2)
{
system("cls");
cout << "\n==========================";
cout << "\n\tTic Tac Toe\n";
cout << "\n==========================\n";
string nam1 = name1;
string nam2 = name2;
cout << nam1 << "(X)" << "========" << nam2 << " (0)\n\n";
cout << " || || " << endl;
cout << " " << CharMatrixDraw[1] << " || " << CharMatrixDraw[2] << " || " << CharMatrixDraw[3] << endl;
cout << "=====||=====||=====" << endl;
cout << " || || " << endl;
cout << " " << CharMatrixDraw[4] << " || " << CharMatrixDraw[5] << " || " << CharMatrixDraw[6] << endl;
cout << "=====||=====||=====" << endl;
cout << " || || " << endl;
cout << " " << CharMatrixDraw[7] << " || " << CharMatrixDraw[8] << " || " << CharMatrixDraw[9] << endl;
cout << " || || " << endl << endl;
}
0 Comments: