OOP A3 Board Games 35
a Board Game Project Made by Students at Cairo FCAI
Loading...
Searching...
No Matches
XO.h
Go to the documentation of this file.
1#ifndef XO_H
2#define XO_H
3
4#include "BoardGame_Classes.h"
5#include <iostream>
6#include <cstdlib>
7#include <ctime>
8using namespace std;
9
10// XO Board
11class XO_Board : public Board<char> {
12public:
13 XO_Board() : Board(3, 3) {
14 for (int i = 0; i < 3; i++) {
15 for (int j = 0; j < 3; j++) {
16 board[i][j] = ' ';
17 }
18 }
19 }
20
21 bool update_board(Move<char>* move) override {
22 int x = move->get_x();
23 int y = move->get_y();
24 char symbol = move->get_symbol();
25
26 if (x < 0 || x >= 3 || y < 0 || y >= 3) {
27 cout << "Invalid position! Use 0-2.\n";
28 return false;
29 }
30
31 if (board[x][y] != ' ') {
32 cout << "Cell already occupied!\n";
33 return false;
34 }
35
36 board[x][y] = symbol;
37 n_moves++;
38 return true;
39 }
40
41 bool is_win(Player<char>* player) override {
42 char symbol = player->get_symbol();
43
44 // Check rows
45 for (int i = 0; i < 3; i++) {
46 if (board[i][0] == symbol && board[i][1] == symbol && board[i][2] == symbol)
47 return true;
48 }
49
50 // Check columns
51 for (int j = 0; j < 3; j++) {
52 if (board[0][j] == symbol && board[1][j] == symbol && board[2][j] == symbol)
53 return true;
54 }
55
56 // Check diagonals
57 if (board[0][0] == symbol && board[1][1] == symbol && board[2][2] == symbol)
58 return true;
59 if (board[0][2] == symbol && board[1][1] == symbol && board[2][0] == symbol)
60 return true;
61
62 return false;
63 }
64
65 bool is_lose(Player<char>* player) override {
66 return false;
67 }
68
69 bool is_draw(Player<char>* player) override {
70 return (n_moves == 9 && !is_win(player));
71 }
72
73 bool game_is_over(Player<char>* player) override {
74 return is_win(player) || is_draw(player);
75 }
76};
77
78// XO Player
79class XO_Player : public Player<char> {
80public:
82};
83
84// XO Random Player
85class XO_Random_Player : public Player<char> {
86public:
87 XO_Random_Player(char symbol) : Player("Random Computer", symbol, PlayerType::COMPUTER) {
88 srand(time(0));
89 }
90};
91
92// XO UI
93class XO_UI : public UI<char> {
94public:
95 XO_UI() : UI("Welcome to Tic-Tac-Toe (XO)!", 3) {}
96
97 Move<char>* get_move(Player<char>* player) override {
98 if (player->get_type() == PlayerType::HUMAN) {
99 int x, y;
100 cout << player->get_name() << " (" << player->get_symbol()
101 << "), enter your move (row and column 0-2): ";
102 cin >> x >> y;
103 return new Move<char>(x, y, player->get_symbol());
104 }
105 else {
106 int x = rand() % 3;
107 int y = rand() % 3;
108 cout << player->get_name() << " chooses position (" << x << ", " << y << ")\n";
109 return new Move<char>(x, y, player->get_symbol());
110 }
111 }
112};
113
114#endif // XO_H
115
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
bool is_win(Player< char > *player) override
Check if a player has won.
Definition XO.h:41
bool is_draw(Player< char > *player) override
Check if the game ended in a draw.
Definition XO.h:69
bool game_is_over(Player< char > *player) override
Check if the game is over.
Definition XO.h:73
bool is_lose(Player< char > *player) override
Check if a player has lost.
Definition XO.h:65
XO_Board()
Definition XO.h:13
bool update_board(Move< char > *move) override
Update the board with a new move.
Definition XO.h:21
XO_Player(string name, char symbol)
Definition XO.h:81
XO_Random_Player(char symbol)
Definition XO.h:87
Move< char > * get_move(Player< char > *player) override
Ask the user (or AI) to make a move.
Definition XO.h:97
XO_UI()
Definition XO.h:95