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

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