-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
63 lines (50 loc) · 1.48 KB
/
game.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
function Game(_board) {
this.inheritFrom = BoardUtilities;
this.inheritFrom(); //Inheritance, yay
this.moves = new Array(); //array of Moves
this.board = _board;
this.nextMove = function(){
return this.moves.size()+1;
}
this.nextPlayer = function(){
return ((this.nextMove()-1)%2);
}
//coords - location played
//Return - update hash
this.playNextMove = function(coords){
var player = this.nextPlayer();
var move = new Move(coords,player);
this.stones().set(coords,move);
move.captures = this.findCaptures(coords,player);
this.moves.push(move);
updateHash = new Hash();
updateHash.set(coords,player);
this.applyUpdatesToStones(updateHash);
return updateHash;
}
this.findCaptures = function(_coords, _player){
var calc = new CaptureCalculator(this.stones());
return calc.capturesForMove(_coords,_player);
}
this.undo = function(){
var lastMove = this.moves.pop();
updateHash = new Hash();
updateHash.set(lastMove.coords,-1);
return updateHash;
}
this.sgf = function(){
var str = "";
return this.moves.inject('(',function(str,move,index) {
var coords = move.coords
var letters = 'abcdefghijklmnopqrs'.toArray();
var player = 'B';
if(index%2 == 1){player = 'W'};
return str+';'+player+'['+letters[coords[0]]+letters[coords[1]]+']'
})+')';
}
}
function Move(_coords,_player){
this.coords = _coords;
this.player = _player;
this.removed = new Array(); //an array of coords of removed stones that move
}