Skip to content

Commit 727f4c8

Browse files
authored
Merge pull request #336 from hyprex-deva/main
Added Tic Tac Toe Game.
2 parents 63bd98e + d645a58 commit 727f4c8

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#include <iostream>
2+
using namespace std;
3+
int choice;
4+
bool draw = false;
5+
char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
6+
char turn = 'X';
7+
int row, column;
8+
9+
void Display_Board()
10+
{
11+
12+
system("cls");
13+
cout << endl
14+
<< endl;
15+
cout << "\t\t******************************************************************************************\n\n";
16+
cout << "\t\t ======= ======= ======= ======= == ======= ======= ====== =======" << endl;
17+
cout << "\t\t = = = = = = = = = = =" << endl;
18+
cout << "\t\t = = = = = = = = = = =" << endl;
19+
cout << "\t\t = = = = ======== = = = = =======" << endl;
20+
cout << "\t\t = = = = = = = = = = =" << endl;
21+
cout << "\t\t = = = = = = = = = = =" << endl;
22+
cout << "\t\t = ======= ======= = = = ======= = ====== =======" << endl;
23+
cout << "\n\n\t\t*******************************************************************************************";
24+
cout << "\n\n\t\t\t\tPLAYER1 [X]\n";
25+
cout << "\n\t\t\t\tPLAYER2 [O]\n\n\n";
26+
27+
cout << "\t\t\t | | " << endl;
28+
cout << "\t\t\t " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " \n";
29+
cout << "\t\t\t_____|_____|_____" << endl;
30+
cout << "\t\t\t | | " << endl;
31+
cout << "\t\t\t " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " \n";
32+
cout << "\t\t\t_____|_____|_____" << endl;
33+
cout << "\t\t\t | | " << endl;
34+
cout << "\t\t\t " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " \n";
35+
cout << "\t\t\t | | " << endl;
36+
}
37+
void Player_Turn()
38+
{
39+
if (turn == 'X')
40+
cout << "\n\tPLAYER1 [x] Turn : ";
41+
if (turn == 'O')
42+
cout << "\n\tPLAYER2 [O] Turn : ";
43+
cin >> choice;
44+
switch (choice)
45+
{
46+
case 1:
47+
row = 0;
48+
column = 0;
49+
break;
50+
case 2:
51+
row = 0;
52+
column = 1;
53+
break;
54+
case 3:
55+
row = 0;
56+
column = 2;
57+
break;
58+
case 4:
59+
row = 1;
60+
column = 0;
61+
break;
62+
case 5:
63+
row = 1;
64+
column = 1;
65+
break;
66+
case 6:
67+
row = 1;
68+
column = 2;
69+
break;
70+
case 7:
71+
row = 2;
72+
column = 0;
73+
break;
74+
case 8:
75+
row = 2;
76+
column = 1;
77+
break;
78+
case 9:
79+
row = 2;
80+
column = 2;
81+
break;
82+
default:
83+
cout << "\nInvalid choice\n ";
84+
break;
85+
}
86+
if (turn == 'X' && board[row][column] != 'X' && board[row][column] != 'O')
87+
{
88+
board[row][column] = 'X';
89+
turn = 'O';
90+
}
91+
else if (turn == 'O' && board[row][column] != 'X' && board[row][column] != 'O')
92+
{
93+
board[row][column] = 'O';
94+
turn = 'X';
95+
}
96+
else
97+
{
98+
cout << "BOX IS ALREADY FILL PLEASE TRY AGAIN ";
99+
Player_Turn();
100+
}
101+
Display_Board();
102+
}
103+
bool Game_Over()
104+
{
105+
// if check win
106+
107+
for (int i = 0; i < 3; i++)
108+
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])
109+
return false;
110+
111+
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])
112+
return false;
113+
114+
// if there is only box not filled
115+
for (int i = 0; i < 3; i++)
116+
for (int j = 0; j < 3; j++)
117+
if (board[i][j] != 'X' && board[i][j] != 'O')
118+
return true;
119+
120+
// draw
121+
draw = true;
122+
return false;
123+
}
124+
125+
int main()
126+
{
127+
char ch;
128+
do
129+
{
130+
while (Game_Over())
131+
{
132+
Display_Board();
133+
Player_Turn();
134+
Game_Over();
135+
}
136+
137+
if (turn == 'X' && draw == false)
138+
{
139+
cout << "PLAYER2 [O] WINS !! congratulations \n";
140+
}
141+
else if (turn == 'O' && draw == false)
142+
{
143+
cout << "PLAYER1 [x] WINS !! congratulations \n";
144+
}
145+
else
146+
{
147+
cout << "GAME DRAW !! \n";
148+
}
149+
cout << "\n"
150+
<< endl;
151+
//cout << "Want to Play Again (y/n) :" << endl;
152+
//cin >> ch;
153+
char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
154+
} while (ch=='y');
155+
cout<<"END\n";
156+
return 0;
157+
}

0 commit comments

Comments
 (0)