-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
170 lines (140 loc) · 4.55 KB
/
main.cpp
File metadata and controls
170 lines (140 loc) · 4.55 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
167
168
169
170
#include <iostream>
#include <iomanip>
using namespace std;
#include <iostream>
#include <iomanip>
using namespace std;
bool isDraw(char** board, int size) {
for (int row = 0; row < size; ++row) {
for (int col = 0; col < size; ++col) {
if (board[row][col] == ' ') {
return false;
}
}
}
return true;
}
bool checkWin(char** board, int size, char currentPlayer) {
// Check rows and columns
for (int i = 0; i < size; ++i) {
// Check row i
if (board[i][0] == currentPlayer && board[i][1] == currentPlayer && board[i][2] == currentPlayer) {
return true; // Current player has won
}
// Check column i
if (board[0][i] == currentPlayer && board[1][i] == currentPlayer && board[2][i] == currentPlayer) {
return true; // Current player has won
}
}
// Check main diagonal
if (board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer) {
return true; // Current player has won
}
// Check anti-diagonal
if (board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer) {
return true; // Current player has won
}
return false; // No win condition found
}
int getUserBoardSize() {
int boardSize;
cout << "What size board do you want to play? (0 to exit) ";
cin >> boardSize;
if (boardSize == 0) {
exit(0);
}
return boardSize;
}
void drawBoard(char** board, int size) {
cout << " ";
for (int col = 0; col < size; ++col) {
cout << setw(2) << col + 1;
if (col < size - 1) {
cout << " ";
}
}
cout << endl;
for (int row = 0; row < size; ++row) {
cout << row + 1 << " ";
for (int col = 0; col < size; ++col) {
cout << setw(2) << board[row][col];
if (col < size - 1) {
cout << " |";
}
}
cout << endl;
if (row < size - 1) {
cout << " ";
for (int col = 0; col < size; ++col) {
cout << "---";
if (col < size - 1) {
cout << "+";
}
}
cout << endl;
}
}
}
char** initializeBoard(int size) {
char** board = new char*[size]; // Create an array of pointers to char
for (int row = 0; row < size; ++row) {
board[row] = new char[size]; // Allocate heap memory for each row (array of char)
for (int col = 0; col < size; ++col) {
board[row][col] = ' '; // Initialize the value of each element to ' '
}
}
return board;
}
void deallocateBoard(char** board, int size) {
for (int row = 0; row < size; ++row) {
delete[] board[row];
}
delete[] board;
}
bool isValidMove(char** board, int size, int row, int col) {
if (row >= 1 && row <= size && col >=1 && col <= size) {
if (board[row - 1][col - 1] == ' ') {
return true;
}
}
return false;
}
int main() {
char** board;
int boardSize;
bool gameOver;
char currentPlayer = 'X';
do {
boardSize = getUserBoardSize();
board = initializeBoard(boardSize);
gameOver = false;
drawBoard(board, boardSize);
do {
int row, col;
cout << "Player " << currentPlayer << ", enter your move (Row): ";
cin >> row;
cout << "Player " << currentPlayer << ", enter your move (Column): ";
cin >> col;
if (isValidMove(board, boardSize, row, col)) {
board[row - 1][col - 1] = currentPlayer; // Assign player's move
if (checkWin(board, boardSize, currentPlayer)) {
drawBoard(board, boardSize); // Display the final winning board
cout << "Player " << currentPlayer << " wins!" << endl;
gameOver = true;
} else if (isDraw(board, boardSize)) {
drawBoard(board, boardSize); // Display the final drawn board
cout << "It's a draw!" << endl;
gameOver = true;
} else {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
drawBoard(board, boardSize);
} else {
cout << "Invalid move. Try again." << endl;
}
} while(!gameOver);
// When the game is over or the program exits
deallocateBoard(board, boardSize);
} while (true);
return 0;
}