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