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>
8using namespace std;
9
10// 5x5 Tic Tac Toe Board
11class TicTacToe5x5_Board : public Board<char> {
12private:
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
50public:
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
121class TicTacToe5x5_Player : public Player<char> {
122public:
124};
125
126// 5x5 Random Player
127class TicTacToe5x5_RandomPlayer : public Player<char> {
128public:
130 srand(time(0));
131 }
132};
133
134// 5x5 UI
135class TicTacToe5x5_UI : public UI<char> {
136public:
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
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
bool update_board(Move< char > *move) override
Update the board with a new move.
Definition TicTacToe5x5.h:59
bool is_draw(Player< char > *player) override
Check if the game ended in a draw.
Definition TicTacToe5x5.h:103
TicTacToe5x5_Board()
Definition TicTacToe5x5.h:51
bool is_win(Player< char > *player) override
Check if a player has won.
Definition TicTacToe5x5.h:79
bool game_is_over(Player< char > *player) override
Check if the game is over.
Definition TicTacToe5x5.h:115
bool is_lose(Player< char > *player) override
Check if a player has lost.
Definition TicTacToe5x5.h:91
TicTacToe5x5_Player(string name, char symbol)
Definition TicTacToe5x5.h:123
TicTacToe5x5_RandomPlayer(char symbol)
Definition TicTacToe5x5.h:129
TicTacToe5x5_UI()
Definition TicTacToe5x5.h:137
Move< char > * get_move(Player< char > *player) override
Ask the user (or AI) to make a move.
Definition TicTacToe5x5.h:139
UI(string message, int cell_display_width)
Definition BoardGame_Classes.h:196