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

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