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

Loading...
Searching...
No Matches
Pyramic_XO.h
Go to the documentation of this file.
1#ifndef PYRAMIC_XO_H
2#define PYRAMIC_XO_H
3
4#include "BoardGame_Classes.h"
5#include <iostream>
6#include <cstdlib>
7#include <ctime>
8 using namespace std;
9
10// Pyramid Tic Tac Toe Board
11 class Pyramic_XO_Board : public Board<char> {
12 private:
13 bool is_valid_position(int x, int y) {
14 if (x < 0 || x >= 3 || y < 0 || y >= 5) return false;
15 int start = (5 - (2 * x + 1)) / 2;
16 int end = start + (2 * x + 1);
17 return (y >= start && y < end);
18 }
19
20 public:
21 Pyramic_XO_Board() : Board(3, 5) {
22 for (int i = 0; i < 3; i++) {
23 for (int j = 0; j < 5; j++) {
24 if (is_valid_position(i, j)) {
25 board[i][j] = '.';
26 }
27 else {
28 board[i][j] = '#'; // Invalid position
29 }
30 }
31 }
32 }
33
34 bool update_board(Move<char>* move) override {
35 int x = move->get_x();
36 int y = move->get_y();
37 char symbol = move->get_symbol();
38
39 if (!is_valid_position(x, y)) {
40 cout << "Invalid position! Not in pyramid shape.\n";
41 return false;
42 }
43
44 if (board[x][y] != '.') {
45 cout << "Cell already occupied!\n";
46 return false;
47 }
48
49 board[x][y] = symbol;
50 n_moves++;
51 return true;
52 }
53
54 bool is_win(Player<char>* player) override {
55 char symbol = player->get_symbol();
56
57 // Check center column
58 if (board[0][2] == symbol && board[1][2] == symbol && board[2][2] == symbol)
59 return true;
60
61 // Check rows
62 for (int i = 0; i < 3; i++) {
63 int start = (5 - (2 * i + 1)) / 2;
64 int end = start + (2 * i + 1);
65 for (int j = start; j <= end - 3; j++) {
66 if (board[i][j] == symbol && board[i][j + 1] == symbol && board[i][j + 2] == symbol)
67 return true;
68 }
69 }
70
71 // Check diagonals
72 if (board[0][2] == symbol && board[1][1] == symbol && board[2][0] == symbol)
73 return true;
74 if (board[0][2] == symbol && board[1][3] == symbol && board[2][4] == symbol)
75 return true;
76
77 return false;
78 }
79
80 bool is_lose(Player<char>* player) override {
81 return false;
82 }
83
84 bool is_draw(Player<char>* player) override {
85 // Count valid cells (9 in pyramid)
86 int valid_cells = 0;
87 int filled_cells = 0;
88
89 for (int i = 0; i < 3; i++) {
90 for (int j = 0; j < 5; j++) {
91 if (board[i][j] != '#') {
92 valid_cells++;
93 if (board[i][j] != '.') {
94 filled_cells++;
95 }
96 }
97 }
98 }
99
100 return (filled_cells == valid_cells && !is_win(player));
101 }
102
103 bool game_is_over(Player<char>* player) override {
104 return is_win(player) || is_draw(player);
105 }
106};
107
108// Pyramid Player
109 class Pyramic_XO_Player : public Player<char> {
110 public:
111 Pyramic_XO_Player(string name, char symbol) : Player(name, symbol, PlayerType::HUMAN) {}
112};
113
114// Pyramid Random Player
115 class Pyramic_XO_RandomPlayer : public Player<char> {
116 public:
117 Pyramic_XO_RandomPlayer(char symbol) : Player("Random Computer", symbol, PlayerType::COMPUTER) {
118 srand(time(0));
119 }
120};
121
122// Pyramid UI
123 class Pyramic_XO_UI : public UI<char> {
124 public:
125 Pyramic_XO_UI() : UI("Welcome to Pyramid Tic-Tac-Toe!", 3) {}
126
127 Move<char>* get_move(Player<char>* player) override {
128 if (player->get_type() == PlayerType::HUMAN) {
129 int x, y;
130 cout << player->get_name() << " (" << player->get_symbol()
131 << "), enter your move (row 0-2, column 0-4 in pyramid shape): ";
132 cin >> x >> y;
133 return new Move<char>(x, y, player->get_symbol());
134 }
135 else {
136 int x, y;
137 do {
138 x = rand() % 3;
139 y = rand() % 5;
140 } while (!is_valid_pyramid_position(x, y));
141
142 cout << player->get_name() << " chooses position (" << x << ", " << y << ")\n";
143 return new Move<char>(x, y, player->get_symbol());
144 }
145 }
146
147 private:
148 bool is_valid_pyramid_position(int x, int y) {
149 int start = (5 - (2 * x + 1)) / 2;
150 int end = start + (2 * x + 1);
151 return (x >= 0 && x < 3 && y >= start && y < end);
152 }
153};
154
155#endif // PYRAMIC_XO_H
BoardGame_Classes.h
PlayerType
PlayerType
Represents the type of player in the game.
Definition BoardGame_Classes.h:24
PlayerType::HUMAN
@ HUMAN
A human player.
Definition BoardGame_Classes.h:25
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 >::Board
Board(int rows, int columns)
Definition BoardGame_Classes.h:51
Board< char >::board
vector< vector< char > > board
Definition BoardGame_Classes.h:44
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< 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
Pyramic_XO_Board::game_is_over
bool game_is_over(Player< char > *player) override
Check if the game is over.
Definition Pyramic_XO.h:103
Pyramic_XO_Board::update_board
bool update_board(Move< char > *move) override
Update the board with a new move.
Definition Pyramic_XO.h:34
Pyramic_XO_Board::Pyramic_XO_Board
Pyramic_XO_Board()
Definition Pyramic_XO.h:21
Pyramic_XO_Board::is_lose
bool is_lose(Player< char > *player) override
Check if a player has lost.
Definition Pyramic_XO.h:80
Pyramic_XO_Board::is_win
bool is_win(Player< char > *player) override
Check if a player has won.
Definition Pyramic_XO.h:54
Pyramic_XO_Board::is_draw
bool is_draw(Player< char > *player) override
Check if the game ended in a draw.
Definition Pyramic_XO.h:84
Pyramic_XO_Player::Pyramic_XO_Player
Pyramic_XO_Player(string name, char symbol)
Definition Pyramic_XO.h:111
Pyramic_XO_RandomPlayer::Pyramic_XO_RandomPlayer
Pyramic_XO_RandomPlayer(char symbol)
Definition Pyramic_XO.h:117
Pyramic_XO_UI::Pyramic_XO_UI
Pyramic_XO_UI()
Definition Pyramic_XO.h:125
Pyramic_XO_UI::get_move
Move< char > * get_move(Player< char > *player) override
Ask the user (or AI) to make a move.
Definition Pyramic_XO.h:127
UI< char >::UI
UI(string message, int cell_display_width)
Definition BoardGame_Classes.h:196