-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (42 loc) · 1.15 KB
/
index.js
File metadata and controls
54 lines (42 loc) · 1.15 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
module.exports = class Qi {
constructor(inHand, square) {
this.inHand = inHand;
this.square = square;
}
moveToActions(moveArr) {
var actions = [];
for(var i = 0; i < moveArr.length; i += 4) {
actions.push(moveArr.slice(i, i + 4));
}
return actions;
}
play(move) {
var nextInHand = this.inHand.concat();
var square = this.square;
var nextSquare = {};
Object.keys(square).forEach(function(key) {
nextSquare[key] = square[key];
});
var actions = this.moveToActions(move);
actions.forEach(function(action) {
var srcSquareId = action[0];
var dstSquareId = action[1];
var movedPieceName = action[2];
var capturedPieceName = action[3];
if (srcSquareId === null) {
var inHandId = nextInHand.indexOf(movedPieceName);
if (inHandId !== -1)
nextInHand.splice(inHandId, 1);
} else {
delete nextSquare[srcSquareId];
}
nextSquare[dstSquareId] = movedPieceName;
if (capturedPieceName)
nextInHand.push(capturedPieceName);
});
return new Qi(
nextInHand,
nextSquare
);
}
};