-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
105 lines (84 loc) · 2.13 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
#include "Board.h"
#include "Square.h"
#include "Position.h"
#include <iostream>
using namespace std;
void allocateRelatedSquares(Square** squares) {
vector<Square> horizontallyRelatedSquares;
vector<Square> verticallyRelatedSquares;
for (int i=0 ; i<3 ; i++) {
for (int j=0 ; j<3 ; j++) {
// set the vertically related squares
for (int k=0 ; k<3 ; k++) {
if (i != k) {
verticallyRelatedSquares.push_back(squares[k][j]);
}
}
// set the horizontally reltated squares
for (int m=0 ; m<3 ; m++) {
if (j != m) {
horizontallyRelatedSquares.push_back(squares[i][m]);
}
}
squares[i][j].setVerticallyRelatedSquares(verticallyRelatedSquares);
squares[i][j].setHorizontallyRelatedSquares(horizontallyRelatedSquares);
verticallyRelatedSquares.clear();
horizontallyRelatedSquares.clear();
}
}
}
Board::Board(int** State) :
state(State)
{
squares = new Square*[3];
for (int i=0 ; i<3 ; i++) {
squares[i] = new Square[3];
for (int j=0 ; j<3 ; j++) {
squares[i][j].setPosition(Position(i,j));
for (int k=0 ; k<3 ; k++) {
for (int m=0 ; m<3 ; m++) {
squares[i][j].data[k][m] = state[i*3 + k][j*3 + m];
}
}
}
}
allocateRelatedSquares(squares);
}
int** Board::getState() {
for (int i=0 ; i<3 ; i++) {
for (int j=0 ; j<3 ; j++) {
for (int k=0 ; k<3 ; k++) {
for (int m=0 ; m<3 ; m++) {
state[i*3 + k][j*3 + m] = squares[i][j].data[k][m];
}
}
}
}
return state;
}
int Board::countEmpties() {
int count = 0;
for (int i=0 ; i<3 ; i++) {
for (int j=0 ; j<3 ; j++) {
for (int k=0 ; k<3 ; k++) {
for (int m=0 ; m<3 ; m++) {
if (squares[i][j].data[k][m] == 0)
count++;
}
}
}
}
return count;
}
void Board::reassignState(int** state_) {
state = state_;
for (int i=0 ; i<3 ; i++) {
for (int j=0 ; j<3 ; j++) {
for (int k=0 ; k<3 ; k++) {
for (int m=0 ; m<3 ; m++) {
squares[i][j].data[k][m] = state[i*3 + k][j*3 + m];
}
}
}
}
}