-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticTacToe.cpp
More file actions
166 lines (147 loc) · 4.39 KB
/
ticTacToe.cpp
File metadata and controls
166 lines (147 loc) · 4.39 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class game{
public:
void start();
char board[3][3];
void initializeBoard();
void displayBoard();
char currentPlayer = 'X';
void makeMove(int choice);
int takeInput();
bool isValidMove(int choice);
void switchPlayer();
bool checkWin();
bool checkDraw();
void play();
bool againOrNot();
int scoreX = 0;
int scoreO = 0;
};
void game :: start(void){
cout << "\n";
cout << "***************************************" << "\n";
cout << "***** LET'S PLAY TIC TAC TOE *****" << "\n";
cout << "***************************************" << "\n \n";
};
void game :: initializeBoard(){
char val = '1';
for(int i = 0 ; i < 3 ; i++){
for(int j = 0 ; j < 3 ; j++){
board[i][j] = val;
val++;
}
}
}
void game :: displayBoard(){
cout <<"\n";
for(int i = 0; i < 3; i++){
cout << " ";
for(int j = 0; j < 3; j++){
cout << board[i][j];
if(j < 2) cout << " | ";
}
cout << "\n";
if(i < 2) cout << "--- --- ---\n";
}
cout <<"\n";
}
int game :: takeInput(){
int choice;
while(true){
cout << "PLAYER " << currentPlayer << " INPUT : ";
if(!(cin >> choice)){
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid input (not a number). Try again.\n";
continue;
}
if(choice > 0 && choice < 10 && isValidMove(choice)){
return choice;
}
cout << "Invalid move. Try again.\n";
}
}
bool game :: isValidMove(int choice){
int row = (choice - 1) / 3;
int col = (choice - 1) % 3;
if(board[row][col] == 'X' || board[row][col] == 'O') return false;
else return true;
}
void game :: makeMove(int choice){
int row = (choice - 1) / 3;
int col = (choice - 1) % 3;
board[row][col] = currentPlayer;
}
void game :: switchPlayer(void){
if(currentPlayer == 'X') currentPlayer = 'O';
else currentPlayer = 'X';
}
bool game :: checkWin(void){
if(board[0][0] == currentPlayer && board[1][0] == currentPlayer && board[2][0] == currentPlayer) return true;
if(board[0][1] == currentPlayer && board[1][1] == currentPlayer && board[2][1] == currentPlayer) return true;
if(board[0][2] == currentPlayer && board[1][2] == currentPlayer && board[2][2] == currentPlayer) return true;
if(board[0][0] == currentPlayer && board[0][1] == currentPlayer && board[0][2] == currentPlayer) return true;
if(board[1][0] == currentPlayer && board[1][1] == currentPlayer && board[1][2] == currentPlayer) return true;
if(board[2][0] == currentPlayer && board[2][1] == currentPlayer && board[2][2] == currentPlayer) return true;
if(board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer) return true;
if(board[2][0] == currentPlayer && board[1][1] == currentPlayer && board[0][2] == currentPlayer) return true;
else return false;
}
bool game :: checkDraw(void){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(board[i][j] != 'X' && board[i][j] != 'O'){
return false;
}
}
}
return true;
}
bool game :: againOrNot(){
char input;
while(true){
cout << "WANT A NEW GAME ?? (Y/N) : ";
cin >> input;
if(input == 'Y' || input == 'y') return true;
if(input == 'N' || input == 'n') return false;
cout << "Invalid input. Try again.\n";
}
}
void game :: play(){
initializeBoard();
while(true){
displayBoard();
int choice = takeInput();
makeMove(choice);
if(checkWin()){
displayBoard();
if(currentPlayer == 'X') scoreX++;
else scoreO++;
cout << "PLAYER " << currentPlayer << " WINS!\n";
cout << "Score = X : " << scoreX << " | O : " << scoreO << "\n";
break;
}
if(checkDraw()){
displayBoard();
cout << "GAME DRAW!\n";
cout << "Score = X : " << scoreX << " | O : " << scoreO << "\n";
break;
}
switchPlayer();
}
}
int main(){
game g;
g.start();
while(true){
g.play();
if(!g.againOrNot()){
cout << "Thanks for playing!\n";
break;
}
}
return 0;
}