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

Loading...
Searching...
No Matches
FourInARow.h
Go to the documentation of this file.
1#ifndef FOURINAROW_H
2#define FOURINAROW_H
3
4#include "BoardGame_Classes.h"
5#include <iostream>
6#include <cstdlib>
7#include <ctime>
8 using namespace std;
9
10// Four in a Row Board
11 class FourInARowBoard : public Board<char> {
12 private:
13 int get_lowest_empty_row(int col) {
14 for (int row = 0; row < 6; row++) {
15 if (board[row][col] == ' ') {
16 return row;
17 }
18 }
19 return -1;
20 }
21
22 public:
23 FourInARowBoard() : Board(6, 7) {
24 for (int i = 0; i < 6; i++) {
25 for (int j = 0; j < 7; j++) {
26 board[i][j] = ' ';
27 }
28 }
29 }
30
31 bool update_board(Move<char>* move) override {
32 int col = move->get_y();
33 char symbol = move->get_symbol();
34
35 if (col < 0 || col >= 7) {
36 cout << "Invalid column! Choose 0-6.\n";
37 return false;
38 }
39
40 int row = get_lowest_empty_row(col);
41 if (row == -1) {
42 cout << "Column " << col << " is full!\n";
43 return false;
44 }
45
46 board[row][col] = symbol;
47 n_moves++;
48 return true;
49 }
50
51 bool is_win(Player<char>* player) override {
52 char symbol = player->get_symbol();
53
54 // Horizontal
55 for (int row = 0; row < 6; row++) {
56 for (int col = 0; col < 4; col++) {
57 if (board[row][col] == symbol &&
58 board[row][col + 1] == symbol &&
59 board[row][col + 2] == symbol &&
60 board[row][col + 3] == symbol)
61 return true;
62 }
63 }
64
65 // Vertical
66 for (int col = 0; col < 7; col++) {
67 for (int row = 0; row < 3; row++) {
68 if (board[row][col] == symbol &&
69 board[row + 1][col] == symbol &&
70 board[row + 2][col] == symbol &&
71 board[row + 3][col] == symbol)
72 return true;
73 }
74 }
75
76 // Diagonal down-right
77 for (int row = 0; row < 3; row++) {
78 for (int col = 0; col < 4; col++) {
79 if (board[row][col] == symbol &&
80 board[row + 1][col + 1] == symbol &&
81 board[row + 2][col + 2] == symbol &&
82 board[row + 3][col + 3] == symbol)
83 return true;
84 }
85 }
86
87 // Diagonal down-left
88 for (int row = 0; row < 3; row++) {
89 for (int col = 3; col < 7; col++) {
90 if (board[row][col] == symbol &&
91 board[row + 1][col - 1] == symbol &&
92 board[row + 2][col - 2] == symbol &&
93 board[row + 3][col - 3] == symbol)
94 return true;
95 }
96 }
97
98 return false;
99 }
100
101 bool is_lose(Player<char>* player) override {
102 return false;
103 }
104
105 bool is_draw(Player<char>* player) override {
106 for (int col = 0; col < 7; col++) {
107 if (board[5][col] == ' ') {
108 return false;
109 }
110 }
111 return !is_win(player);
112 }
113
114 bool game_is_over(Player<char>* player) override {
115 return is_win(player) || is_draw(player);
116 }
117};
118
119// Four in a Row Player
120 class FourInARowPlayer : public Player<char> {
121 public:
122 FourInARowPlayer(string name, char symbol) : Player(name, symbol, PlayerType::HUMAN) {}
123};
124
125// Four in a Row Random Player
126 class FourInARowRandomPlayer : public Player<char> {
127 public:
128 FourInARowRandomPlayer(char symbol) : Player("Random Computer", symbol, PlayerType::COMPUTER) {
129 srand(time(0));
130 }
131};
132
133// Four in a Row UI
134 class FourInARowUI : public UI<char> {
135 public:
136 FourInARowUI() : UI("Welcome to Four-in-a-Row!", 3) {}
137
138 Move<char>* get_move(Player<char>* player) override {
139 if (player->get_type() == PlayerType::HUMAN) {
140 int col;
141 cout << player->get_name() << " (" << player->get_symbol()
142 << "), enter column (0-6): ";
143 cin >> col;
144 return new Move<char>(0, col, player->get_symbol());
145 }
146 else {
147 int col = rand() % 7;
148 cout << player->get_name() << " chooses column " << col << "\n";
149 return new Move<char>(0, col, player->get_symbol());
150 }
151 }
152};
153
154#endif // FOURINAROW_H
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< char >::n_moves
int n_moves
Definition BoardGame_Classes.h:45
Board< char >::Board
Board(int rows, int columns)
Definition BoardGame_Classes.h:51
Board< char >::board
vector< vector< char > > board
Definition BoardGame_Classes.h:44
FourInARowBoard::is_lose
bool is_lose(Player< char > *player) override
Check if a player has lost.
Definition FourInARow.h:101
FourInARowBoard::game_is_over
bool game_is_over(Player< char > *player) override
Check if the game is over.
Definition FourInARow.h:114
FourInARowBoard::is_draw
bool is_draw(Player< char > *player) override
Check if the game ended in a draw.
Definition FourInARow.h:105
FourInARowBoard::FourInARowBoard
FourInARowBoard()
Definition FourInARow.h:23
FourInARowBoard::update_board
bool update_board(Move< char > *move) override
Update the board with a new move.
Definition FourInARow.h:31
FourInARowBoard::is_win
bool is_win(Player< char > *player) override
Check if a player has won.
Definition FourInARow.h:51
FourInARowPlayer::FourInARowPlayer
FourInARowPlayer(string name, char symbol)
Definition FourInARow.h:122
FourInARowRandomPlayer::FourInARowRandomPlayer
FourInARowRandomPlayer(char symbol)
Definition FourInARow.h:128
FourInARowUI::get_move
Move< char > * get_move(Player< char > *player) override
Ask the user (or AI) to make a move.
Definition FourInARow.h:138
FourInARowUI::FourInARowUI
FourInARowUI()
Definition FourInARow.h:136
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
Player
Base template for all players (human or AI).
Definition BoardGame_Classes.h:126
Player< char >::symbol
char 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< char >::Player
Player(string n, char 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< char >::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< char >::UI
UI(string message, int cell_display_width)
Definition BoardGame_Classes.h:196