-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.cpp
More file actions
113 lines (113 loc) · 3.33 KB
/
tictactoe.cpp
File metadata and controls
113 lines (113 loc) · 3.33 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
#include <bits/stdc++.h>
using namespace std;
void printBoard(char board[3][3])
{
cout << "Current Board:\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";
}
}
bool checkWin(char board[3][3], char player)
{
// Check basic row column win condition
for (int i = 0; i < 3; ++i) {
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) ||
(board[0][i] == player && board[1][i] == player && board[2][i] == player)) {
return true;
}
}
// Check diagonals win condition
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return true;
}
return false;
}
bool checkDraw(char board[3][3])
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (board[i][j] == ' ')
{
return false;
}
}
}
return true;
}
//switching of players
void switchPlayer(char &player)
{
player = (player == 'X') ? 'O' : 'X';
}
int main()
{
while (true) {
// make the board ready
char board[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
char currentPlayer = 'X';
char winner = ' ';
while (true)
{
printBoard(board);
int row, col;
cout << "Player " << currentPlayer << ", enter your move (row and column: 1, 2, 3) or enter -1 to quit: ";
//Players can enter -1 at any time during their turn to exit the game immediately
cin >> row;
if (row == -1)
{
cout << "Game exited by player " << currentPlayer << "\n";
return 0;
}
cin >> col;
if (col == -1)
{
cout << "Game exited by player " << currentPlayer << "\n";
return 0;
}
// IF USER GIVES WRONG INPUT SHOW INVALID
if (cin.fail() || row < 1 || row > 3 || col < 1 || col > 3 || board[row-1][col-1] != ' ')
{
cin.clear(); // clear the error flag
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // discard invalid input
cout << "Invalid move. Try again.\n";
continue;
}
board[row-1][col-1] = currentPlayer;
if (checkWin(board, currentPlayer))
{
winner = currentPlayer;
break;
}
if (checkDraw(board))
{
break;
}
switchPlayer(currentPlayer);
}
printBoard(board);
if (winner != ' ')
{
cout << "Player " << winner << " wins!\n";
}
else
{
cout << "It's a draw!\n";
}
char playAgain;
cout << "Do you want to play again? (y/n): ";
cin >> playAgain;
if (playAgain != 'y')
{
break;
}
}
return 0;
}