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

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