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

Loading...
Searching...
No Matches
FourUI.h
Go to the documentation of this file.
1#pragma once
2#include "BoardGame_Classes.h"
3#include "FourBoard.h"
4#include <iostream>
5#include <random>
6#include <chrono>
7 class FourUI : public UI<char> {
8 public:
9 FourUI() : UI("Four-in-a-row (6x7)", 2) {}
10 void print_board(const vector<vector<char>>& b) {
11 cout << "\nColumns: ";
12 for (int c = 0; c < (int)b[0].size(); ++c) cout << c << ' ';
13 cout << "\n---------------------\n";
14 for (int r = 0; r < (int)b.size(); ++r) {
15 for (int c = 0; c < (int)b[r].size(); ++c) {
16 char ch = b[r][c];
17 cout << (ch == ' ' ? '.' : ch) << ' ';
18 }
19 cout << '\n';
20 }
21 cout << endl;
22 }
23 Move<char>* get_move(Player<char>* p) override {
24 FourBoard* bptr = dynamic_cast<FourBoard*>(p->get_board_ptr());
25 if (!bptr) {
26 int r, c; char s;
27 cin >> r >> c >> s;
28 return new Move<char>(r, c, s);
29 }
30
31 auto matrix = bptr->get_board_matrix();
32 print_board(matrix);
33
34 if (p->get_type() == PlayerType::COMPUTER) {
35
36 vector<int> validCols;
37 for (int c = 0; c < bptr->get_columns(); ++c) {
38 if (matrix[0][c] == ' ') validCols.push_back(c);
39 }
40 if (validCols.empty()) return new Move<char>(0, 0, p->get_symbol());
41 static std::mt19937 rng((unsigned)chrono::high_resolution_clock::now().time_since_epoch().count());
42 std::uniform_int_distribution<int> dist(0, (int)validCols.size() - 1);
43 int col = validCols[dist(rng)];
44 cout << "Computer " << p->get_name() << " plays column " << col << '\n';
45 return new Move<char>(0, col, p->get_symbol());
46 }
47
48 while (true) {
49 int col;
50 cout << p->get_name() << " enter column (0-" << (bptr->get_columns() - 1) << "): ";
51 if (!(cin >> col)) { cin.clear(); string garbage; getline(cin, garbage); cout << "Invalid input\n"; continue; }
52 if (col < 0 || col >= bptr->get_columns()) { cout << "Column out of range\n"; continue; }
53 if (matrix[0][col] != ' ') { cout << "Column full. Choose another.\n"; continue; }
54 return new Move<char>(0, col, p->get_symbol());
55 }
56 }
57
58 Player<char>* create_player(string& name, char symbol, PlayerType type) override {
59 return new Player<char>(name, symbol, type);
60 }
61};
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
FourBoard.h
Board::get_board_matrix
vector< vector< T > > get_board_matrix() const
Return a copy of the current board as a 2D vector.
Definition BoardGame_Classes.h:82
Board::get_columns
int get_columns() const
Get number of columns.
Definition BoardGame_Classes.h:90
FourBoard
Definition FourBoard.h:5
FourUI::get_move
Move< char > * get_move(Player< char > *p) override
Ask the user (or AI) to make a move.
Definition FourUI.h:23
FourUI::FourUI
FourUI()
Definition FourUI.h:9
FourUI::create_player
Player< char > * create_player(string &name, char symbol, PlayerType type) override
Create a player object based on input name, symbol, and type.
Definition FourUI.h:58
FourUI::print_board
void print_board(const vector< vector< char > > &b)
Definition FourUI.h:10
Move
Represents a single move in a board game.
Definition BoardGame_Classes.h:100
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
Player::get_symbol
T get_symbol() const
Get the player's symbol.
Definition BoardGame_Classes.h:150
UI< char >::UI
UI(string message, int cell_display_width)
Definition BoardGame_Classes.h:196