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

Loading...
Searching...
No Matches
FourBoard.h
Go to the documentation of this file.
1#pragma once
2#include "BoardGame_Classes.h"
3#include <vector>
4
5 class FourBoard : public Board<char> {
6 public:
7 FourBoard() : Board<char>(6, 7) {
8 board = vector<vector<char>>(6, vector<char>(7, ' '));
9 }
10
11 // Update: move->get_y() is the column chosen. We drop to lowest empty row.
12 bool update_board(Move<char>* m) override {
13 int col = m->get_y();
14 char sym = m->get_symbol();
15
16 if (col < 0 || col >= columns) return false;
17
18 // find lowest empty row (largest row index)
19 for (int r = rows - 1; r >= 0; --r) {
20 if (board[r][col] == ' ') {
21 board[r][col] = sym;
22 n_moves++;
23 return true;
24 }
25 }
26 // column full
27 return false;
28 }
29
30 bool is_win(Player<char>* p) override {
31 char sym = p->get_symbol();
32 // check 4-in-a-row in all directions
33 // horizontal
34 for (int r = 0; r < rows; ++r) {
35 for (int c = 0; c + 3 < columns; ++c) {
36 if (board[r][c] == sym && board[r][c + 1] == sym && board[r][c + 2] == sym && board[r][c + 3] == sym)
37 return true;
38 }
39 }
40 // vertical
41 for (int c = 0; c < columns; ++c) {
42 for (int r = 0; r + 3 < rows; ++r) {
43 if (board[r][c] == sym && board[r + 1][c] == sym && board[r + 2][c] == sym && board[r + 3][c] == sym)
44 return true;
45 }
46 }
47 // diagonal down-right
48 for (int r = 0; r + 3 < rows; ++r) {
49 for (int c = 0; c + 3 < columns; ++c) {
50 if (board[r][c] == sym && board[r + 1][c + 1] == sym && board[r + 2][c + 2] == sym && board[r + 3][c + 3] == sym)
51 return true;
52 }
53 }
54 // diagonal up-right
55 for (int r = 3; r < rows; ++r) {
56 for (int c = 0; c + 3 < columns; ++c) {
57 if (board[r][c] == sym && board[r - 1][c + 1] == sym && board[r - 2][c + 2] == sym && board[r - 3][c + 3] == sym)
58 return true;
59 }
60 }
61 return false;
62 }
63
64 bool is_draw(Player<char>* p) override {
65 // draw if board full and no winner
66 for (int r = 0; r < rows; ++r)
67 for (int c = 0; c < columns; ++c)
68 if (board[r][c] == ' ')
69 return false;
70 return true;
71 }
72
73 bool is_lose(Player<char>* p) override { return false; }
74
75 bool game_is_over(Player<char>* p) override {
76 return is_win(p) || is_draw(p);
77 }
78};
BoardGame_Classes.h
Board< char >::n_moves
int n_moves
Definition BoardGame_Classes.h:45
Board< char >::rows
int rows
Definition BoardGame_Classes.h:42
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
Board< char >::columns
int columns
Definition BoardGame_Classes.h:43
FourBoard::update_board
bool update_board(Move< char > *m) override
Update the board with a new move.
Definition FourBoard.h:12
FourBoard::game_is_over
bool game_is_over(Player< char > *p) override
Check if the game is over.
Definition FourBoard.h:75
FourBoard::is_win
bool is_win(Player< char > *p) override
Check if a player has won.
Definition FourBoard.h:30
FourBoard::is_lose
bool is_lose(Player< char > *p) override
Check if a player has lost.
Definition FourBoard.h:73
FourBoard::FourBoard
FourBoard()
Definition FourBoard.h:7
FourBoard::is_draw
bool is_draw(Player< char > *p) override
Check if the game ended in a draw.
Definition FourBoard.h:64
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::get_symbol
T get_symbol() const
Get the player's symbol.
Definition BoardGame_Classes.h:150