-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBoardState.js
229 lines (211 loc) · 7.61 KB
/
BoardState.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
let
BOARDSIZE = 8,
red = 1,
black = -1,
redKing = 1.1,
blackKing = -1.1;
class BoardState {
constructor(board) {
if (board == null)
this.board = [
[red, 0, red, 0, red, 0, red, 0],
[0, red, 0, red, 0, red, 0, red],
[red, 0, red, 0, red, 0, red, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, black, 0, black, 0, black, 0, black],
[black, 0, black, 0, black, 0, black, 0],
[0, black, 0, black, 0, black, 0, black]
]
else this.board = board
// this.turn = black;
}
getBoard() {
return this.board;
}
// makes a deep copy of a given array
static copy(board) {
return new BoardState(JSON.parse(JSON.stringify(board)))
}
static doAction(action,board){
// var distance = {
// x: to.x - from.x,
// y: to.y - from.y
// };
if(Math.abs(action[1].x-action[0].x)==2){
let jumpedPiece=BoardState.getJumpedPiece(action[0],action[1])
board[jumpedPiece[0]][jumpedPiece[1]]=0
}
let valueAt=board[action[0].x][action[0].y]
board[action[0].x][action[0].y]=0
board[action[1].x][action[1].y]=valueAt
}
static winner(board) {
let redN = 0,
blackN = 0;
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
if (parseInt(board[i][j]) == red)
redN++
else if (parseInt(board[i][j]) == black)
blackN++
}
}
if (redN == 0)
return black
else if (blackN == 0)
return red
else if (blackN > 0 && redN > 0) {
return 0
}
return 0
}
static getLegalActions(player,board) {
let singleActions = [
[1, 1],
[-1, -1],
[-1, 1],
[1, -1]
],
doubleActions = [
[2, 2],
[-2, -2],
[-2, 2],
[2, -2]
],
legalPositions = []
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
let offset = 0;
player == 1 ? offset = .1 : offset = -.1;
if (board[i][j] === player || board[i][j] === (player + offset)) {
for (let a = 0; a < singleActions.length; a++) {
if (BoardState.isMoveLegal({
x: i,
y: j
}, {
x: i + singleActions[a][0],
y: j + singleActions[a][1]
}, null, board)) {
legalPositions.push([{
x: i,
y: j
}, {
x: i + singleActions[a][0],
y: j + singleActions[a][1]
}])
}
}
for (let a = 0; a < doubleActions.length; a++) {
if (BoardState.isMoveLegal({
x: i,
y: j
}, {
x: i + doubleActions[a][0],
y: j + doubleActions[a][1]
}, null,board)) {
legalPositions.push([{
x: i,
y: j
}, {
x: i + doubleActions[a][0],
y: j + doubleActions[a][1]
}])
}
}
}
}
}
return legalPositions
}
// nextTurn() {
// if (this.turn == red || this.turn == redKing)
// this.turn = black
// else
// this.turn = red;
// }
static getJumpedPiece(from, to) {
var distance = {
x: to.x - from.x,
y: to.y - from.y
};
if (Math.abs(distance.x) == 2) {
var jumpy = parseInt(from.y) + Math.sign(distance.y);
var jumpx = parseInt(from.x) + Math.sign(distance.x);
return [jumpx, jumpy];
} else return null;
}
static isMoveLegal(from, to, game,board) {
if ((to.x < 0) || (to.y < 0) || (to.x > 7) || (to.y > 7)) {
return false;
}
let piece = board[to.x][to.y],
removeMiddlePiece = false,
jumpedPieceIndex;
var distance = {
x: to.x - from.x,
y: to.y - from.y
};
if ((distance.x == 0) || (distance.y == 0)) {
//console.log("ILLEGAL MOVE: horizontal or vertical move");
return false;
}
if (Math.abs(distance.x) != Math.abs(distance.y)) {
//console.log("ILLEGAL MOVE: non-diagonal move");
return false;
}
if (Math.abs(distance.x) > 2) {
//console.log("ILLEGAL MOVE: more than two diagonals");
return false;
}
if (board[to.x][to.y] != 0) {
//console.log("ILLEGAL MOVE: cell is not empty");
return false;
}
if (Math.abs(distance.x) == 2) {
jumpedPieceIndex = BoardState.getJumpedPiece(from, to);
// board[jumpedPieceIndex[0]][jumpedPieceIndex[1]]=0
var jumpedPiece = board[jumpedPieceIndex[0]][jumpedPieceIndex[1]]
if (jumpedPiece == null) {
//console.log("ILLEGAL MOVE: no piece to jump");
return false;
}
var pieceState = parseInt(board[from.x][from.y]);
var jumpedState = parseInt(jumpedPiece);
if (pieceState != -jumpedState) {
//console.log("ILLEGAL MOVE: can't jump own piece");
return false;
}
removeMiddlePiece = true;
}
if ((parseInt(board[from.x][from.y]) === board[from.x][from.y]) && (Math.sign(board[from.x][from.y]) != Math.sign(distance.x))) {
//console.log("ILLEGAL MOVE: wrong direction");
return false;
}
// if (this.turn != parseInt(this.board[from.x][from.y])) {
// console.log("ILLEGAL TURN: wrong player")
// return false;
// }
// if (to.x == 0 || to.x == 7) {
// if (board[from.x][from.y] == red) {
// board[from.x][from.y] = redKing
// if (game != null)
// game.setKing([from.x, from.y], 1)
// } else if (board[from.x][from.y] == black) {
// board[from.x][from.y] = blackKing
// if (game != null)
// game.setKing([from.x, from.y], -1)
// }
// }
if (removeMiddlePiece && game != null) {
// board[jumpedPieceIndex[0]][jumpedPieceIndex[1]]=0
game.removeMiddlePiece(jumpedPieceIndex)
}
// if (game != null)
// this.nextTurn()
return true
}
// getTurn() {
// return this.turn
// }
}