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
8class TicTacToe4x4_Board : public Board<char> {
9public:
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
18class TicTacToe4x4_Player : public Player<char> {
19public:
22};
23
24class TicTacToe4x4_UI : public UI<char> {
25public:
26 TicTacToe4x4_UI() : UI<char>("4x4 TicTacToe", 3) {}
27
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
73 // Initialize empty board
74 for (auto& row : board) {
75 for (auto& cell : row) {
76 cell = ' ';
77 }
78 }
79 n_moves = 0;
80}
81
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
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
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
146 return false;
147}
148
150 return is_win(player) || is_draw(player);
151}
152
153#endif // TICTACTOE4X4_H
PlayerType
Represents the type of player in the game.
Definition BoardGame_Classes.h:24
@ COMPUTER
A computer-controlled player.
Definition BoardGame_Classes.h:26
int n_moves
Definition BoardGame_Classes.h:45
int rows
Definition BoardGame_Classes.h:42
Board(int rows, int columns)
Definition BoardGame_Classes.h:51
vector< vector< char > > board
Definition BoardGame_Classes.h:44
int columns
Definition BoardGame_Classes.h:43
Represents a single move in a board game.
Definition BoardGame_Classes.h:100
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
char symbol
Definition BoardGame_Classes.h:130
PlayerType get_type() const
Get player type (e.g., 'H' or 'C').
Definition BoardGame_Classes.h:147
Board< T > * get_board_ptr() const
Get a pointer to the game board.
Definition BoardGame_Classes.h:153
PlayerType type
Definition BoardGame_Classes.h:129
Player(string n, char s, PlayerType t)
Definition BoardGame_Classes.h:137
string get_name() const
Get the player's name.
Definition BoardGame_Classes.h:144
string name
Definition BoardGame_Classes.h:128
T get_symbol() const
Get the player's symbol.
Definition BoardGame_Classes.h:150
bool update_board(Move< char > *move) override
Update the board with a new move.
Definition TicTacToe4x4.h:82
bool is_draw(Player< char > *player) override
Check if the game ended in a draw.
Definition TicTacToe4x4.h:133
bool is_lose(Player< char > *player) override
Check if a player has lost.
Definition TicTacToe4x4.h:145
TicTacToe4x4_Board()
Definition TicTacToe4x4.h:72
bool is_win(Player< char > *player) override
Check if a player has won.
Definition TicTacToe4x4.h:101
bool game_is_over(Player< char > *player) override
Check if the game is over.
Definition TicTacToe4x4.h:149
Definition TicTacToe4x4.h:18
TicTacToe4x4_Player(string name, char symbol, PlayerType type)
Definition TicTacToe4x4.h:20
Move< char > * get_move(Player< char > *p) override
Ask the user (or AI) to make a move.
Definition TicTacToe4x4.h:28
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()
Definition TicTacToe4x4.h:26
UI(string message, int cell_display_width)
Definition BoardGame_Classes.h:196