-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
93 lines (78 loc) · 2.25 KB
/
Copy pathBoard.cpp
File metadata and controls
93 lines (78 loc) · 2.25 KB
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
#include "Board.h"
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <algorithm>
Board::Board(int width, int height) :
width(width), height(height),
edges(num_edges(), false),
decider(NULL)
{
score[0] = score[1] = 0;
}
bool Board::move(int player, Edge move)
{
assert(is_move_valid(move));
int oldscore = score[player];
edges[edge_index(move)] = true;
for_each_adjacent_node(move, [&] (Node node)
{ if (this->degree(node) == 4) ++score[player]; });
return oldscore != score[player];
}
void Board::unmove(int player, Edge move)
{
assert(!is_move_valid(move));
edges[edge_index(move)] = false;
for_each_adjacent_node(move, [&] (Node node)
{ if (this->degree(node) == 3) --score[player]; });
}
bool Board::is_move_valid(Edge move) const
{
if (move.dir == HORIZ) {
if (move.x < 0 || move.x >= width || move.y < 0 || move.y > height)
return false;
} else {
if (move.x < 0 || move.x > width || move.y < 0 || move.y >= height)
return false;
}
return !edges[edge_index(move)];
}
bool Board::is_game_over() const
{
return std::count(edges.begin(), edges.end(), false) == 0;
}
int Board::degree(Node node) const
{
int sum = 0;
for_each_adjacent_edge(node, [&] (Edge edge)
{ sum += (int) edges[this->edge_index(edge)]; });
return sum;
}
std::string basename_str(const std::string &str)
{
char *cpy = strdup(str.c_str());
char *base = basename(cpy);
std::string ret(base);
free(cpy);
return ret;
}
void Board::set_move_decider(const std::string &solver)
{
std::string base = basename_str(solver);
if (base == "random") {
decider = &Board::decide_move_random;
} else if (base == "first") {
decider = &Board::decide_move_first;
} else if (base == "invalid") {
decider = &Board::decide_move_invalid;
} else if (base == "timeout") {
decider = &Board::decide_move_timeout;
} else if (base == "crash") {
exit(1);
} else if (base == "nocheap") {
decider = &Board::decide_move_nocheap;
} else {
fprintf(stderr, "dots solver run with command: %s, cannot decide which move decider to use\n", base.c_str());
exit(1);
}
}