1#ifndef ULTIMATETICTACTOE_H
2#define ULTIMATETICTACTOE_H
13 char sub_winners[3][3] = { {
' ',
' ',
' '}, {
' ',
' ',
' '}, {
' ',
' ',
' '} };
14 int last_move_x = -1, last_move_y = -1;
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;
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)
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)
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)
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)
50 bool check_main_win(
char symbol) {
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)
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)
64 if (sub_winners[0][0] == symbol && sub_winners[1][1] == symbol && sub_winners[2][2] == symbol)
66 if (sub_winners[0][2] == symbol && sub_winners[1][1] == symbol && sub_winners[2][0] == symbol)
74 for (
int i = 0; i < 9; i++) {
75 for (
int j = 0; j < 9; j++) {
82 int x = move->
get_x();
83 int y = move->
get_y();
86 if (x < 0 || x >= 9 || y < 0 || y >= 9) {
87 cout <<
"Invalid position! Use 0-8.\n";
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;
98 if (sub_winners[target_subgrid_row][target_subgrid_col] ==
' ') {
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";
109 if (
board[x][y] !=
' ') {
110 cout <<
"Cell already occupied!\n";
114 board[x][y] = symbol;
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;
142 for (
int i = 0; i < 3; i++) {
143 for (
int j = 0; j < 3; j++) {
144 if (sub_winners[i][j] ==
' ') {
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] ==
' ') {
154 if (has_empty)
break;
156 if (has_empty)
return false;
191 <<
"), enter your move (row and column 0-8): ";
198 cout << player->
get_name() <<
" chooses position (" << x <<
", " << y <<
")\n";
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
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
UltimateTicTacToe_Player(string name, char symbol)
Definition UltimateTicTacToe.h:171
UltimateTicTacToe_RandomPlayer(char symbol)
Definition UltimateTicTacToe.h:177
Move< char > * get_move(Player< char > *player) override
Ask the user (or AI) to make a move.
Definition UltimateTicTacToe.h:187
UltimateTicTacToe_UI()
Definition UltimateTicTacToe.h:185
UltimateTicTacToeBoard()
Definition UltimateTicTacToe.h:73
bool is_lose(Player< char > *player) override
Check if a player has lost.
Definition UltimateTicTacToe.h:136
bool is_win(Player< char > *player) override
Check if a player has won.
Definition UltimateTicTacToe.h:132
bool game_is_over(Player< char > *player) override
Check if the game is over.
Definition UltimateTicTacToe.h:163
bool update_board(Move< char > *move) override
Update the board with a new move.
Definition UltimateTicTacToe.h:81
bool is_draw(Player< char > *player) override
Check if the game ended in a draw.
Definition UltimateTicTacToe.h:140