-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToeGame.cpp
91 lines (73 loc) · 2.95 KB
/
TicTacToeGame.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
#include <iostream>
#include <vector>
using namespace std;
// Function to print the Tic-Tac-Toe board
void printBoard(const vector<vector<char>>& board) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << board[i][j];
if (j < 2) cout << " | ";
}
cout << endl;
if (i < 2) cout << "---------" << endl;
}
}
// Function to check if a player has won
bool checkWin(const vector<vector<char>>& board, char player) {
for (int i = 0; i < 3; ++i) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) return true;
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) return true;
}
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) return true;
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) return true;
return false;
}
// Function to check if the game is a draw
bool checkDraw(const vector<vector<char>>& board) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == ' ') return false;
}
}
return true;
}
int main() {
char opt;
do{
vector<vector<char>> board(3, vector<char>(3, ' '));
char currentPlayer = 'X';
bool gameWon = false;
bool gameDraw = false;
system("CLS");
cout << " ******* * ****** ******* ****** ****** ******* ****** ******" << endl;
cout << " * * * * * * * * * * *" <<endl;
cout << " * * * * ****** * * * * ******" <<endl;
cout << " * * * * * * * * * * *" <<endl;
cout << " * * ****** * * * ****** * ****** ******" <<endl;
while (!gameWon && !gameDraw) {
cout << "\n[+] CURRENT BOARD\n" << endl;
printBoard(board);
cout << "\n[+] PLAYER " << currentPlayer <<endl;
cout << " ENTER YOUR MOVE (ROW & COLUMN)";
int row, col;
cin >> row >> col;
if (row < 1 || row > 3 || col < 1 || col > 3 || board[row-1][col-1] != ' ') {
cout << "INVALID MOVE :(" << endl;
continue;
}
board[row-1][col-1] = currentPlayer;
if (checkWin(board, currentPlayer)) {
cout << "\n\n~~~~~~~~~ PLAYER " << currentPlayer << " WINS! ~~~~~~~~~" << endl;
gameWon = true;
} else if (checkDraw(board)) {
cout << "IT'S A DRAW :|" << endl;
gameDraw = true;
}
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
cout << "THANKS FOR PLAYING!" << endl;
cout << "\n[+] ANOTHER MATCH ??? (Y/N)" <<endl;
cin >> opt;
}while(opt == 'Y' || opt == 'y');
return 0;
}