-
Notifications
You must be signed in to change notification settings - Fork 0
/
halma.js
256 lines (229 loc) · 7.2 KB
/
halma.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
var kBoardWidth = 9;
var kBoardHeight= 9;
var kNumPieces = 9;
var kPieceWidth = 50;
var kPieceHeight= 50;
var kPixelWidth = 1 + (kBoardWidth * kPieceWidth);
var kPixelHeight= 1 + (kBoardHeight * kPieceHeight);
var gCanvasElement;
var gDrawingContext;
var gPattern;
var gPieces;
var gNumPieces;
var gSelectedPieceIndex;
var gSelectedPieceHasMoved;
var gMoveCount;
var gMoveCountElem;
var gGameInProgress;
function Cell(row, column) {
this.row = row;
this.column = column;
}
function getCursorPosition(e) {
/* returns Cell with .row and .column properties */
var x;
var y;
if (e.pageX || e.pageY) {
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;
}
x -= gCanvasElement.offsetLeft;
y -= gCanvasElement.offsetTop;
x = Math.min(x, kBoardWidth * kPieceWidth);
y = Math.min(y, kBoardHeight * kPieceHeight);
var cell = new Cell(Math.floor(y/kPieceWidth), Math.floor(x/kPieceHeight));
return cell;
}
function halmaOnClick(e) {
var cell = getCursorPosition(e);
for (var i = 0; i < gNumPieces; i++) {
if ((gPieces[i].row == cell.row) &&
(gPieces[i].column == cell.column)) {
clickOnPiece(i);
return;
}
}
clickOnEmptyCell(cell);
}
function clickOnEmptyCell(cell) {
if (gSelectedPieceIndex == -1) { return; }
var rowDiff = Math.abs(cell.row - gPieces[gSelectedPieceIndex].row);
var columnDiff = Math.abs(cell.column - gPieces[gSelectedPieceIndex].column);
if ((rowDiff <= 1) &&
(columnDiff <= 1)) {
/* we already know that this click was on an empty square,
so that must mean this was a valid single-square move */
gPieces[gSelectedPieceIndex].row = cell.row;
gPieces[gSelectedPieceIndex].column = cell.column;
gMoveCount += 1;
gSelectedPieceIndex = -1;
gSelectedPieceHasMoved = false;
drawBoard();
return;
}
if ((((rowDiff == 2) && (columnDiff == 0)) ||
((rowDiff == 0) && (columnDiff == 2)) ||
((rowDiff == 2) && (columnDiff == 2))) &&
isThereAPieceBetween(gPieces[gSelectedPieceIndex], cell)) {
/* this was a valid jump */
if (!gSelectedPieceHasMoved) {
gMoveCount += 1;
}
gSelectedPieceHasMoved = true;
gPieces[gSelectedPieceIndex].row = cell.row;
gPieces[gSelectedPieceIndex].column = cell.column;
drawBoard();
return;
}
gSelectedPieceIndex = -1;
gSelectedPieceHasMoved = false;
drawBoard();
}
function clickOnPiece(pieceIndex) {
if (gSelectedPieceIndex == pieceIndex) { return; }
gSelectedPieceIndex = pieceIndex;
gSelectedPieceHasMoved = false;
drawBoard();
}
function isThereAPieceBetween(cell1, cell2) {
/* note: assumes cell1 and cell2 are 2 squares away
either vertically, horizontally, or diagonally */
var rowBetween = (cell1.row + cell2.row) / 2;
var columnBetween = (cell1.column + cell2.column) / 2;
for (var i = 0; i < gNumPieces; i++) {
if ((gPieces[i].row == rowBetween) &&
(gPieces[i].column == columnBetween)) {
return true;
}
}
return false;
}
function isTheGameOver() {
for (var i = 0; i < gNumPieces; i++) {
if (gPieces[i].row > 2) {
return false;
}
if (gPieces[i].column < (kBoardWidth - 3)) {
return false;
}
}
return true;
}
function drawBoard() {
if (gGameInProgress && isTheGameOver()) {
endGame();
}
gDrawingContext.clearRect(0, 0, kPixelWidth, kPixelHeight);
gDrawingContext.beginPath();
/* vertical lines */
for (var x = 0; x <= kPixelWidth; x += kPieceWidth) {
gDrawingContext.moveTo(0.5 + x, 0);
gDrawingContext.lineTo(0.5 + x, kPixelHeight);
}
/* horizontal lines */
for (var y = 0; y <= kPixelHeight; y += kPieceHeight) {
gDrawingContext.moveTo(0, 0.5 + y);
gDrawingContext.lineTo(kPixelWidth, 0.5 + y);
}
/* draw it! */
gDrawingContext.strokeStyle = "#ccc";
gDrawingContext.stroke();
for (var i = 0; i < 9; i++) {
drawPiece(gPieces[i], i == gSelectedPieceIndex);
}
gMoveCountElem.innerHTML = gMoveCount;
saveGameState();
}
function drawPiece(p, selected) {
var column = p.column;
var row = p.row;
var x = (column * kPieceWidth) + (kPieceWidth/2);
var y = (row * kPieceHeight) + (kPieceHeight/2);
var radius = (kPieceWidth/2) - (kPieceWidth/10);
gDrawingContext.beginPath();
gDrawingContext.arc(x, y, radius, 0, Math.PI*2, false);
gDrawingContext.closePath();
gDrawingContext.strokeStyle = "#000";
gDrawingContext.stroke();
if (selected) {
gDrawingContext.fillStyle = "#000";
gDrawingContext.fill();
}
}
function supportsLocalStorage() {
return ('localStorage' in window) && window['localStorage'] !== null;
}
function saveGameState() {
if (!supportsLocalStorage()) { return false; }
localStorage["halma.game.in.progress"] = gGameInProgress;
for (var i = 0; i < kNumPieces; i++) {
localStorage["halma.piece." + i + ".row"] = gPieces[i].row;
localStorage["halma.piece." + i + ".column"] = gPieces[i].column;
}
localStorage["halma.selectedpiece"] = gSelectedPieceIndex;
localStorage["halma.selectedpiecehasmoved"] = gSelectedPieceHasMoved;
localStorage["halma.movecount"] = gMoveCount;
return true;
}
function resumeGame() {
if (!supportsLocalStorage()) { return false; }
gGameInProgress = (localStorage["halma.game.in.progress"] == "true");
if (!gGameInProgress) { return false; }
gPieces = new Array(kNumPieces);
for (var i = 0; i < kNumPieces; i++) {
var row = parseInt(localStorage["halma.piece." + i + ".row"]);
var column = parseInt(localStorage["halma.piece." + i + ".column"]);
gPieces[i] = new Cell(row, column);
}
gNumPieces = kNumPieces;
gSelectedPieceIndex = parseInt(localStorage["halma.selectedpiece"]);
gSelectedPieceHasMoved = localStorage["halma.selectedpiecehasmoved"] == "true";
gMoveCount = parseInt(localStorage["halma.movecount"]);
drawBoard();
return true;
}
function newGame() {
gPieces = [new Cell(kBoardHeight - 3, 0),
new Cell(kBoardHeight - 2, 0),
new Cell(kBoardHeight - 1, 0),
new Cell(kBoardHeight - 3, 1),
new Cell(kBoardHeight - 2, 1),
new Cell(kBoardHeight - 1, 1),
new Cell(kBoardHeight - 3, 2),
new Cell(kBoardHeight - 2, 2),
new Cell(kBoardHeight - 1, 2)];
gNumPieces = gPieces.length;
gSelectedPieceIndex = -1;
gSelectedPieceHasMoved = false;
gMoveCount = 0;
gGameInProgress = true;
drawBoard();
}
function endGame() {
gSelectedPieceIndex = -1;
gGameInProgress = false;
}
function initGame(canvasElement, moveCountElement) {
if (!canvasElement) {
canvasElement = document.createElement("canvas");
canvasElement.id = "halma_canvas";
document.body.appendChild(canvasElement);
}
if (!moveCountElement) {
moveCountElement = document.createElement("p");
document.body.appendChild(moveCountElement);
}
gCanvasElement = canvasElement;
gCanvasElement.width = kPixelWidth;
gCanvasElement.height = kPixelHeight;
gCanvasElement.addEventListener("click", halmaOnClick, false);
gMoveCountElem = moveCountElement;
gDrawingContext = gCanvasElement.getContext("2d");
if (!resumeGame()) {
newGame();
}
}