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

Loading...
Searching...
No Matches
Numerical_Board.h
Go to the documentation of this file.
1#pragma once
2#include "BoardGame_Classes.h"
3
4 class Numerical_Board : public Board<int> {
5 public:
6 Numerical_Board() : Board(3, 3) {
7 for (auto& r : board) fill(r.begin(), r.end(), 0);
8 }
9
10 bool update_board(Move<int>* m) override {
11 int x = m->get_x(), y = m->get_y(), val = m->get_symbol();
12 if (x < 0 || x >= 3 || y < 0 || y >= 3 || board[x][y] != 0)
13 return false;
14 board[x][y] = val;
15 return true;
16 }
17
18 bool is_win(Player<int>* p) override {
19 for (int i = 0; i < 3; i++) {
20 if (board[i][0] + board[i][1] + board[i][2] == 15) return true;
21 if (board[0][i] + board[1][i] + board[2][i] == 15) return true;
22 }
23 if (board[0][0] + board[1][1] + board[2][2] == 15) return true;
24 if (board[0][2] + board[1][1] + board[2][0] == 15) return true;
25 return false;
26 }
27
28 bool is_draw(Player<int>* p) override {
29 for (auto& r : board)
30 for (int c : r)
31 if (c == 0) return false;
32 return !is_win(p);
33 }
34
35 bool is_lose(Player<int>* p) override { return false; }
36
37 bool game_is_over(Player<int>* p) override {
38 return is_win(p) || is_draw(p);
39 }
40};
BoardGame_Classes.h
Board< int >::Board
Board(int rows, int columns)
Definition BoardGame_Classes.h:51
Board< int >::board
vector< vector< int > > board
Definition BoardGame_Classes.h:44
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
Move::get_x
int get_x() const
Get row index.
Definition BoardGame_Classes.h:110
Numerical_Board::is_lose
bool is_lose(Player< int > *p) override
Check if a player has lost.
Definition Numerical_Board.h:35
Numerical_Board::game_is_over
bool game_is_over(Player< int > *p) override
Check if the game is over.
Definition Numerical_Board.h:37
Numerical_Board::Numerical_Board
Numerical_Board()
Definition Numerical_Board.h:6
Numerical_Board::is_draw
bool is_draw(Player< int > *p) override
Check if the game ended in a draw.
Definition Numerical_Board.h:28
Numerical_Board::is_win
bool is_win(Player< int > *p) override
Check if a player has won.
Definition Numerical_Board.h:18
Numerical_Board::update_board
bool update_board(Move< int > *m) override
Update the board with a new move.
Definition Numerical_Board.h:10
Player
Base template for all players (human or AI).
Definition BoardGame_Classes.h:126