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

Loading...
Searching...
No Matches
InfinityTicTacToe.h
Go to the documentation of this file.
1#ifndef INFINITYTICTACTOE_H
2#define INFINITYTICTACTOE_H
3
4#include "BoardGame_Classes.h"
5#include <iostream>
6#include <cstdlib>
7#include <ctime>
8#include <queue>
9 using namespace std;
10
11// Infinity Tic Tac Toe Board
12 class InfinityTicTacToeBoard : public Board<char> {
13 private:
14 queue<pair<int, int>> move_history;
15
16 void remove_oldest_move() {
17 if (move_history.empty()) return;
18
19 auto oldest = move_history.front();
20 move_history.pop();
21 board[oldest.first][oldest.second] = ' ';
22 n_moves--;
23 }
24
25 public:
26 InfinityTicTacToeBoard() : Board(3, 3) {
27 for (int i = 0; i < 3; i++) {
28 for (int j = 0; j < 3; j++) {
29 board[i][j] = ' ';
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 (x < 0 || x >= 3 || y < 0 || y >= 3) {
40 cout << "Invalid position! Use 0-2.\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 move_history.push({ x, y });
51 n_moves++;
52
53 // Remove oldest move every 3 moves
54 if (n_moves > 3) {
55 remove_oldest_move();
56 }
57
58 return true;
59 }
60
61 bool is_win(Player<char>* player) override {
62 char symbol = player->get_symbol();
63
64 // Check rows
65 for (int i = 0; i < 3; i++) {
66 if (board[i][0] == symbol && board[i][1] == symbol && board[i][2] == symbol)
67 return true;
68 }
69
70 // Check columns
71 for (int j = 0; j < 3; j++) {
72 if (board[0][j] == symbol && board[1][j] == symbol && board[2][j] == symbol)
73 return true;
74 }
75
76 // Check diagonals
77 if (board[0][0] == symbol && board[1][1] == symbol && board[2][2] == symbol)
78 return true;
79 if (board[0][2] == symbol && board[1][1] == symbol && board[2][0] == symbol)
80 return true;
81
82 return false;
83 }
84
85 bool is_lose(Player<char>* player) override {
86 return false;
87 }
88
89 bool is_draw(Player<char>* player) override {
90 // Infinity mode - technically no draw unless we want to limit
91 return false;
92 }
93
94 bool game_is_over(Player<char>* player) override {
95 return is_win(player);
96 }
97};
98
99// Infinity Player
100 class InfinityTicTacToe_Player : public Player<char> {
101 public:
102 InfinityTicTacToe_Player(string name, char symbol) : Player(name, symbol, PlayerType::HUMAN) {}
103};
104
105// Infinity Random Player
106 class InfinityTicTacToe_RandomPlayer : public Player<char> {
107 public:
108 InfinityTicTacToe_RandomPlayer(char symbol) : Player("Random Computer", symbol, PlayerType::COMPUTER) {
109 srand(time(0));
110 }
111};
112
113// Infinity UI
114 class InfinityTicTacToe_UI : public UI<char> {
115 public:
116 InfinityTicTacToe_UI() : UI("Welcome to Infinity Tic-Tac-Toe! (Oldest moves disappear)", 3) {}
117
118 Move<char>* get_move(Player<char>* player) override {
119 if (player->get_type() == PlayerType::HUMAN) {
120 int x, y;
121 cout << player->get_name() << " (" << player->get_symbol()
122 << "), enter your move (row and column 0-2): ";
123 cin >> x >> y;
124 return new Move<char>(x, y, player->get_symbol());
125 }
126 else {
127 int x = rand() % 3;
128 int y = rand() % 3;
129 cout << player->get_name() << " chooses position (" << x << ", " << y << ")\n";
130 return new Move<char>(x, y, player->get_symbol());
131 }
132 }
133};
134
135#endif // INFINITYTICTACTOE_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
InfinityTicTacToe_Player::InfinityTicTacToe_Player
InfinityTicTacToe_Player(string name, char symbol)
Definition InfinityTicTacToe.h:102
InfinityTicTacToe_RandomPlayer::InfinityTicTacToe_RandomPlayer
InfinityTicTacToe_RandomPlayer(char symbol)
Definition InfinityTicTacToe.h:108
InfinityTicTacToe_UI::get_move
Move< char > * get_move(Player< char > *player) override
Ask the user (or AI) to make a move.
Definition InfinityTicTacToe.h:118
InfinityTicTacToe_UI::InfinityTicTacToe_UI
InfinityTicTacToe_UI()
Definition InfinityTicTacToe.h:116
InfinityTicTacToeBoard::update_board
bool update_board(Move< char > *move) override
Update the board with a new move.
Definition InfinityTicTacToe.h:34
InfinityTicTacToeBoard::game_is_over
bool game_is_over(Player< char > *player) override
Check if the game is over.
Definition InfinityTicTacToe.h:94
InfinityTicTacToeBoard::is_draw
bool is_draw(Player< char > *player) override
Check if the game ended in a draw.
Definition InfinityTicTacToe.h:89
InfinityTicTacToeBoard::is_win
bool is_win(Player< char > *player) override
Check if a player has won.
Definition InfinityTicTacToe.h:61
InfinityTicTacToeBoard::InfinityTicTacToeBoard
InfinityTicTacToeBoard()
Definition InfinityTicTacToe.h:26
InfinityTicTacToeBoard::is_lose
bool is_lose(Player< char > *player) override
Check if a player has lost.
Definition InfinityTicTacToe.h:85
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