| OOP A3 Board Games 35 a Board Game Project Made by Students at Cairo FCAI

Loading...
Searching...
No Matches
TicTacToe4x4.h
Go to the documentation of this file.
1#ifndef TICTACTOE4X4_H
2#define TICTACTOE4X4_H
3
4#include "BoardGame_Classes.h"
5#include <cstdlib>
6#include <ctime>
7
8 class TicTacToe4x4_Board : public Board<char> {
9 public:
10 TicTacToe4x4_Board();
11 bool update_board(Move<char>* move) override;
12 bool is_win(Player<char>* player) override;
13 bool is_draw(Player<char>* player) override;
14 bool is_lose(Player<char>* player) override;
15 bool game_is_over(Player<char>* player) override;
16};
17
18 class TicTacToe4x4_Player : public Player<char> {
19 public:
20 TicTacToe4x4_Player(string name, char symbol, PlayerType type)
21 : Player<char>(name, symbol, type) {}
22};
23
24 class TicTacToe4x4_UI : public UI<char> {
25 public:
26 TicTacToe4x4_UI() : UI<char>("4x4 TicTacToe", 3) {}
27
28 Move<char>* get_move(Player<char>* p) override {
29 int x, y;
30
31 // Computer move
32 if (p->get_type() == PlayerType::COMPUTER) {
33 srand(time(0));
34 auto board_matrix = p->get_board_ptr()->get_board_matrix();
35
36 // find an empty cell
37 do {
38 x = rand() % 4;
39 y = rand() % 4;
40 } while (board_matrix[x][y] != ' ');
41
42 cout << "\nComputer played at (" << x << "," << y << ")\n";
43 return new Move<char>(x, y, p->get_symbol());
44 }
45
46 // Human move
47 while (true) {
48 cout << "\n" << p->get_name() << " enter row and column [0-3]: ";
49 cin >> x >> y;
50
51 if (x < 0 || x > 3 || y < 0 || y > 3) {
52 cout << "Invalid cell. Please enter values between 0-3.\n";
53 continue;
54 }
55
56 auto board_matrix = p->get_board_ptr()->get_board_matrix();
57 if (board_matrix[x][y] != ' ') {
58 cout << "Box already occupied. Choose another Box.\n";
59 continue;
60 }
61
62 return new Move<char>(x, y, p->get_symbol());
63 }
64 }
65
66 Player<char>* create_player(string& name, char symbol, PlayerType type) override {
67 return new TicTacToe4x4_Player(name, symbol, type);
68 }
69};
70
71// Implementation
72 TicTacToe4x4_Board::TicTacToe4x4_Board() : Board(4, 4) {
73 // Initialize empty board
74 for (auto& row : board) {
75 for (auto& cell : row) {
76 cell = ' ';
77 }
78 }
79 n_moves = 0;
80}
81
82 bool TicTacToe4x4_Board::update_board(Move<char>* move) {
83 int x = move->get_x();
84 int y = move->get_y();
85 char symbol = move->get_symbol();
86
87 // Check if move is valid
88 if (x < 0 || x >= rows || y < 0 || y >= columns) {
89 return false;
90 }
91
92 if (board[x][y] != ' ') {
93 return false;
94 }
95
96 board[x][y] = symbol;
97 n_moves++;
98 return true;
99}
100
101 bool TicTacToe4x4_Board::is_win(Player<char>* player) {
102 char symbol = player->get_symbol();
103
104 // Check rows
105 for (int i = 0; i < 4; i++) {
106 if (board[i][0] == symbol && board[i][1] == symbol &&
107 board[i][2] == symbol && board[i][3] == symbol) {
108 return true;
109 }
110 }
111
112 // Check columns
113 for (int j = 0; j < 4; j++) {
114 if (board[0][j] == symbol && board[1][j] == symbol &&
115 board[2][j] == symbol && board[3][j] == symbol) {
116 return true;
117 }
118 }
119
120 // Check diagonals
121 if (board[0][0] == symbol && board[1][1] == symbol &&
122 board[2][2] == symbol && board[3][3] == symbol) {
123 return true;
124 }
125 if (board[0][3] == symbol && board[1][2] == symbol &&
126 board[2][1] == symbol && board[3][0] == symbol) {
127 return true;
128 }
129
130 return false;
131}
132
133 bool TicTacToe4x4_Board::is_draw(Player<char>* player) {
134 // Check if board is full and no win
135 for (int i = 0; i < rows; i++) {
136 for (int j = 0; j < columns; j++) {
137 if (board[i][j] == ' ') {
138 return false;
139 }
140 }
141 }
142 return !is_win(player);
143}
144
145 bool TicTacToe4x4_Board::is_lose(Player<char>* player) {
146 return false;
147}
148
149 bool TicTacToe4x4_Board::game_is_over(Player<char>* player) {
150 return is_win(player) || is_draw(player);
151}
152
153#endif // TICTACTOE4X4_H
BoardGame_Classes.h
PlayerType
PlayerType
Represents the type of player in the game.
Definition BoardGame_Classes.h:24
PlayerType::COMPUTER
@ COMPUTER
A computer-controlled player.
Definition BoardGame_Classes.h:26
Board< char >::n_moves
int n_moves
Definition BoardGame_Classes.h:45
Board< char >::rows
int rows
Definition BoardGame_Classes.h:42
Board< char >::Board
Board(int rows, int columns)
Definition BoardGame_Classes.h:51
Board< char >::board
vector< vector< char > > board
Definition BoardGame_Classes.h:44
Board< char >::columns
int columns
Definition BoardGame_Classes.h:43
Move
Represents a single move in a board game.
Definition BoardGame_Classes.h:100
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< char >::symbol
char symbol
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::get_board_ptr
Board< T > * get_board_ptr() const
Get a pointer to the game board.
Definition BoardGame_Classes.h:153
Player< char >::type
PlayerType type
Definition BoardGame_Classes.h:129
Player< char >::Player
Player(string n, char s, PlayerType t)
Definition BoardGame_Classes.h:137
Player::get_name
string get_name() const
Get the player's name.
Definition BoardGame_Classes.h:144
Player< char >::name
string name
Definition BoardGame_Classes.h:128
Player::get_symbol
T get_symbol() const
Get the player's symbol.
Definition BoardGame_Classes.h:150
TicTacToe4x4_Board::update_board
bool update_board(Move< char > *move) override
Update the board with a new move.
Definition TicTacToe4x4.h:82
TicTacToe4x4_Board::is_draw
bool is_draw(Player< char > *player) override
Check if the game ended in a draw.
Definition TicTacToe4x4.h:133
TicTacToe4x4_Board::is_lose
bool is_lose(Player< char > *player) override
Check if a player has lost.
Definition TicTacToe4x4.h:145
TicTacToe4x4_Board::TicTacToe4x4_Board
TicTacToe4x4_Board()
Definition TicTacToe4x4.h:72
TicTacToe4x4_Board::is_win
bool is_win(Player< char > *player) override
Check if a player has won.
Definition TicTacToe4x4.h:101
TicTacToe4x4_Board::game_is_over
bool game_is_over(Player< char > *player) override
Check if the game is over.
Definition TicTacToe4x4.h:149
TicTacToe4x4_Player
Definition TicTacToe4x4.h:18
TicTacToe4x4_Player::TicTacToe4x4_Player
TicTacToe4x4_Player(string name, char symbol, PlayerType type)
Definition TicTacToe4x4.h:20
TicTacToe4x4_UI::get_move
Move< char > * get_move(Player< char > *p) override
Ask the user (or AI) to make a move.
Definition TicTacToe4x4.h:28
TicTacToe4x4_UI::create_player
Player< char > * create_player(string &name, char symbol, PlayerType type) override
Create a player object based on input name, symbol, and type.
Definition TicTacToe4x4.h:66
TicTacToe4x4_UI::TicTacToe4x4_UI
TicTacToe4x4_UI()
Definition TicTacToe4x4.h:26
UI< char >::UI
UI(string message, int cell_display_width)
Definition BoardGame_Classes.h:196