forked from ismyrnow/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathish.go.js
156 lines (145 loc) · 3.73 KB
/
ish.go.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
window.onload = function() {
Ish.Go.View.init();
}
var gGameState;
var Constants = new function() {
this.Color = {
BLACK : "black",
WHITE : "white"
};
this.Direction = {
NORTH : "north",
EAST : "east",
SOUTH : "south",
WEST : "west",
ALL : ["north", "east", "south", "west"]
};
this.PointState = {
EMPTY : ".",
BLACK : "X",
WHITE : "O"
};
this.MoveError = {
REPEAT : "The attempted move would result in a repeated board state.",
OCCUPIED : "The selected intersection is occupied.",
SUICIDE : "The attepted move would result in a suicide."
};
this.TerritoryOwner = {
UNKNOWN : this.PointState.EMPTY,
NEUTRAL : "-",
BLACK : this.PointState.BLACK,
WHITE : this.PointState.WHITE
};
this.GameStatus = {
ACTIVE : "active",
IDLE : "idle",
ENDED : "ended"
};
};
/**
* OBJ: Defines changed points after a move is made.
*/
function MoveResult(player, newPoint, capturedPoints) {
this.player = player;
this.newPoint = newPoint;
this.capturedPoints = capturedPoints;
}
/**
* OBJ: Defines common attributes for board points/intersections.
*/
function Point(row, column) {
this.row = row;
this.column = column;
this.getNeighborAt = function(side) {
switch (side) {
case Constants.Direction.NORTH:
return new Point(this.row-1, this.column);
case Constants.Direction.SOUTH:
return new Point(this.row+1, this.column);
case Constants.Direction.EAST:
return new Point(this.row, this.column+1);
case Constants.Direction.WEST:
return new Point(this.row, this.column-1);
}
};
this.toString = function() {
return "(" + this.row + ", " + this.column + ")";
};
this.equals = function(other) {
return (this.row == other.row &&
this.column == other.column);
};
this.isInArray = function(array) {
for (var i=0; i<array.length; i++) {
if (this.equals(array[i])) {
return true;
}
}
return false;
};
}
/**
* OBJ: Defines common attributes for a territory.
*/
function Territory(points, owner) {
this.points = points || new Array();
this.owner = owner || Constants.TerritoryOwner.UNKNOWN;
}
/**
* OBJ: Defines common attributes for a player.
*/
function Player(color, pointState, score) {
this.color = color; // Constants.Color.(BLACK/WHITE)
this.pointState = pointState; // Constants.PointState.(BLACK/WHITE)
this.score = score || 0;
this.equals = function(other) {
return (this.color == other.color);
};
this.toString = function() {
return this.color;
};
}
/**
* OBJ: Defines common attributes for a game of Go.
*/
function GameState(boardWidth, boardHeight, player1, player2, status) {
this.boardWidth = boardWidth;
this.boardHeight = boardHeight;
// Initialize board
this.board = new Array(this.boardHeight);
for (var i = 0; i < this.boardHeight; i++) {
this.board[i] = new Array(this.boardWidth);
for (var j = 0; j < this.boardWidth; j++) {
this.board[i][j] = Constants.PointState.EMPTY;
}
}
this.previousBoard = $.extend(true, [], this.board);
this.player1 = player1;
this.player2 = player2;
this.currentPlayer = player1;
this.moveError;
this.status = status || Constants.GameStatus.ACTIVE;
this.getPointStateAt = function(point) {
return this.board[point.row][point.column];
};
this.setPointStateAt = function(point, pointState) {
this.board[point.row][point.column] = pointState;
};
this.isUniqueBoard = function() {
// Compare board and previousBoard arrays
for (var y = 0; y < this.boardHeight; y++) {
for (var x = 0; x < this.boardWidth; x++) {
if (this.board[y][x] != this.previousBoard[y][x]) {
return true;
}
}
}
return false;
};
this.getBoardCopy = function() {
return $.extend(true, [], this.board)
};
this.setBoardCopy = function(board) {
this.board = $.extend(true, [], board);
};
};