forked from ismyrnow/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathish.go.view.h5.js
344 lines (285 loc) · 9.12 KB
/
ish.go.view.h5.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Ish.Go namespace declaration
var Ish = Ish || {};
Ish.Go = Ish.Go || {};
// begin Ish.Go.View namespace
Ish.Go.View = new function() {
var canvas;
var context;
var isBoardMarked = false;
// Static object for storing constants
var ViewConstants = new function() {
this.boardWidth = 19;
this.boardHeight = 19;
this.boardPadding = 6;
this.pieceWidth = 27;
this.pieceHeight = 27;
this.pixelWidth = 522;
this.pixelHeight = 522;
this.imgPieceBlack = "piece-black.png";
this.imgPieceWhite = "piece-white.png";
this.imgFlagBlack = "flag-black.png";
this.imgFlagWhite = "flag-white.png";
};
// Object for tracking xy coords
var Coords = function(x, y) {
this.x = x;
this.y = y;
};
// Tracks clicks on the board (canvas)
var clickListener = function(e) {
var point = Ish.Go.View.getPoint(e);
if (point && !isBoardMarked) {
Ish.Go.View.placePiece(point);
}
};
// Tracks mouse movement over the board (canvas)
var mouseMoveListener = function(e) {
var coords = Ish.Go.View.getCanvasCoords(e);
var point = Ish.Go.View.getPoint(e);
$("#coords").html("(" + coords.x + ", " + coords.y + ")");
if (point) {
$("#point").html("(" + point.row + ", " + point.column + ")");
} else {
$("#point").html("(-, -)");
}
};
/**
* Initializes a canvas and context for use in the View, but only if necessary
*/
var initCanvas = function() {
if ($("#go-canvas").length == 0 || !canvas || !context) {
canvas = document.createElement("canvas");
canvas.id = "go-canvas";
$("#board").append(canvas);
canvas.width = ViewConstants.pixelWidth;
canvas.height = ViewConstants.pixelHeight;
canvas.style.background = "transparent url(board.png) no-repeat 0 0";
canvas.addEventListener("click", clickListener, false);
canvas.addEventListener("mousemove", mouseMoveListener);
context = canvas.getContext("2d");
}
};
// Given a mouse event, returns Coords relative to the canvas
this.getCanvasCoords = function(e) {
var x, y;
// Get xy coords on page
if (e.pageX != undefined && e.pageY != undefined) {
x = e.pageX;
y = e.pageY;
} else {
x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
// Narrow xy coords to canvas
x -= canvas.offsetLeft;
y -= canvas.offsetTop;
return new Coords(x, y);
};
// Returns board Point from mouse event, or null if not a valid Point
this.getPoint = function(e) {
var coords = this.getCanvasCoords(e);
var x = coords.x;
var y = coords.y;
// Remove padding from coords
x -= ViewConstants.boardPadding;
y -= ViewConstants.boardPadding;
// Check if xy coords are in the padding
if (x <= 0 || x >= ViewConstants.pixelWidth - (2 * ViewConstants.boardPadding) ||
y <= 0 || y >= ViewConstants.pixelHeight - (2 * ViewConstants.boardPadding)) {
return null;
}
// Get Point from xy coords on canvas
var point = new Point(
Math.floor(y/ViewConstants.pieceHeight), // row
Math.floor(x/ViewConstants.pieceWidth) // column
);
return point;
};
// Given a Point, returns the top-left Coords on the canvas
this.getCoordsFromPoint = function(point) {
return new Coords(
((point.column) * ViewConstants.pieceWidth) + ViewConstants.boardPadding,
((point.row) * ViewConstants.pieceHeight) + ViewConstants.boardPadding
);
};
// Places piece, and draws changes on the board
this.placePiece = function(point) {
var moveResult = Ish.Go.Logic.move(point.row, point.column);
// Check for empty MoveResult (indicates invalid move)
if (!moveResult) {
var alertMsg = "Invalid Move";
// Add specific message if present
if (gGameState.moveError) {
alertMsg += ":\n" + gGameState.moveError;
}
alert(alertMsg);
return;
}
// Redraw board changes as a result of the move
this.update(moveResult);
};
/**
* Draws piece on canvas
*/
this.drawPiece = function(point, color) {
var coords = this.getCoordsFromPoint(point);
var piece = new Image();
if (color == Constants.Color.BLACK) {
piece.src = ViewConstants.imgPieceBlack;
} else {
piece.src = ViewConstants.imgPieceWhite;
}
piece.onload = function() {
context.drawImage(piece, coords.x, coords.y);
};
};
/**
* Draws territory on canvas
*/
this.drawTerritory = function(point, owner) {
var coords = this.getCoordsFromPoint(point);
var territory = new Image();
if (owner == Constants.TerritoryOwner.BLACK) {
territory.src = ViewConstants.imgFlagBlack;
}
else if (owner == Constants.TerritoryOwner.WHITE) {
territory.src = ViewConstants.imgFlagWhite;
}
else { // Neutral
return;
}
territory.onload = function() {
context.drawImage(territory, coords.x, coords.y);
};
};
this.removePieces = function(points) {
var coords;
$.each(points, function() {
coords = Ish.Go.View.getCoordsFromPoint(this);
context.clearRect(
coords.x,
coords.y,
ViewConstants.pieceWidth,
ViewConstants.pieceHeight
);
});
};
this.update = function(moveResult) {
if (moveResult) {
// Draw only board changes
this.drawPiece(moveResult.newPoint, moveResult.player.color);
this.removePieces(moveResult.capturedPoints);
this.drawInfo();
}
};
this.redraw = function(canvasElement) {
// Create canvas and context if necessary
if (!canvasElement) {
initCanvas();
}
this.drawBoard();
this.drawInfo();
};
this.drawBoard = function() {
context.clearRect(0, 0, ViewConstants.pixelWidth, ViewConstants.pixelHeight);
var point;
var pState;
for (var y = 0; y < gGameState.boardHeight; y++) {
for (var x = 0; x < gGameState.boardWidth; x++) {
point = new Point(y, x);
pState = gGameState.getPointStateAt(point);
if (pState == Constants.PointState.BLACK) {
this.drawPiece(point, Constants.Color.BLACK);
}
else if (pState == Constants.PointState.WHITE) {
this.drawPiece(point, Constants.Color.WHITE);
}
}
}
};
this.drawMarkedBoard = function() {
var markedBoard = Ish.Go.Logic.getMarkedBoard();
context.clearRect(0, 0, ViewConstants.pixelWidth, ViewConstants.pixelHeight);
for (var y = 0; y < gGameState.boardHeight; y++) {
for (var x = 0; x < gGameState.boardWidth; x++) {
this.drawTerritory(new Point(y,x), markedBoard[y][x]);
}
}
};
this.drawInfo = function() {
// Print turn
$("#turn").html("Current Turn: " + gGameState.currentPlayer.color);
// Print scores
Ish.Go.Logic.setScores();
var p1 = gGameState.player1;
var p2 = gGameState.player2;
$("#score").html("Score:" +
"<br> " +
p1.color + ": " + p1.score +
"<br> " +
p2.color + ": " + p2.score);
};
/**
* Starts a new game.
*/
this.startNewGame = function() {
Ish.Go.Logic.newGame(19, 19);
this.redraw($("go-canvas"));
};
/**
* Prints code defining current game state on web page
*/
this.printGameState = function(aId) {
var id = aId || 'gameState';
var sBoard = "";
// Initialize game state
sBoard += "gGameState = new GameState(\n";
sBoard += "\t" + gGameState.boardWidth + ",\n";
sBoard += "\t" + gGameState.boardHeight + ",\n";
sBoard += "\tnew Player(Constants.Color.BLACK, Constants.PointState.BLACK),\n";
sBoard += "\tnew Player(Constants.Color.WHITE, Constants.PointState.WHITE)\n";
sBoard += ");\n";
// Set current player
sBoard += "gGameState.currentPlayer = " +
(gGameState.currentPlayer == gGameState.player1 ?
"gGameState.player1;\n" :
"gGameState.player2;\n");
// Set board
for (var y = 0; y < gGameState.boardHeight; y++) {
sBoard += "gGameState.board[" + y + "] = [";
for (var x = 0; x < gGameState.boardWidth; x++) {
sBoard += "\"" + gGameState.board[y][x] + "\",";
}
sBoard = sBoard.substring(0, sBoard.length-1);
sBoard += "];\n";
}
// Set previous board
for (var y = 0; y < gGameState.boardHeight; y++) {
sBoard += "gGameState.previousBoard[" + y + "] = [";
for (var x = 0; x < gGameState.boardWidth; x++) {
sBoard += "\"" + gGameState.previousBoard[y][x] + "\",";
}
sBoard = sBoard.substring(0, sBoard.length-1);
sBoard += "];\n";
}
$("#" + id).html("<textarea>" + sBoard + "</textarea>");
};
/**
* Toggles between showing a regular or marked board.
* Merely calls appropriate print functions.
*/
this.toggleMarkedBoard = function() {
isBoardMarked ? this.drawBoard() : this.drawMarkedBoard();
isBoardMarked = !isBoardMarked;
};
this.init = function() {
// Initialize game state
gGameState = new GameState(
19,
19,
new Player(Constants.Color.BLACK, Constants.PointState.BLACK),
new Player(Constants.Color.WHITE, Constants.PointState.WHITE)
);
this.redraw();
};
}; // end Ish.Go.View namespace