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
8class Numerical_UI : public UI<int> {
9public:
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
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
PlayerType
Represents the type of player in the game.
Definition BoardGame_Classes.h:24
@ COMPUTER
A computer-controlled player.
Definition BoardGame_Classes.h:26
Represents a single move in a board game.
Definition BoardGame_Classes.h:100
Definition Numerical_Player.h:4
Numerical_UI()
Definition Numerical_UI.h:10
Move< int > * get_move(Player< int > *p) override
Ask the user (or AI) to make a move.
Definition Numerical_UI.h:21
void display_board_matrix(const vector< vector< int > > &b) const
Definition Numerical_UI.h:12
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
Base template for all players (human or AI).
Definition BoardGame_Classes.h:126
PlayerType get_type() const
Get player type (e.g., 'H' or 'C').
Definition BoardGame_Classes.h:147
Board< T > * get_board_ptr() const
Get a pointer to the game board.
Definition BoardGame_Classes.h:153
string get_name() const
Get the player's name.
Definition BoardGame_Classes.h:144
UI(string message, int cell_display_width)
Definition BoardGame_Classes.h:196