forked from Vesper-Team/JavaScriptUI-DOM-TeamWork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
461 lines (415 loc) · 16 KB
/
Copy pathindex.js
File metadata and controls
461 lines (415 loc) · 16 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// CONSTANTS
var fieldWidth = 54, // the width of every of the 24 fields
offsetBetweenFields = 5, // between small field(white) and big field(dark)
boardSide = 324, // the width of one side of the board
boardBar = 34, // the thing between the board sides in pixels
boardFrameOffset = 18, // the offset in the corners between the board itself and the frame in pixels
checkerHeight = 42, // the height of the checker after being fully drawn in pixels
checkerRadius = 21, // full width = 44; outer stroke 1px
checkerInnerCircleRadius = 10,
initialDiceValue = 6;
// GAME ELEMENTS - canvas, ctx, keystate, etc.
var canvas,
ctx,
diceCanvas,
diceCtx,
clickedField,
firstPlayer,
secondPlayer,
firstDice,
secondDice,
firstDiceThrow,
firstPlayerOnTurn,
mustThrowDices;
// DEFINING GAME OBJECTS - player, checker, dice, board(not if it is just a background) ...
var player = (function () {
var player = {
init: function (name, color) {
this.name = name;
this.color = color;
this.currentCheckersCount = 15;
this.hasHitChecker = false;
this.hitCheckersCount = 0;
this.canTakeChecker = false;
return this;
},
tookChecker: function () {
// TODO: Validate depending on call.
this.currentCheckersCount -= 1;
}
};
Object.defineProperty(player, 'name', {
get: function () {
return this._name;
},
set: function (value) {
//TODO: Validate
this._name = value;
}
});
Object.defineProperty(player, 'currentCheckersCount', {
get: function () {
return this._currentCheckersCount;
},
set: function (value) {
//TODO: Validate
this._currentCheckersCount = value;
}
});
Object.defineProperty(player, 'color', {
get: function () {
return this._color;
},
set: function (value) {
// TODO: Validate
this._color = value;
}
});
Object.defineProperty(player, 'hasHitChecker', {
get: function () {
return this._hasHitChecker;
},
set: function (value) {
// TODO: Validate
this._hasHitChecker = value;
}
});
Object.defineProperty(player, 'hitCheckersCount', {
get: function () {
return this._hitCheckersCount;
},
set: function (value) {
// TODO: Validate
this._hitCheckersCount = value;
}
});
Object.defineProperty(player, 'canTakeChecker', {
get: function () {
return this._canTakeChecker;
},
set: function (value) {
// TODO: Validate
this._canTakeChecker = value;
}
});
return player;
}());
var field = (function () { /* the board is filled with 24 fields,
IT IS A RESPONSIBILITY OF THE FIELDS TO GIVE COORDINATES TO THE CHECKERS, WITH RESPECT TO THEIR OWN X,Y COORDINATES
IN THIS WAY WE DON'T CARE HOW TO STACK CHECKERS AND TO TAKE INTO ACCOUNT THEIR Xs AND Ys,
THE FIELD JUST STACKS THEM DEPENDING ON THE COUNT IT IS HOLDING, GIVING HIS OWN X, Y */
var field = {
init: function (x, y) {
this.checkers = [];
this.x = x;
this.y = y;
/* if field is 0~11 y = canvas.height - boardFrameOffset; else y = boardFrameOffset;
this is needed only for knowing at which side are we, so we can draw the checkers properly */
return this;
},
addChecker: function (checker) {
this.checkers.push(checker);
},
removeChecker: function () {
this.checkers.pop();
},
draw: function () {
var count = this.checkers.length, // count checkers, if more than 5 draw stacked
i,
fieldCenter;
for (i = 0; i < count; i += 1) {
if (i > 4) {
// draw stacked
ctx.save();
ctx.fillStyle = 'red';
ctx.font = "12px Verdana";
ctx.fillText('x' + (i - 3), (this.x - 6) + fieldWidth / 2, this.y + checkerRadius + 4);
// x2 for the 6th checker because it represents how many checkers are stacked
// 6 and 4 are just a magic numbers to position the text in the center of the checker, because the text has also its own offset
ctx.restore();
} else {
if (this.y < canvas.height / 2) { // if field is in the upper half of the board, should stack them up to bottom
fieldCenter = this.x + (fieldWidth / 2); // x coordinate for the checker center
this.checkers[i].draw(fieldCenter, this.y + i * checkerHeight + checkerRadius + 2);
/* +2 just to look better,
the checker from the board frame */
} else { // when the field is in the lower part, should stack them from bottom to up
fieldCenter = this.x + (fieldWidth / 2); // x coordinate for the checker center
this.checkers[i].draw(fieldCenter, this.y - i * checkerHeight + checkerRadius - 2); // -2 just to look better
}
}
}
}
};
Object.defineProperty(field, 'checkers', {
get: function () {
return this._checkers;
},
set: function (value) {
this._checkers = value;
}
});
Object.defineProperty(field, 'x', {
get: function () {
return this._x;
},
set: function (value) {
this._x = value;
}
});
Object.defineProperty(field, 'y', {
get: function () {
return this._y;
},
set: function (value) {
this._y = value;
}
});
return field;
}());
var checker = (function () {
var checker = {
init: function (color) {
this.color = color; // can be only 'black' or 'white'
return this;
},
draw: function (x, y) {
ctx.save();
if (this.color === 'black') {
ctx.strokeStyle = 'white';
}
// outer circle
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.lineWidth = 1;
ctx.arc(x, y, checkerRadius, 0, 2 * Math.PI);
ctx.fill();
ctx.stroke();
// inner circle
ctx.beginPath();
ctx.strokeStyle = '#808080';
ctx.lineWidth = 2;
ctx.arc(x, y, checkerInnerCircleRadius, 0, 2 * Math.PI);
ctx.stroke();
ctx.restore();
}
};
Object.defineProperty(checker, 'color', {
get: function () {
return this._color;
},
set: function (value) {
this._color = value;
}
});
return checker;
}());
var board = (function () {
var bottomSideAllSet, // flag to indicate that we must start with a field having 5 checkers in it(the 12th field)
// at bottom we start with 12. 5 3 5 2 .23
// at top we start with 11. 5 3 5 2 .0
patternToPutColors = []; // colors are inverted in the opposite sides of the field, sorry it truly looks ugly..
var gameboard = {
init: function () {
this.fields = [];
bottomSideAllSet = false;
patternToPutColors[0] = 1;
patternToPutColors[5] = 0;
patternToPutColors[7] = 0;
patternToPutColors[11] = 1;
var self = this;
setupHalfGameBoard(self, bottomSideAllSet);
bottomSideAllSet = true;
setupHalfGameBoard(self, bottomSideAllSet);
return this;
},
addField: function (value) {
this.fields.push(value);
}
};
function setupHalfGameBoard(self, bottomSideAllSet) {
var i,
len,
x,
y,
currentField,
patternOfTheNumberOfCheckersToPut = [2, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, 5],// on 0 field put 2, on field 1 put 0, etc.. the setup rules..
j, // needed to go trough the patternToPutCheckers
colorToPut,
countPassedFields = 0; // needed to add a boardBar offset to the X coordinate
if (bottomSideAllSet) {
i = 12;
j = 11;
} else {
i = 0;
j = 0;
}
for (len = i + 12; i < len; i += 1, (i >= 12) ? j -= 1 : j += 1, countPassedFields += 1) {
if (countPassedFields >= 6) { // after every 6 fields we must consider the boardBar also, in both the upper and the lower side of the board
x = (!bottomSideAllSet) * canvas.width - boardFrameOffset - ((countPassedFields + !bottomSideAllSet) * fieldWidth) - boardBar;
} else {
x = (!bottomSideAllSet) * canvas.width - boardFrameOffset - ((countPassedFields + !bottomSideAllSet) * fieldWidth);
// if we are at the bottom we must consider the canvas width and to subtract from it
}
y = (!bottomSideAllSet) * (canvas.height - checkerHeight) - boardFrameOffset;
if (x < 0) { // because we are extracting everything from the canvas width or height
x *= -1; // when we are at the bottom side, we start from the MOST-RIGHT field(our 0 field) so canvas height, and width must be considered,
} // while when we are at the upper side of the board, the MOST-LEFT field(the 12th field) must be filled first,
if (y < 0) { // so canvas width and height in the upper side are 0, and we get a negative number but we actually need that number to be positive,
y *= -1; // to go to the right.. to fill the rest of the fields.. it's all because of how the fields position in backgammon
}
if (patternToPutColors[j] === 1) {
colorToPut = 'white';
patternToPutColors[j] = 0; // swapping the pattern for the next half of the board
} else if (patternToPutColors[j] === 0) {
colorToPut = 'black';
patternToPutColors[j] = 1;
}
currentField = Object.create(field).init(x, y); // creating one of the 24 fields in the board
putCheckersOnField(patternOfTheNumberOfCheckersToPut[j], currentField, colorToPut); // assigning checkers to it with respect to the Backgammon setup rules
self.addField(currentField); // adding the field to the board, used 'self' because the scope of this has changed
}
}
function putCheckersOnField(checkersToPut, givenField, color) {
var i;
for (i = 0; i < checkersToPut; i += 1) {
givenField.addChecker(Object.create(checker).init(color));
}
}
return gameboard;
}());
var dice = (function () {
var dice = {
init: function () {
this.number = initialDiceValue;
return this;
}
};
Object.defineProperty(dice, 'number', {
get: function () {
return this._number;
},
set: function (value) {
checkIfDiceValueIsValid(value);
this._number = value;
}
});
Object.defineProperty(dice, 'generateNewNumber', {
value: function () {
return this.number = Math.round(Math.random() * 5) + 1;
}
});
function checkIfDiceValueIsValid(number) {
if (isNaN(number) || number % 1 != 0 || number < 1 || number > 6) {
throw new Error('Dice number is invalid!');
}
}
return dice;
}());
// function newGame() or main()
// create initiate and append game canvas
// INITIATE GAME OBJECTS;
function newGame() {
canvas = document.getElementById('board-canvas');
ctx = canvas.getContext('2d');
// INIT GAME OBJECTS, SETS UP GAME
board.init();
firstDice = Object.create(dice).init();
secondDice = Object.create(dice).init();
firstDiceThrow = true;
}
newGame();
function play() {
// GAME LOOP - game logic, updating the fields, players turns, dices etc. Update update update
// change fields depending on how the player interact(key presses)
canvas.addEventListener('click', findPressedField);
//LOGIC NEEDS TO BE IMPLEMENTED FOR CURRENT PLAYER
//THROW DICE - at first time, throw to see who will be playing first
if (firstDiceThrow) {
firstDiceThrow = false;
firstDice.generateNewNumber();
secondDice.generateNewNumber();
if (firstDice.number > secondDice.number) {
firstPlayerOnTurn = true;
} else if (firstDice.number < secondDice.number) {
firstPlayerOnTurn = false;
} else {
firstDiceThrow = true;
play();
}
} else {
firstDice.generateNewNumber();
secondDice.generateNewNumber();
}
//CHECK IF ANY POSSIBLE MOVE
//IF ANY POSSIBLE MOVES AVAILABLE --> CHECK FOR NEW CLICKS AND CATCH START POSITION , else change player
//IF ANY POSSIBLE MOVES AVAILABLE --> CHECK IF NEW SET/CLICKED POSITION IS CORRECT AND MOVE PULL , else try again
// DRAW
// draws every time and if we have changes its ok
draw();
// CHANGE PLAYER
firstPlayerOnTurn ? firstPlayerOnTurn = false : firstPlayerOnTurn = true;
requestAnimationFrame(play);
}
play();
function findPressedField(event) {
var x = event.offsetX;
var y = event.offsetY;
if (((x < canvas.width - boardFrameOffset && x > boardFrameOffset + boardSide + boardBar) ||
(x < boardFrameOffset + boardSide && x > boardFrameOffset)) &&
(y < canvas.height - boardFrameOffset && y > boardFrameOffset)) { // ignoring every piece of the frame
var i = 0,
len,
foundField,
upperSide = false;
// determining in which quadrant are we exactly
if (y < canvas.height / 2 && y > boardFrameOffset) {
i = 12; // last field of the upper side, last because if we take the first our condition in the for-loop will be achieved every time
upperSide = true;
} else {
i = 0; // first field of the lower side
}
if (x < boardSide + boardFrameOffset && x > boardFrameOffset) { // left side of the board
if (i === 0) { // bottom left side
i = 6;
} else { // upper left side
i = 12;
}
} else { // right side of the board
if (i === 0) { // bottom right side
i = 0;
} else { // upper left side
i = 18; // again last of the array
}
}
for (len = i + 6; i < len; i += 1) {
var currentField = board.fields[i];
if (upperSide) { /* its all because of the fields..
the start position of the lower side is most-right and it starts from 0,
while the start position of the upper side is the most-left which confuses the algorithm */
var previousField = board.fields[i - 1];
if (currentField.x > x) {
foundField = previousField;
break;
}
} else {
if (x > currentField.x) {
foundField = currentField;
break;
}
}
}
if (typeof foundField === 'undefined') { // when we are going out of the for-in and the condition for the upperSide is not met,
// because we take the previous field element we could not get to check the last one
foundField = board.fields[len - 1];
}
clickedField = foundField;
console.log(clickedField); // testing purposes
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // refresh canvas
board.fields.forEach(function (field) {
field.draw(); // redraw every field again
});
}