OOP A3 Board Games 35
a Board Game Project Made by Students at Cairo FCAI
Loading...
Searching...
No Matches
ObstaclesTicTacToe.h
Go to the documentation of this file.
1#ifndef OBSTACLESTICTACTOE_H
2#define OBSTACLESTICTACTOE_H
3
4#include "BoardGame_Classes.h"
5#include <iostream>
6#include <cstdlib>
7#include <ctime>
8#include <vector>
9using namespace std;
10
11// Obstacles Tic Tac Toe Board
12class ObstaclesTicTacToeBoard : public Board<char> {
13private:
14 vector<pair<int, int>> obstacles;
15 int moves_since_last_obstacle = 0;
16
17 void add_random_obstacle() {
18 int x, y;
19 do {
20 x = rand() % 6;
21 y = rand() % 6;
22 } while (board[x][y] != ' ' || is_obstacle(x, y));
23
24 obstacles.push_back({ x, y });
25 board[x][y] = '#'; // Obstacle symbol
26 }
27
28 bool is_obstacle(int x, int y) {
29 for (auto& obs : obstacles) {
30 if (obs.first == x && obs.second == y)
31 return true;
32 }
33 return false;
34 }
35
36public:
38 for (int i = 0; i < 6; i++) {
39 for (int j = 0; j < 6; j++) {
40 board[i][j] = ' ';
41 }
42 }
43 srand(time(0));
44 }
45
46 bool update_board(Move<char>* move) override {
47 int x = move->get_x();
48 int y = move->get_y();
49 char symbol = move->get_symbol();
50
51 if (x < 0 || x >= 6 || y < 0 || y >= 6) {
52 cout << "Invalid position! Use 0-5.\n";
53 return false;
54 }
55
56 if (is_obstacle(x, y)) {
57 cout << "Cell is an obstacle!\n";
58 return false;
59 }
60
61 if (board[x][y] != ' ') {
62 cout << "Cell already occupied!\n";
63 return false;
64 }
65
66 board[x][y] = symbol;
67 n_moves++;
68 moves_since_last_obstacle++;
69
70 // Add obstacles every 2 moves
71 if (moves_since_last_obstacle >= 2) {
72 for (int i = 0; i < 2; i++) {
73 add_random_obstacle();
74 }
75 moves_since_last_obstacle = 0;
76 cout << "Two new obstacles added!\n";
77 }
78
79 return true;
80 }
81
82 bool is_win(Player<char>* player) override {
83 char symbol = player->get_symbol();
84
85 // Check 4 in a row
86 // Horizontal
87 for (int i = 0; i < 6; i++) {
88 for (int j = 0; j < 3; j++) {
89 if (board[i][j] == symbol && board[i][j + 1] == symbol &&
90 board[i][j + 2] == symbol && board[i][j + 3] == symbol)
91 return true;
92 }
93 }
94
95 // Vertical
96 for (int j = 0; j < 6; j++) {
97 for (int i = 0; i < 3; i++) {
98 if (board[i][j] == symbol && board[i + 1][j] == symbol &&
99 board[i + 2][j] == symbol && board[i + 3][j] == symbol)
100 return true;
101 }
102 }
103
104 // Diagonals
105 for (int i = 0; i < 3; i++) {
106 for (int j = 0; j < 3; j++) {
107 if (board[i][j] == symbol && board[i + 1][j + 1] == symbol &&
108 board[i + 2][j + 2] == symbol && board[i + 3][j + 3] == symbol)
109 return true;
110 }
111 }
112
113 for (int i = 0; i < 3; i++) {
114 for (int j = 3; j < 6; j++) {
115 if (board[i][j] == symbol && board[i + 1][j - 1] == symbol &&
116 board[i + 2][j - 2] == symbol && board[i + 3][j - 3] == symbol)
117 return true;
118 }
119 }
120
121 return false;
122 }
123
124 bool is_lose(Player<char>* player) override {
125 return false;
126 }
127
128 bool is_draw(Player<char>* player) override {
129 // Check if board is full (no empty cells except obstacles)
130 for (int i = 0; i < 6; i++) {
131 for (int j = 0; j < 6; j++) {
132 if (board[i][j] == ' ') {
133 return false;
134 }
135 }
136 }
137 return !is_win(player);
138 }
139
140 bool game_is_over(Player<char>* player) override {
141 return is_win(player) || is_draw(player);
142 }
143};
144
145// Obstacles Player
146class ObstaclesTicTacToe_Player : public Player<char> {
147public:
149};
150
151// Obstacles Random Player
153public:
155 srand(time(0));
156 }
157};
158
159// Obstacles UI
160class ObstaclesTicTacToe_UI : public UI<char> {
161public:
162 ObstaclesTicTacToe_UI() : UI("Welcome to Obstacles Tic-Tac-Toe! (4 in a row, obstacles appear)", 3) {}
163
164 Move<char>* get_move(Player<char>* player) override {
165 if (player->get_type() == PlayerType::HUMAN) {
166 int x, y;
167 cout << player->get_name() << " (" << player->get_symbol()
168 << "), enter your move (row and column 0-5): ";
169 cin >> x >> y;
170 return new Move<char>(x, y, player->get_symbol());
171 }
172 else {
173 int x = rand() % 6;
174 int y = rand() % 6;
175 cout << player->get_name() << " chooses position (" << x << ", " << y << ")\n";
176 return new Move<char>(x, y, player->get_symbol());
177 }
178 }
179};
180
181#endif // OBSTACLESTICTACTOE_H
PlayerType
Represents the type of player in the game.
Definition BoardGame_Classes.h:24
@ HUMAN
A human player.
Definition BoardGame_Classes.h:25
@ COMPUTER
A computer-controlled player.
Definition BoardGame_Classes.h:26
int n_moves
Definition BoardGame_Classes.h:45
Board(int rows, int columns)
Definition BoardGame_Classes.h:51
vector< vector< char > > board
Definition BoardGame_Classes.h:44
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
ObstaclesTicTacToe_Player(string name, char symbol)
Definition ObstaclesTicTacToe.h:148
ObstaclesTicTacToe_RandomPlayer(char symbol)
Definition ObstaclesTicTacToe.h:154
ObstaclesTicTacToe_UI()
Definition ObstaclesTicTacToe.h:162
Move< char > * get_move(Player< char > *player) override
Ask the user (or AI) to make a move.
Definition ObstaclesTicTacToe.h:164
bool is_win(Player< char > *player) override
Check if a player has won.
Definition ObstaclesTicTacToe.h:82
bool is_lose(Player< char > *player) override
Check if a player has lost.
Definition ObstaclesTicTacToe.h:124
bool game_is_over(Player< char > *player) override
Check if the game is over.
Definition ObstaclesTicTacToe.h:140
bool update_board(Move< char > *move) override
Update the board with a new move.
Definition ObstaclesTicTacToe.h:46
ObstaclesTicTacToeBoard()
Definition ObstaclesTicTacToe.h:37
bool is_draw(Player< char > *player) override
Check if the game ended in a draw.
Definition ObstaclesTicTacToe.h:128
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
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
UI(string message, int cell_display_width)
Definition BoardGame_Classes.h:196