-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.txt
154 lines (139 loc) · 4.28 KB
/
TicTacToe.txt
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class TicTacToe {
private:
char** board;
int size;
void initBoard() {
board = new char* [size];
for (int i = 0; i < size; ++i) {
board[i] = new char[size];
for (int j = 0; j < size; ++j) {
board[i][j] = '.';
}
}
}
public:
// Default Constructor
TicTacToe() : size(3) {
initBoard();
}
// Parametrized Constructor
TicTacToe(int n) : size(n) {
initBoard();
}
// Copy Constructor
TicTacToe(const TicTacToe& other) : size(other.size) {
initBoard();
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
board[i][j] = other.board[i][j];
}
}
}
// Destructor
~TicTacToe() {
for (int i = 0; i < size; ++i) {
delete[] board[i];
}
delete[] board;
}
// Print the current board
friend ostream& operator<<(ostream& os, const TicTacToe& game) {
for (int i = 0; i < game.size; ++i) {
for (int j = 0; j < game.size; ++j) {
os << game.board[i][j] << " ";
}
os << endl;
}
return os;
}
// Get a move from the user
bool makeMove(int row, int col, char player) {
int x;
return x = (row >= 0 && row < size && col >= 0 && col < size && board[row][col] == '.') ? (board[row][col] = player, true) : false;
}
// Check if the move is valid
bool isValidMove(int row, int col) {
return row >= 0 && row < size && col >= 0 && col < size && board[row][col] == '.';
}
// Determine if there is a winner
bool isWinner(char player) {
// Check rows, columns and diagonals
int diag1 = 0, diag2 = 0;
for (int i = 0; i < size; ++i) {
int row = 0, col = 0;
for (int j = 0; j < size; ++j) {
if (board[i][j] == player) row++;
if (board[j][i] == player) col++;
}
if (row == size || col == size) return true;
if (board[i][i] == player) diag1++;
if (board[i][size - 1 - i] == player) diag2++;
}
return diag1 == size || diag2 == size;
}
// Save game to file
void saveGame(const string& filename) {
ofstream file(filename);
file << size << endl;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
file << board[i][j];
}
file << endl;
}
file.close();
}
// Load game from file
bool loadGame(const string& filename) {
ifstream file(filename);
if (!file.is_open()) return false;
file >> size;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
file >> board[i][j];
}
}
file.close();
return true;
}
};
int main() {
int n;
cout << "Enter the size of the board (N >= 3): ";
cin >> n;
TicTacToe game(n);
cout << "Starting a new Tic-Tac-Toe game with a " << n << "x" << n << " board.\n";
int playerTurn = 0; // 0 for 'X', 1 for 'O'
int moves = 0;
int maxMoves = n * n;
while (true) {
cout << game;
int row, col;
cout << "Player " << (playerTurn == 0 ? 'X' : 'O') << ", enter row and column to place your mark: ";
cin >> row >> col;
if (game.isValidMove(row - 1, col - 1)) {
game.makeMove(row - 1, col - 1, (playerTurn == 0 ? 'X' : 'O'));
moves++;
if (game.isWinner(playerTurn == 0 ? 'X' : 'O')) {
cout << "Player " << (playerTurn == 0 ? 'X' : 'O') << " wins!\n";
cout << game;
break;
}
if (moves == maxMoves) {
cout << "The game is a draw.\n";
cout << game;
break;
}
playerTurn = 1 - playerTurn; // Switch players
}
else {
cout << "Invalid move, please try again.\n";
}
}
return 0;
}