how to make tic tac toe game in c++
game in c++
this is my homework
#include <iostream>
using namespace std;
void reset();
void PrintTable();
void GetInput();
void PutXO(int i, char XO);
void CEndGame();
void ShowDraw();
void ChWin();
bool isFirstRun = true;
char table[3][3];
bool gameEnd = false;
bool IsXTurn = true;
int TotalDone = 0;
bool isDraw = false;
void main() {
system("color F2");
reset();
while (!gameEnd)
{
CEndGame();
PrintTable();
GetInput();
ChWin();
}
if(isDraw)
ShowDraw();
cout << endl << " Press any Key To Exit";
system("pause>null");
}
void reset() {
table[0][0] = '1';
table[0][1] = '2';
table[0][2] = '3';
table[1][0] = '4';
table[1][1] = '5';
table[1][2] = '6';
table[2][0] = '7';
table[2][1] = '8';
table[2][2] = '9';
}
void PrintTable() {
if (!isFirstRun)
{
system("cls");
}
isFirstRun = false;
cout << endl << "!!!! Tic Tac To Game !!!!" << endl;
cout << " +---+---+---+" << endl;
for (int i = 0; i < 3; i++)
{
cout << " | ";
for (int j = 0; j <3; j++)
{
cout << table[i][j] << " | ";
}
cout << endl << " +---+---+---+" << endl;
}
}
void GetInput() {
cout << endl;
int input;
if (IsXTurn) {
system("color F2");
cout << " X: ";
}
else
{
system("color F4");
cout << " O: ";
}
cin >> input;
if (IsXTurn) {
PutXO(input, 'X');
IsXTurn = !IsXTurn;
}
else
{
PutXO(input, 'O');
IsXTurn = !IsXTurn;
}
}
void PutXO(int i, char XO) {
switch (i)
{
case 1:
table[0][0] = XO;
break;
case 2:
table[0][1] = XO;
break;
case 3:
table[0][2] = XO;
break;
case 4:
table[1][0] = XO;
break;
case 5:
table[1][1] = XO;
break;
case 6:
table[1][2] = XO;
break;
case 7:
table[2][0] = XO;
break;
case 8:
table[2][1] = XO;
break;
case 9:
table[2][2] = XO;
break;
default:
break;
}
}
void CEndGame() {
TotalDone++;
if (TotalDone == 9)
{
gameEnd = !gameEnd;
isDraw = !isDraw;
}
}
void ShowDraw() {
cout << endl;
PrintTable();
cout << endl << " Draw!! "<< endl;
}
void ChWin() {
if ((table[0][0] == table[0][1] && table[0][1] == table[0][2]) && table[0][0] == 'X' ||
(table[1][0] == table[1][1] && table[1][1] == table[1][2]) && table[1][0] == 'X' ||
(table[2][0] == table[2][1] && table[2][1] == table[2][2]) && table[2][0] == 'X' ||
(table[0][0] == table[1][0] && table[1][0] == table[2][0]) && table[0][0] == 'X' ||
(table[0][1] == table[1][1] && table[1][1] == table[2][1]) && table[0][1] == 'X' ||
(table[0][2] == table[1][2] && table[1][2] == table[2][2]) && table[0][2] == 'X' ||
(table[0][0] == table[1][1] && table[1][1] == table[2][2]) && table[0][0] == 'X' ||
(table[0][2] == table[1][1] && table[1][1] == table[2][0]) && table[0][2] == 'X' )
{
cout << " X Win!!";
gameEnd = !gameEnd;
}
else if (
(table[0][0] == table[0][1] && table[0][1] == table[0][2]) && table[0][0] == 'O' ||
(table[1][0] == table[1][1] && table[1][1] == table[1][2]) && table[1][0] == 'O' ||
(table[2][0] == table[2][1] && table[2][1] == table[2][2]) && table[2][0] == 'O' ||
(table[0][0] == table[1][0] && table[1][0] == table[2][0]) && table[0][0] == 'O' ||
(table[0][1] == table[1][1] && table[1][1] == table[2][1]) && table[0][1] == 'O' ||
(table[0][2] == table[1][2] && table[1][2] == table[2][2]) && table[0][2] == 'O' ||
(table[0][0] == table[1][1] && table[1][1] == table[2][2]) && table[0][0] == 'O' ||
(table[0][2] == table[1][1] && table[1][1] == table[2][0]) && table[0][2] == 'O' )
{
cout << " O Win!!";
gameEnd = !gameEnd;
}
}
game in c++
this is my homework
#include <iostream>
using namespace std;
void reset();
void PrintTable();
void GetInput();
void PutXO(int i, char XO);
void CEndGame();
void ShowDraw();
void ChWin();
bool isFirstRun = true;
char table[3][3];
bool gameEnd = false;
bool IsXTurn = true;
int TotalDone = 0;
bool isDraw = false;
void main() {
system("color F2");
reset();
while (!gameEnd)
{
CEndGame();
PrintTable();
GetInput();
ChWin();
}
if(isDraw)
ShowDraw();
cout << endl << " Press any Key To Exit";
system("pause>null");
}
void reset() {
table[0][0] = '1';
table[0][1] = '2';
table[0][2] = '3';
table[1][0] = '4';
table[1][1] = '5';
table[1][2] = '6';
table[2][0] = '7';
table[2][1] = '8';
table[2][2] = '9';
}
void PrintTable() {
if (!isFirstRun)
{
system("cls");
}
isFirstRun = false;
cout << endl << "!!!! Tic Tac To Game !!!!" << endl;
cout << " +---+---+---+" << endl;
for (int i = 0; i < 3; i++)
{
cout << " | ";
for (int j = 0; j <3; j++)
{
cout << table[i][j] << " | ";
}
cout << endl << " +---+---+---+" << endl;
}
}
void GetInput() {
cout << endl;
int input;
if (IsXTurn) {
system("color F2");
cout << " X: ";
}
else
{
system("color F4");
cout << " O: ";
}
cin >> input;
if (IsXTurn) {
PutXO(input, 'X');
IsXTurn = !IsXTurn;
}
else
{
PutXO(input, 'O');
IsXTurn = !IsXTurn;
}
}
void PutXO(int i, char XO) {
switch (i)
{
case 1:
table[0][0] = XO;
break;
case 2:
table[0][1] = XO;
break;
case 3:
table[0][2] = XO;
break;
case 4:
table[1][0] = XO;
break;
case 5:
table[1][1] = XO;
break;
case 6:
table[1][2] = XO;
break;
case 7:
table[2][0] = XO;
break;
case 8:
table[2][1] = XO;
break;
case 9:
table[2][2] = XO;
break;
default:
break;
}
}
void CEndGame() {
TotalDone++;
if (TotalDone == 9)
{
gameEnd = !gameEnd;
isDraw = !isDraw;
}
}
void ShowDraw() {
cout << endl;
PrintTable();
cout << endl << " Draw!! "<< endl;
}
void ChWin() {
if ((table[0][0] == table[0][1] && table[0][1] == table[0][2]) && table[0][0] == 'X' ||
(table[1][0] == table[1][1] && table[1][1] == table[1][2]) && table[1][0] == 'X' ||
(table[2][0] == table[2][1] && table[2][1] == table[2][2]) && table[2][0] == 'X' ||
(table[0][0] == table[1][0] && table[1][0] == table[2][0]) && table[0][0] == 'X' ||
(table[0][1] == table[1][1] && table[1][1] == table[2][1]) && table[0][1] == 'X' ||
(table[0][2] == table[1][2] && table[1][2] == table[2][2]) && table[0][2] == 'X' ||
(table[0][0] == table[1][1] && table[1][1] == table[2][2]) && table[0][0] == 'X' ||
(table[0][2] == table[1][1] && table[1][1] == table[2][0]) && table[0][2] == 'X' )
{
cout << " X Win!!";
gameEnd = !gameEnd;
}
else if (
(table[0][0] == table[0][1] && table[0][1] == table[0][2]) && table[0][0] == 'O' ||
(table[1][0] == table[1][1] && table[1][1] == table[1][2]) && table[1][0] == 'O' ||
(table[2][0] == table[2][1] && table[2][1] == table[2][2]) && table[2][0] == 'O' ||
(table[0][0] == table[1][0] && table[1][0] == table[2][0]) && table[0][0] == 'O' ||
(table[0][1] == table[1][1] && table[1][1] == table[2][1]) && table[0][1] == 'O' ||
(table[0][2] == table[1][2] && table[1][2] == table[2][2]) && table[0][2] == 'O' ||
(table[0][0] == table[1][1] && table[1][1] == table[2][2]) && table[0][0] == 'O' ||
(table[0][2] == table[1][1] && table[1][1] == table[2][0]) && table[0][2] == 'O' )
{
cout << " O Win!!";
gameEnd = !gameEnd;
}
}
Sign up here with your email
ConversionConversion EmoticonEmoticon