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

Loading...
Searching...
No Matches
Numerical_UI.h
Go to the documentation of this file.
1#pragma once
2#include "BoardGame_Classes.h"
3#include "Numerical_Player.h"
4#include "Numerical_Board.h"
5#include <cstdlib>
6#include <ctime>
7
8 class Numerical_UI : public UI<int> {
9 public:
10 Numerical_UI() : UI<int>("Numerical Tic Tac Toe", 2) {}
11
12 void display_board_matrix(const vector<vector<int>>& b) const {
13 cout << "\nBoard:\n";
14 for (auto& r : b) {
15 for (int c : r)
16 cout << (c == 0 ? '.' : char('0' + c)) << " ";
17 cout << endl;
18 }
19 }
20
21 Move<int>* get_move(Player<int>* p) override {
22 auto np = (Numerical_Player*)p;
23 int x, y, val;
24
25 // Computer move
26 if (p->get_type() == PlayerType::COMPUTER) {
27 srand(time(0));
28 do { x = rand() % 3; y = rand() % 3; } while (p->get_board_ptr()->get_board_matrix()[x][y] != 0);
29 val = np->isOdd ? (rand() % 5) * 2 + 1 : (rand() % 4) * 2 + 2;
30 cout << "\nComputer played " << val << " at (" << x << "," << y << ")\n";
31 return new Move<int>(x, y, val);
32 }
33
34 // Human move
35 while (true) {
36 cout << "\n" << p->get_name() << " enter row and column (0-2): ";
37 cin >> x >> y;
38 cout << "Enter a " << (np->isOdd ? "odd" : "even") << " number: ";
39 cin >> val;
40
41 if (x < 0 || x > 2 || y < 0 || y > 2) { cout << "Invalid cell.\n"; continue; }
42 if (np->isOdd && val % 2 == 0) { cout << "You must use odd numbers.\n"; continue; }
43 if (!np->isOdd && val % 2 != 0) { cout << "You must use even numbers.\n"; continue; }
44 return new Move<int>(x, y, val);
45 }
46 }
47
48 Player<int>* create_player(string& n, int symbol, PlayerType t) override {
49 return new Numerical_Player(n, true, t);
50 }
51};
52
53
54
BoardGame_Classes.h
PlayerType
PlayerType
Represents the type of player in the game.
Definition BoardGame_Classes.h:24
PlayerType::COMPUTER
@ COMPUTER
A computer-controlled player.
Definition BoardGame_Classes.h:26
Numerical_Board.h
Numerical_Player.h
Move
Represents a single move in a board game.
Definition BoardGame_Classes.h:100
Numerical_Player
Definition Numerical_Player.h:4
Numerical_UI::Numerical_UI
Numerical_UI()
Definition Numerical_UI.h:10
Numerical_UI::get_move
Move< int > * get_move(Player< int > *p) override
Ask the user (or AI) to make a move.
Definition Numerical_UI.h:21
Numerical_UI::display_board_matrix
void display_board_matrix(const vector< vector< int > > &b) const
Definition Numerical_UI.h:12
Numerical_UI::create_player
Player< int > * create_player(string &n, int symbol, PlayerType t) override
Create a player object based on input name, symbol, and type.
Definition Numerical_UI.h:48
Player
Base template for all players (human or AI).
Definition BoardGame_Classes.h:126
Player::get_type
PlayerType get_type() const
Get player type (e.g., 'H' or 'C').
Definition BoardGame_Classes.h:147
Player::get_board_ptr
Board< T > * get_board_ptr() const
Get a pointer to the game board.
Definition BoardGame_Classes.h:153
Player::get_name
string get_name() const
Get the player's name.
Definition BoardGame_Classes.h:144
UI< int >::UI
UI(string message, int cell_display_width)
Definition BoardGame_Classes.h:196