-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTic-Tac-Toe.cpp
127 lines (117 loc) · 3.93 KB
/
Tic-Tac-Toe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//CODE FOR PLAYING TIC-TOC-TOE GAME
// COPYRIGHT CONTENT BY: DEVANSH DUBEY
// BELOW CODE IS FOR HELP PURPOSE ONLY.
// RECOMMENDED TO CODE IT BY YOURSELF
// THANKS FOR YOUR VISIT :)
#include<bits/stdc++.h>
using namespace std;
static char board[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}}, ch1;
string p1, p2;
int r, c;
int alt = 0;
void boardReset(){
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
board[i][j] = ' ';
}
void printBoard(){
system("cls");
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(j == 0)
cout << "\t\t\t";
cout << " " << board[i][j] << " ";
if(j != 2)
cout << "|";
}
cout << "\n";
if(i != 2)
cout << "\t\t\t-----------\n";
}
}
bool isWinner(char ch4){
return ((board[0][0] == ch4 && board[0][1] == ch4 && board[0][2] == ch4) ||
(board[1][0] == ch4 && board[1][1] == ch4 && board[1][2] == ch4) ||
(board[2][0] == ch4 && board[2][1] == ch4 && board[2][2] == ch4) ||
(board[0][0] == ch4 && board[1][0] == ch4 && board[2][0] == ch4) ||
(board[0][1] == ch4 && board[1][1] == ch4 && board[2][1] == ch4) ||
(board[0][2] == ch4 && board[1][2] == ch4 && board[2][2] == ch4) ||
(board[0][0] == ch4 && board[1][1] == ch4 && board[2][2] == ch4) ||
(board[0][2] == ch4 && board[1][1] == ch4 && board[2][0] == ch4));
}
bool Boardfull(){
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
if(board[i][j] == ' ')
return false;
return true;
}
void makeMove(char c1){
if(board[r-1][c-1] == ' ')
board[r-1][c-1] = c1;
else{
cout << "\nThis Position is Already Occupied!\n\n";
alt--;
}
}
void playGame(){
do{
if(alt % 2 == 0){
cout << endl << "\n" << p1 << "'s Turn\n";
ch1 = 'X';
}
else{
cout << endl << "\n" << p2 << "'s Turn\n";
ch1 = 'O';
}
while(1){
cout << "Enter Column No. you want to make move: ";
cin >> c;
if(c < 1 || c > 3)
cout << "Enter Coulmn in Range 1 to 3\n";
else
break;
}
while(1){
cout << "Enter Row No. you want to make move: ";
cin >> r;
if(r < 1 || r > 3)
cout << "Enter Row in Range 1 to 3\n";
else
break;
}
makeMove(ch1);
alt++;
printBoard();
}while(!isWinner(ch1) && !Boardfull());
}
int main(){
char ch;
system("cls");
do{
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "\t\t\tWELCOME TO THE TIC-TAC-TOE GAME\n";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "\nEnter First Player's first Name: ";
cin >> p1;
cout << "Symbol Assigned to " << p1 << " is 'X'\n";
cout << "\nEnter Second Player's first Name: ";
cin >> p2;
cout << "Symbol Assigned to " << p2 << " is 'O'\n\n";
printBoard();
playGame();
cout << "\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "\t\t\t\tRESULT: ";
if(Boardfull() && !(isWinner('X') || isWinner('O')))
cout << "Game Tie!\n";
else if(alt % 2 == 0)
cout << p2 << " Wins!\n";
else
cout << p1 << " Wins!\n";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
cout << "\nDo You Want to Play Again? (Y / N): ";
boardReset();
alt = 0;
cin >> ch;
}while(ch != 'n' && ch != 'N');
}