-
Notifications
You must be signed in to change notification settings - Fork 2
/
Board.cpp
107 lines (97 loc) · 2.22 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
#include "./includes/Board.h"
Board::Board() {
board.resize(3, vector<int>(3));
}
Board::Board(vector<vector<int>> other) {
assert(other.size() == (size_t) 3);
assert(other[0].size() == (size_t) 3);
board = other;
}
pair<int, int> Board::get_free_tile() {
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
if(board[i][j] == 0) {
return pair<int, int>(i, j);
}
}
}
return pair<int, int>{-1, -1};
}
int Board::getHval(const Board& final) {
int h = 0;
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
if(board[i][j] != final.board[i][j]) h++;
}
}
return h;
}
vector<Board> Board::getNeigbors() {
pair<int, int> free = get_free_tile();
assert(free.first != -1);
vector<Board> neighbors;
for(int iter = 0; iter < 4; ++iter) {
int x = free.first + dx[iter], y = free.second + dy[iter];
if(x < 0 || x >= 3 || y < 0 || y >= 3) {
continue;
}
std::swap(board[x][y], board[free.first][free.second]);
neighbors.push_back(Board(board));
std::swap(board[x][y], board[free.first][free.second]);
}
return neighbors;
}
bool operator== (Board b, Board other) {
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
if(b.board[i][j] != other.board[i][j]) {
return false;
}
}
}
return true;
}
u64 Board::get_hash() {
u64 hash = 0;
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
hash = hash * 10 + board[i][j];
}
}
return hash;
}
Board get_from_hash(u64 hash) {
vector<vector<int>> b(3, vector<int>(3, 0));
for(int i = 2; i >= 0; --i) {
for(int j = 2; j >= 0; --j) {
b[i][j] = hash % 10;
hash /= 10;
}
}
return Board(b);
}
std::ostream& operator << (std::ostream& out, const Board& b) {
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 7; ++j) {
out << "- ";
}
out << std::endl;
out << "| ";
for(int j = 0; j < 3; ++j) {
out << b.board[i][j] << " | ";
}
out << std::endl;
}
for(int j = 0; j < 7; ++j) {
out << "- ";
}
return out;
}
std::istream& operator >> (std::istream& in, Board &b) {
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
in >> b.board[i][j];
}
}
return in;
}