-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard_state.js
130 lines (116 loc) · 3.34 KB
/
board_state.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var Board = function(size) {
this.EMPTY = 0;
this.P1 = 1;
this.P2 = 2;
this.state = {};
var adjacent_cache;
this.index = function(x, y) {
if (x < 0 || x >= this.state.size || y < 0 || y >= this.state.size)
return -1;
return x + y * this.state.size;
}
this.doMove = function(begin_index, end_index, delta) {
var new_board = new Board(this.state.size);
for (var i = 0; i < this.state.square_count; i++) {
new_board.state.board_array[i] = this.state.board_array[i];
}
for (var i = begin_index; i != (end_index + delta); i += delta) {
new_board.state.board_array[i] = this.state.player;
}
new_board.state.player = this.opposingPlayer();
new_board.state.last_move = begin_index;
return new_board;
}
this.create = function(size) {
this.state.score = 0;
this.state.size = size;
this.state.square_count = size * size;
this.state.board_array = new Uint8Array(this.state.square_count);
this.state.player = this.P1;
if (!adjacent_cache) {
adjacent_cache = new Array(size * size);
for (var x = 0; x < size; x++) {
for (var y = 0; y < size; y++) {
var i = this.index(x, y);
adjacent_cache[i] = new Array();
var add_adjacent = function(a) {
if (a >= 0 && a < adjacent_cache.length) {
adjacent_cache[i].push(a);
}
}
add_adjacent(this.index(x - 1, y - 1));
add_adjacent(this.index(x, y - 1));
add_adjacent(this.index(x + 1, y - 1));
add_adjacent(this.index(x - 1, y));
add_adjacent(this.index(x + 1, y));
add_adjacent(this.index(x - 1, y + 1));
add_adjacent(this.index(x, y + 1));
add_adjacent(this.index(x + 1, y + 1));
}
}
}
}
this.size = function() { return this.state.size; }
this.square_count = function() { return this.state.square_count; }
this.data = function() { return this.state.board_array; }
this.set_data = function(ba) {
this.state.board_array = ba;
}
this.set = function(x, y, val) {
var i = this.index(x, y);
if (i < 0) {
console.log("out of bounds", x, y, this.size);
return;
}
if (val < this.EMPTY || val > this.P2) {
console.log("illegal value", val);
return;
}
this.state.board_array[i] = val;
}
// if one arg, x is treated as index
this.get = function(x, y) {
var i;
if (y == undefined)
i = x;
else
i = this.index(x, y);
if (i < 0) {
console.log("out of bounds", x, y, this.size);
return;
}
return this.state.board_array[i];
}
this.counts = function() {
var c = [0, 0, 0];
for (var i = 0; i < this.state.board_array.length; i++) {
var val = this.state.board_array[i];
if (val <= this.EMPTY)
c[0]++;
else if (val == this.P1)
c[1]++;
else if (val == this.P2)
c[2]++;
else
console.log("illegal value in board", i, val);
}
return c;
}
this.score = function() { return this.state.score; }
this.setScore = function(s) { this.state.score = s; }
this.currentPlayer = function() {
return this.state.player;
}
this.opposingPlayer = function() {
return this.state.player == this.P1 ? this.P2 : this.P1;
}
this.adjacent = function(x,y) {
var i = this.index(x, y);
if (i < 0) {
console.log("out of bounds", x, y, this.state.size);
return;
}
return adjacent_cache[i];
}
this.create(size);
};