OOP A3 Board Games 35
a Board Game Project Made by Students at Cairo FCAI
Loading...
Searching...
No Matches
BoardGame_Classes.h
Go to the documentation of this file.
1#ifndef _BOARDGAME_CLASSES_H
2#define _BOARDGAME_CLASSES_H
3
4#include <string>
5#include <vector>
6#include <iostream>
7#include <iomanip>
8using namespace std;
9
11// Forward declarations
13
14template <typename T> class Player;
15template <typename T> class Move;
16
18// Class declarations
20
30
39template <typename T>
40class Board {
41protected:
42 int rows;
43 int columns;
44 vector<vector<T>> board;
45 int n_moves = 0;
46
47public:
51 Board(int rows, int columns)
52 : rows(rows), columns(columns), board(rows, vector<T>(columns)) {
53 }
54
58 virtual ~Board() {}
59
65 virtual bool update_board(Move<T>* move) = 0;
66
68 virtual bool is_win(Player<T>*) = 0;
69
71 virtual bool is_lose(Player<T>*) = 0;
72
74 virtual bool is_draw(Player<T>*) = 0;
75
77 virtual bool game_is_over(Player<T>*) = 0;
78
82 vector<vector<T>> get_board_matrix() const {
83 return board;
84 }
85
87 int get_rows() const { return rows; }
88
90 int get_columns() const { return columns; }
91};
92
93//-----------------------------------------------------
99template <typename T>
100class Move {
101 int x;
102 int y;
103 T symbol;
104
105public:
107 Move(int x, int y, T symbol) : x(x), y(y), symbol(symbol) {}
108
110 int get_x() const { return x; }
111
113 int get_y() const { return y; }
114
116 T get_symbol() const { return symbol; }
117};
118
119//-----------------------------------------------------
125template <typename T>
126class Player {
127protected:
128 string name;
132
133public:
137 Player(string n, T s, PlayerType t)
138 : name(n), symbol(s), type(t), boardPtr(nullptr) {
139 }
140
141 virtual ~Player() {}
142
144 string get_name() const { return name; }
145
147 PlayerType get_type() const { return type; }
148
150 T get_symbol() const { return symbol; }
151
153 Board<T>* get_board_ptr() const { return boardPtr; }
154
157};
158
159//-----------------------------------------------------
165template <typename T>
166class UI {
167protected:
169
173 string get_player_name(string player_label) {
174 string name;
175 cout << "Enter " << player_label << " name: ";
176 getline(cin >> ws, name);
177 return name;
178 }
179
183 PlayerType get_player_type_choice(string player_label, const vector<string>& options) {
184 cout << "Choose " << player_label << " type:\n";
185 for (size_t i = 0; i < options.size(); ++i)
186 cout << i + 1 << ". " << options[i] << "\n";
187 int choice;
188 cin >> choice;
189 return (choice == 2) ? PlayerType::COMPUTER : PlayerType::HUMAN;
190 }
191
192public:
196 UI(string message, int cell_display_width)
197 : cell_width(cell_display_width) {
198 cout << message << endl;
199 }
200
201 virtual ~UI() {}
202
204 void display_message(string message) { cout << message << "\n"; }
205
209 virtual Move<T>* get_move(Player<T>*) = 0;
210
215
219 virtual Player<T>* create_player(string& name, T symbol, PlayerType type) = 0;
220
224 void display_board_matrix(const vector<vector<T>>& matrix) const {
225 if (matrix.empty() || matrix[0].empty()) return;
226
227 int rows = matrix.size();
228 int cols = matrix[0].size();
229
230 cout << "\n ";
231 for (int j = 0; j < cols; ++j)
232 cout << setw(cell_width + 1) << j;
233 cout << "\n " << string((cell_width + 2) * cols, '-') << "\n";
234
235 for (int i = 0; i < rows; ++i) {
236 cout << setw(2) << i << " |";
237 for (int j = 0; j < cols; ++j)
238 cout << setw(cell_width) << matrix[i][j] << " |";
239 cout << "\n " << string((cell_width + 2) * cols, '-') << "\n";
240 }
241 cout << endl;
242 }
243};
244
245//-----------------------------------------------------
251template <typename T>
253 Board<T>* boardPtr;
254 Player<T>* players[2];
255 UI<T>* ui;
256
257public:
262 : boardPtr(b), ui(u) {
263 players[0] = p[0];
264 players[1] = p[1];
265 players[0]->set_board_ptr(b);
266 players[1]->set_board_ptr(b);
267 }
268
272 void run() {
273 ui->display_board_matrix(boardPtr->get_board_matrix());
274 Player<T>* currentPlayer = players[0];
275
276 while (true) {
277 for (int i : {0, 1}) {
278 currentPlayer = players[i];
279 Move<T>* move = ui->get_move(currentPlayer);
280
281 while (!boardPtr->update_board(move))
282 move = ui->get_move(currentPlayer);
283
284 ui->display_board_matrix(boardPtr->get_board_matrix());
285
286 if (boardPtr->is_win(currentPlayer)) {
287 ui->display_message(currentPlayer->get_name() + " wins!");
288 return;
289 }
290 if (boardPtr->is_lose(currentPlayer)) {
291 ui->display_message(players[1 - i]->get_name() + " wins!");
292 return;
293 }
294 if (boardPtr->is_draw(currentPlayer)) {
295 ui->display_message("Draw!");
296 return;
297 }
298 }
299 }
300 }
301};
302
303//-----------------------------------------------------
307template <typename T>
309 Player<T>** players = new Player<T>*[2];
310 vector<string> type_options = { "Human", "Computer" };
311
312 string nameX = get_player_name("Player X");
313 PlayerType typeX = get_player_type_choice("Player X", type_options);
314 players[0] = create_player(nameX, static_cast<T>('X'), typeX);
315
316 string nameO = get_player_name("Player O");
317 PlayerType typeO = get_player_type_choice("Player O", type_options);
318 players[1] = create_player(nameO, static_cast<T>('O'), typeO);
319
320 return players;
321}
322
323#endif // _BOARDGAME_CLASSES_H#pragma once
PlayerType
Represents the type of player in the game.
Definition BoardGame_Classes.h:24
@ AI
An AI player.
Definition BoardGame_Classes.h:27
@ RANDOM
A Random player.
Definition BoardGame_Classes.h:28
@ HUMAN
A human player.
Definition BoardGame_Classes.h:25
@ COMPUTER
A computer-controlled player.
Definition BoardGame_Classes.h:26
Base template for any board used in board games.
Definition BoardGame_Classes.h:40
vector< vector< T > > get_board_matrix() const
Return a copy of the current board as a 2D vector.
Definition BoardGame_Classes.h:82
virtual ~Board()
Virtual destructor. Frees allocated board memory.
Definition BoardGame_Classes.h:58
virtual bool is_lose(Player< T > *)=0
Check if a player has lost.
int n_moves
Number of moves made.
Definition BoardGame_Classes.h:45
virtual bool is_draw(Player< T > *)=0
Check if the game ended in a draw.
int get_rows() const
Get number of rows.
Definition BoardGame_Classes.h:87
virtual bool update_board(Move< T > *move)=0
Update the board with a new move.
int rows
Number of rows.
Definition BoardGame_Classes.h:42
virtual bool is_win(Player< T > *)=0
Check if a player has won.
int get_columns() const
Get number of columns.
Definition BoardGame_Classes.h:90
virtual bool game_is_over(Player< T > *)=0
Check if the game is over.
Board(int rows, int columns)
Construct a board with given dimensions.
Definition BoardGame_Classes.h:51
vector< vector< T > > board
2D vector for the board
Definition BoardGame_Classes.h:44
int columns
Number of columns.
Definition BoardGame_Classes.h:43
void run()
Run the main game loop until someone wins or the game ends.
Definition BoardGame_Classes.h:272
GameManager(Board< T > *b, Player< T > *p[2], UI< T > *u)
Construct a game manager with board, players, and UI.
Definition BoardGame_Classes.h:261
Represents a single move in a board game.
Definition BoardGame_Classes.h:100
Move(int x, int y, T symbol)
Construct a move at (x, y) using a symbol.
Definition BoardGame_Classes.h:107
T get_symbol() const
Get the move symbol.
Definition BoardGame_Classes.h:116
int get_y() const
Get column index.
Definition BoardGame_Classes.h:113
int get_x() const
Get row index.
Definition BoardGame_Classes.h:110
Base template for all players (human or AI).
Definition BoardGame_Classes.h:126
Board< T > * boardPtr
Pointer to the game board.
Definition BoardGame_Classes.h:131
T symbol
Player’s symbol on board.
Definition BoardGame_Classes.h:130
PlayerType get_type() const
Get player type (e.g., 'H' or 'C').
Definition BoardGame_Classes.h:147
void set_board_ptr(Board< T > *b)
Assign the board pointer for the player.
Definition BoardGame_Classes.h:156
Board< T > * get_board_ptr() const
Get a pointer to the game board.
Definition BoardGame_Classes.h:153
PlayerType type
Player type (e.g., HUMAN or COMPUTER).
Definition BoardGame_Classes.h:129
virtual ~Player()
Definition BoardGame_Classes.h:141
Player(string n, T s, PlayerType t)
Construct a player with name, symbol, and type.
Definition BoardGame_Classes.h:137
string get_name() const
Get the player's name.
Definition BoardGame_Classes.h:144
string name
Player name.
Definition BoardGame_Classes.h:128
T get_symbol() const
Get the player's symbol.
Definition BoardGame_Classes.h:150
Base class for handling user interface and input/output.
Definition BoardGame_Classes.h:166
virtual ~UI()
Definition BoardGame_Classes.h:201
void display_message(string message)
Display any message to the user.
Definition BoardGame_Classes.h:204
void display_board_matrix(const vector< vector< T > > &matrix) const
Display the current board matrix in formatted form.
Definition BoardGame_Classes.h:224
virtual Player< T > ** setup_players()
Set up players for the game.
Definition BoardGame_Classes.h:308
int cell_width
Width of each displayed board cell.
Definition BoardGame_Classes.h:168
string get_player_name(string player_label)
Ask the user for the player's name.
Definition BoardGame_Classes.h:173
virtual Move< T > * get_move(Player< T > *)=0
Ask the user (or AI) to make a move.
UI(string message, int cell_display_width)
Construct the UI and display a welcome message.
Definition BoardGame_Classes.h:196
PlayerType get_player_type_choice(string player_label, const vector< string > &options)
Ask the user to choose the player type from a list.
Definition BoardGame_Classes.h:183
virtual Player< T > * create_player(string &name, T symbol, PlayerType type)=0
Create a player object based on input name, symbol, and type.