-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboard.cpp
115 lines (84 loc) · 2.15 KB
/
board.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "board.h"
#include <string>
using namespace std;
bool Cell::occupy(Player* p){
if (this->occupied) return false;
this->occupied = true;
this->playerOwned = p;
return true;
}
void Cell::free(){
this->occupied = false;
this->playerOwned = NULL;
}
Player* Cell::changePossession(Player* newPlayer){
Player* oldPlayer = this->playerOwned;
this->occupy(newPlayer);
return oldPlayer; // may be NULL
}
bool Cell::isOccupied()const{
return this->occupied;
}
bool Cell::belongsTo(const Player* p)const{
return this->playerOwned == p;
}
int Cell::getRow()const{
return this->row_coordinate;
}
int Cell::getColumn()const{
return this->column_coordinate;
}
Player* Cell::getPlayerOwned()const{
return playerOwned;
}
Board::Board(int size)
:size(size), occupiedCells(0){
table = vector< vector<Cell> >();
for (int i=0; i<size; i++){
table.push_back(vector<Cell>());
for (int j=0; j<size; j++){
table[i].push_back( Cell(i,j) );
}
}
}
Board::~Board(){}
int Board::getSize()const{ return size;}
bool Board::isFull()const{ return occupiedCells==size*size;}
bool Board::isEmpty()const{ return occupiedCells==0;}
bool Board::isCellOwnedBy(const int row, const int col, const Player *p){
return getCell(row,col).belongsTo(p);
}
bool Board::isCellOccupied(const int row, const int col) {
return getCell(row,col).isOccupied();
}
int Board::countStonesFor(const Player *p){
int count=0;
for (int row=0; row<size; row++){
for (int col=0; col<size; col++){
if (getCell(row,col).belongsTo(p)){
count++;
}
}
}
return count;
}
Player* Board::removeStone(const int row, const int col){
Cell& c = getCell(row,col);
Player* p = c.getPlayerOwned();
c.free();
return p;
}
bool Board::playAt(const int row, const int col, Player* p){
Cell& c = getCell(row,col);
if (c.isOccupied()){
return false;
}else{
c.occupy(p);
return true;
}
}
Cell& Board::getCell(const int row,const int col){
Cell& c = table[row][col];
//_assertNotNull(&c);
return c;
}