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

Loading...
Searching...
No Matches
UltimateTicTacToe.h
Go to the documentation of this file.
1#ifndef ULTIMATETICTACTOE_H
2#define ULTIMATETICTACTOE_H
3
4#include "BoardGame_Classes.h"
5#include <iostream>
6#include <cstdlib>
7#include <ctime>
8 using namespace std;
9
10// Ultimate Tic Tac Toe Board
11 class UltimateTicTacToeBoard : public Board<char> {
12 private:
13 char sub_winners[3][3] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };
14 int last_move_x = -1, last_move_y = -1;
15
16 bool check_sub_win(int subgrid_row, int subgrid_col, char symbol) {
17 int start_row = subgrid_row * 3;
18 int start_col = subgrid_col * 3;
19
20 // Check rows in subgrid
21 for (int i = 0; i < 3; i++) {
22 if (board[start_row + i][start_col] == symbol &&
23 board[start_row + i][start_col + 1] == symbol &&
24 board[start_row + i][start_col + 2] == symbol)
25 return true;
26 }
27
28 // Check columns in subgrid
29 for (int j = 0; j < 3; j++) {
30 if (board[start_row][start_col + j] == symbol &&
31 board[start_row + 1][start_col + j] == symbol &&
32 board[start_row + 2][start_col + j] == symbol)
33 return true;
34 }
35
36 // Check diagonals in subgrid
37 if (board[start_row][start_col] == symbol &&
38 board[start_row + 1][start_col + 1] == symbol &&
39 board[start_row + 2][start_col + 2] == symbol)
40 return true;
41
42 if (board[start_row][start_col + 2] == symbol &&
43 board[start_row + 1][start_col + 1] == symbol &&
44 board[start_row + 2][start_col] == symbol)
45 return true;
46
47 return false;
48 }
49
50 bool check_main_win(char symbol) {
51 // Check rows in main grid
52 for (int i = 0; i < 3; i++) {
53 if (sub_winners[i][0] == symbol && sub_winners[i][1] == symbol && sub_winners[i][2] == symbol)
54 return true;
55 }
56
57 // Check columns in main grid
58 for (int j = 0; j < 3; j++) {
59 if (sub_winners[0][j] == symbol && sub_winners[1][j] == symbol && sub_winners[2][j] == symbol)
60 return true;
61 }
62
63 // Check diagonals in main grid
64 if (sub_winners[0][0] == symbol && sub_winners[1][1] == symbol && sub_winners[2][2] == symbol)
65 return true;
66 if (sub_winners[0][2] == symbol && sub_winners[1][1] == symbol && sub_winners[2][0] == symbol)
67 return true;
68
69 return false;
70 }
71
72 public:
73 UltimateTicTacToeBoard() : Board(9, 9) {
74 for (int i = 0; i < 9; i++) {
75 for (int j = 0; j < 9; j++) {
76 board[i][j] = ' ';
77 }
78 }
79 }
80
81 bool update_board(Move<char>* move) override {
82 int x = move->get_x();
83 int y = move->get_y();
84 char symbol = move->get_symbol();
85
86 if (x < 0 || x >= 9 || y < 0 || y >= 9) {
87 cout << "Invalid position! Use 0-8.\n";
88 return false;
89 }
90
91 // Check if move is in allowed subgrid
92 if (last_move_x != -1 && last_move_y != -1) {
93 int target_subgrid_row = last_move_x % 3;
94 int target_subgrid_col = last_move_y % 3;
95 int current_subgrid_row = x / 3;
96 int current_subgrid_col = y / 3;
97
98 if (sub_winners[target_subgrid_row][target_subgrid_col] == ' ') {
99 // Must play in specified subgrid unless it's won
100 if (current_subgrid_row != target_subgrid_row ||
101 current_subgrid_col != target_subgrid_col) {
102 cout << "Must play in subgrid (" << target_subgrid_row
103 << ", " << target_subgrid_col << ")\n";
104 return false;
105 }
106 }
107 }
108
109 if (board[x][y] != ' ') {
110 cout << "Cell already occupied!\n";
111 return false;
112 }
113
114 board[x][y] = symbol;
115 n_moves++;
116
117 // Check if subgrid is won
118 int subgrid_row = x / 3;
119 int subgrid_col = y / 3;
120 if (sub_winners[subgrid_row][subgrid_col] == ' ' &&
121 check_sub_win(subgrid_row, subgrid_col, symbol)) {
122 sub_winners[subgrid_row][subgrid_col] = symbol;
123 }
124
125 // Set next move target
126 last_move_x = x;
127 last_move_y = y;
128
129 return true;
130 }
131
132 bool is_win(Player<char>* player) override {
133 return check_main_win(player->get_symbol());
134 }
135
136 bool is_lose(Player<char>* player) override {
137 return false;
138 }
139
140 bool is_draw(Player<char>* player) override {
141 // Check if all subgrids are won or full
142 for (int i = 0; i < 3; i++) {
143 for (int j = 0; j < 3; j++) {
144 if (sub_winners[i][j] == ' ') {
145 // Check if subgrid has empty cells
146 bool has_empty = false;
147 for (int si = 0; si < 3; si++) {
148 for (int sj = 0; sj < 3; sj++) {
149 if (board[i * 3 + si][j * 3 + sj] == ' ') {
150 has_empty = true;
151 break;
152 }
153 }
154 if (has_empty) break;
155 }
156 if (has_empty) return false;
157 }
158 }
159 }
160 return !is_win(player);
161 }
162
163 bool game_is_over(Player<char>* player) override {
164 return is_win(player) || is_draw(player);
165 }
166};
167
168// Ultimate Player
169 class UltimateTicTacToe_Player : public Player<char> {
170 public:
171 UltimateTicTacToe_Player(string name, char symbol) : Player(name, symbol, PlayerType::HUMAN) {}
172};
173
174// Ultimate Random Player
175 class UltimateTicTacToe_RandomPlayer : public Player<char> {
176 public:
177 UltimateTicTacToe_RandomPlayer(char symbol) : Player("Random Computer", symbol, PlayerType::COMPUTER) {
178 srand(time(0));
179 }
180};
181
182// Ultimate UI
183 class UltimateTicTacToe_UI : public UI<char> {
184 public:
185 UltimateTicTacToe_UI() : UI("Welcome to Ultimate Tic-Tac-Toe!", 3) {}
186
187 Move<char>* get_move(Player<char>* player) override {
188 if (player->get_type() == PlayerType::HUMAN) {
189 int x, y;
190 cout << player->get_name() << " (" << player->get_symbol()
191 << "), enter your move (row and column 0-8): ";
192 cin >> x >> y;
193 return new Move<char>(x, y, player->get_symbol());
194 }
195 else {
196 int x = rand() % 9;
197 int y = rand() % 9;
198 cout << player->get_name() << " chooses position (" << x << ", " << y << ")\n";
199 return new Move<char>(x, y, player->get_symbol());
200 }
201 }
202};
203
204#endif // ULTIMATETICTACTOE_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
UI< char >::UI
UI(string message, int cell_display_width)
Definition BoardGame_Classes.h:196
UltimateTicTacToe_Player::UltimateTicTacToe_Player
UltimateTicTacToe_Player(string name, char symbol)
Definition UltimateTicTacToe.h:171
UltimateTicTacToe_RandomPlayer::UltimateTicTacToe_RandomPlayer
UltimateTicTacToe_RandomPlayer(char symbol)
Definition UltimateTicTacToe.h:177
UltimateTicTacToe_UI::get_move
Move< char > * get_move(Player< char > *player) override
Ask the user (or AI) to make a move.
Definition UltimateTicTacToe.h:187
UltimateTicTacToe_UI::UltimateTicTacToe_UI
UltimateTicTacToe_UI()
Definition UltimateTicTacToe.h:185
UltimateTicTacToeBoard::UltimateTicTacToeBoard
UltimateTicTacToeBoard()
Definition UltimateTicTacToe.h:73
UltimateTicTacToeBoard::is_lose
bool is_lose(Player< char > *player) override
Check if a player has lost.
Definition UltimateTicTacToe.h:136
UltimateTicTacToeBoard::is_win
bool is_win(Player< char > *player) override
Check if a player has won.
Definition UltimateTicTacToe.h:132
UltimateTicTacToeBoard::game_is_over
bool game_is_over(Player< char > *player) override
Check if the game is over.
Definition UltimateTicTacToe.h:163
UltimateTicTacToeBoard::update_board
bool update_board(Move< char > *move) override
Update the board with a new move.
Definition UltimateTicTacToe.h:81
UltimateTicTacToeBoard::is_draw
bool is_draw(Player< char > *player) override
Check if the game ended in a draw.
Definition UltimateTicTacToe.h:140