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

Loading...
Searching...
No Matches
SUSBoard.h
Go to the documentation of this file.
1#pragma once
2#include "BoardGame_Classes.h"
3
4 class SUSBoard : public Board<char> {
5 private:
6 int score_x, score_o;
7
8 public:
9 SUSBoard() : Board<char>(3, 3) {
10 for (int i = 0; i < 3; i++)
11 for (int j = 0; j < 3; j++)
12 board[i][j] = ' ';
13 score_x = score_o = 0;
14 }
15
16 int count_xux() {
17 int c = 0;
18
19 for (int r = 0; r < 3; r++)
20 if (board[r][0] == 'X' && board[r][1] == 'O' && board[r][2] == 'X')
21 c++;
22
23 for (int col = 0; col < 3; col++)
24 if (board[0][col] == 'X' && board[1][col] == 'O' && board[2][col] == 'X')
25 c++;
26
27 if (board[0][0] == 'X' && board[1][1] == 'O' && board[2][2] == 'X') c++;
28 if (board[0][2] == 'X' && board[1][1] == 'O' && board[2][0] == 'X') c++;
29
30 return c;
31 }
32
33 bool update_board(Move<char>* m) override {
34 int x = m->get_x(), y = m->get_y();
35 char sym = m->get_symbol();
36
37 if (x < 0 || x >= 3 || y < 0 || y >= 3) return false;
38 if (board[x][y] != ' ') return false;
39
40 board[x][y] = sym;
41
42 int s = count_xux();
43 score_x = s;
44 score_o = s;
45
46 n_moves++;
47 return true;
48 }
49
50 bool full() {
51 return n_moves == 9;
52 }
53
54 bool is_win(Player<char>* p) override {
55 if (!full()) return false;
56 if (p->get_symbol() == 'X' && score_x > score_o) return true;
57 if (p->get_symbol() == 'O' && score_o > score_x) return true;
58 return false;
59 }
60
61 bool is_lose(Player<char>* p) override {
62 if (!full()) return false;
63 if (p->get_symbol() == 'X' && score_o > score_x) return true;
64 if (p->get_symbol() == 'O' && score_x > score_o) return true;
65 return false;
66 }
67
68 bool is_draw(Player<char>* p) override {
69 return full() && score_x == score_o;
70 }
71
72 bool game_is_over(Player<char>* p) override {
73 return full();
74 }
75
76 vector<vector<char>> get_board_matrix() const {
77 return board;
78 }
79
80};
81
82
83
84
BoardGame_Classes.h
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
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
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
SUSBoard::SUSBoard
SUSBoard()
Definition SUSBoard.h:9
SUSBoard::count_xux
int count_xux()
Definition SUSBoard.h:16
SUSBoard::full
bool full()
Definition SUSBoard.h:50
SUSBoard::update_board
bool update_board(Move< char > *m) override
Update the board with a new move.
Definition SUSBoard.h:33
SUSBoard::get_board_matrix
vector< vector< char > > get_board_matrix() const
Definition SUSBoard.h:76
SUSBoard::is_lose
bool is_lose(Player< char > *p) override
Check if a player has lost.
Definition SUSBoard.h:61
SUSBoard::is_draw
bool is_draw(Player< char > *p) override
Check if the game ended in a draw.
Definition SUSBoard.h:68
SUSBoard::game_is_over
bool game_is_over(Player< char > *p) override
Check if the game is over.
Definition SUSBoard.h:72
SUSBoard::is_win
bool is_win(Player< char > *p) override
Check if a player has won.
Definition SUSBoard.h:54