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

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