-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic_Tac_Toe_game.cpp
133 lines (108 loc) · 3.05 KB
/
Tic_Tac_Toe_game.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
128
129
130
131
132
133
#include <iostream>
using namespace std;
void displayBoard();
bool checkWin();
bool checkDraw();
void switchPlayer();
void resetBoard();
char board[3][3] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} };
char currentPlayer = 'X';
int main() {
bool playAgain = true;
char choice;
do {
int move;
bool gameWon = false;
bool gameDraw = false;
resetBoard();
while (!gameWon && !gameDraw) {
displayBoard();
cout << "Player " << currentPlayer << ", enter your move (1-9): ";
cin >> move;
if (move >= 1 && move <= 9) {
int row = (move - 1) / 3;
int col = (move - 1) % 3;
if (board[row][col] != 'X' && board[row][col] != 'O') {
board[row][col] = currentPlayer;
gameWon = checkWin();
gameDraw = checkDraw();
if (!gameWon && !gameDraw) {
switchPlayer();
}
} else {
cout << "Invalid move, try again.\n";
}
} else {
cout << "Invalid input! Please enter a number between 1 and 9.\n";
}
}
displayBoard();
if (gameWon) {
cout << "Player " << currentPlayer << " wins the game!\n";
} else if (gameDraw) {
cout << "The game is a draw!\n";
}
cout << "Do you want to play again? (Y/N): ";
cin >> choice;
playAgain = (choice == 'Y' || choice == 'y');
} while (playAgain);
cout << "Thanks for playing!\n";
return 0;
}
void displayBoard() {
cout << "\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << " " << board[i][j] << " ";
if (j < 2) cout << "|";
}
cout << "\n";
if (i < 2) cout << "---+---+---\n";
}
cout << "\n";
}
bool checkWin() {
for (int i = 0; i < 3; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
return true;
}
}
for (int i = 0; i < 3; i++) {
if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
return true;
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
return true;
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
return true;
}
return false;
}
bool checkDraw() {
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;
}
void switchPlayer() {
if (currentPlayer == 'X') {
currentPlayer = 'O';
} else {
currentPlayer = 'X';
}
}
void resetBoard() {
char counter = '1';
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = counter++;
}
}
currentPlayer = 'X';
}