| 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>
8 using namespace std;
9
11// Forward declarations
13
14 template <typename T> class Player;
15 template <typename T> class Move;
16
18// Class declarations
20
24 enum class PlayerType {
25 HUMAN,
26 COMPUTER,
27 AI,
28 RANDOM
29};
30
39 template <typename T>
40 class Board {
41 protected:
42 int rows;
43 int columns;
44 vector<vector<T>> board;
45 int n_moves = 0;
46
47 public:
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//-----------------------------------------------------
99 template <typename T>
100 class Move {
101 int x;
102 int y;
103 T symbol;
104
105 public:
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//-----------------------------------------------------
125 template <typename T>
126 class Player {
127 protected:
128 string name;
129 PlayerType type;
130 T symbol;
131 Board<T>* boardPtr;
132
133 public:
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
156 void set_board_ptr(Board<T>* b) { boardPtr = b; }
157};
158
159//-----------------------------------------------------
165 template <typename T>
166 class UI {
167 protected:
168 int cell_width;
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
192 public:
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
214 virtual Player<T>** setup_players();
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//-----------------------------------------------------
251 template <typename T>
252 class GameManager {
253 Board<T>* boardPtr;
254 Player<T>* players[2];
255 UI<T>* ui;
256
257 public:
261 GameManager(Board<T>* b, Player<T>* p[2], UI<T>* u)
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//-----------------------------------------------------
307 template <typename T>
308 Player<T>** UI<T>::setup_players() {
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
PlayerType
Represents the type of player in the game.
Definition BoardGame_Classes.h:24
PlayerType::AI
@ AI
An AI player.
Definition BoardGame_Classes.h:27
PlayerType::RANDOM
@ RANDOM
A Random player.
Definition BoardGame_Classes.h:28
PlayerType::HUMAN
@ HUMAN
A human player.
Definition BoardGame_Classes.h:25
PlayerType::COMPUTER
@ COMPUTER
A computer-controlled player.
Definition BoardGame_Classes.h:26
Board
Base template for any board used in board games.
Definition BoardGame_Classes.h:40
Board::get_board_matrix
vector< vector< T > > get_board_matrix() const
Return a copy of the current board as a 2D vector.
Definition BoardGame_Classes.h:82
Board::~Board
virtual ~Board()
Virtual destructor. Frees allocated board memory.
Definition BoardGame_Classes.h:58
Board::is_lose
virtual bool is_lose(Player< T > *)=0
Check if a player has lost.
Board::n_moves
int n_moves
Number of moves made.
Definition BoardGame_Classes.h:45
Board::is_draw
virtual bool is_draw(Player< T > *)=0
Check if the game ended in a draw.
Board::get_rows
int get_rows() const
Get number of rows.
Definition BoardGame_Classes.h:87
Board::update_board
virtual bool update_board(Move< T > *move)=0
Update the board with a new move.
Board::rows
int rows
Number of rows.
Definition BoardGame_Classes.h:42
Board::is_win
virtual bool is_win(Player< T > *)=0
Check if a player has won.
Board::get_columns
int get_columns() const
Get number of columns.
Definition BoardGame_Classes.h:90
Board::game_is_over
virtual bool game_is_over(Player< T > *)=0
Check if the game is over.
Board::Board
Board(int rows, int columns)
Construct a board with given dimensions.
Definition BoardGame_Classes.h:51
Board::board
vector< vector< T > > board
2D vector for the board
Definition BoardGame_Classes.h:44
Board::columns
int columns
Number of columns.
Definition BoardGame_Classes.h:43
GameManager::run
void run()
Run the main game loop until someone wins or the game ends.
Definition BoardGame_Classes.h:272
GameManager::GameManager
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
Move
Represents a single move in a board game.
Definition BoardGame_Classes.h:100
Move::Move
Move(int x, int y, T symbol)
Construct a move at (x, y) using a symbol.
Definition BoardGame_Classes.h:107
Move::get_symbol
T get_symbol() const
Get the move symbol.
Definition BoardGame_Classes.h:116
Move::get_y
int get_y() const
Get column index.
Definition BoardGame_Classes.h:113
Move::get_x
int get_x() const
Get row index.
Definition BoardGame_Classes.h:110
Player
Base template for all players (human or AI).
Definition BoardGame_Classes.h:126
Player::boardPtr
Board< T > * boardPtr
Pointer to the game board.
Definition BoardGame_Classes.h:131
Player::symbol
T symbol
Player’s symbol on board.
Definition BoardGame_Classes.h:130
Player::get_type
PlayerType get_type() const
Get player type (e.g., 'H' or 'C').
Definition BoardGame_Classes.h:147
Player::set_board_ptr
void set_board_ptr(Board< T > *b)
Assign the board pointer for the player.
Definition BoardGame_Classes.h:156
Player::get_board_ptr
Board< T > * get_board_ptr() const
Get a pointer to the game board.
Definition BoardGame_Classes.h:153
Player::type
PlayerType type
Player type (e.g., HUMAN or COMPUTER).
Definition BoardGame_Classes.h:129
Player::~Player
virtual ~Player()
Definition BoardGame_Classes.h:141
Player::Player
Player(string n, T s, PlayerType t)
Construct a player with name, symbol, and type.
Definition BoardGame_Classes.h:137
Player::get_name
string get_name() const
Get the player's name.
Definition BoardGame_Classes.h:144
Player::name
string name
Player name.
Definition BoardGame_Classes.h:128
Player::get_symbol
T get_symbol() const
Get the player's symbol.
Definition BoardGame_Classes.h:150
UI
Base class for handling user interface and input/output.
Definition BoardGame_Classes.h:166
UI::~UI
virtual ~UI()
Definition BoardGame_Classes.h:201
UI::display_message
void display_message(string message)
Display any message to the user.
Definition BoardGame_Classes.h:204
UI::display_board_matrix
void display_board_matrix(const vector< vector< T > > &matrix) const
Display the current board matrix in formatted form.
Definition BoardGame_Classes.h:224
UI::setup_players
virtual Player< T > ** setup_players()
Set up players for the game.
Definition BoardGame_Classes.h:308
UI::cell_width
int cell_width
Width of each displayed board cell.
Definition BoardGame_Classes.h:168
UI::get_player_name
string get_player_name(string player_label)
Ask the user for the player's name.
Definition BoardGame_Classes.h:173
UI::get_move
virtual Move< T > * get_move(Player< T > *)=0
Ask the user (or AI) to make a move.
UI::UI
UI(string message, int cell_display_width)
Construct the UI and display a welcome message.
Definition BoardGame_Classes.h:196
UI::get_player_type_choice
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
UI::create_player
virtual Player< T > * create_player(string &name, T symbol, PlayerType type)=0
Create a player object based on input name, symbol, and type.