forked from codethejason/checkers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
265 lines (252 loc) · 10.2 KB
/
Copy pathscript.js
File metadata and controls
265 lines (252 loc) · 10.2 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
window.onload = function() {
//The initial setup
var gameBoard = [
[ 0, 1, 0, 1, 0, 1, 0, 1 ],
[ 1, 0, 1, 0, 1, 0, 1, 0 ],
[ 0, 1, 0, 1, 0, 1, 0, 1 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 2, 0, 2, 0, 2, 0, 2, 0 ],
[ 0, 2, 0, 2, 0, 2, 0, 2 ],
[ 2, 0, 2, 0, 2, 0, 2, 0 ]
];
//arrays to store the instances
var pieces = [];
var tiles = [];
//distance formula
var dist = function (x1, y1, x2, y2) {
return Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2));
}
//Piece object - there are 24 instances of them in a checkers game
function Piece (element, position) {
//linked DOM element
this.element = element;
//positions on gameBoard array in format row, column
this.position = position;
//which player's piece i it
this.player = '';
//figure out player by piece id
if(this.element.attr("id") < 12)
this.player = 1;
else
this.player = 2;
//makes object a king
this.king = false;
this.makeKing = function () {
this.element.css("backgroundImage", "url('king"+this.player+".png')");
this.king = true;
}
//moves the piece
this.move = function (tile) {
this.element.removeClass('selected');
if(!Board.isValidPlacetoMove(tile.position[0], tile.position[1])) return false;
//make sure piece doesn't go backwards if it's not a king
if(this.player == 1 && this.king == false) {
if(tile.position[0] < this.position[0]) return false;
} else if (this.player == 2 && this.king == false) {
if(tile.position[0] > this.position[0]) return false;
}
//remove the mark from Board.board and put it in the new spot
Board.board[this.position[0]][this.position[1]] = 0;
Board.board[tile.position[0]][tile.position[1]] = this.player;
this.position = [tile.position[0], tile.position[1]];
//change the css using board's dictionary
this.element.css('top', Board.dictionary[this.position[0]]);
this.element.css('left', Board.dictionary[this.position[1]]);
//if piece reaches the end of the row on opposite side crown it a king (can move all directions)
if(!this.king && (this.position[0] == 0 || this.position[0] == 7 ))
this.makeKing();
Board.changePlayerTurn();
return true;
};
//tests if piece can jump anywhere
this.canJumpAny = function () {
if(this.canOpponentJump([this.position[0]+2, this.position[1]+2]) ||
this.canOpponentJump([this.position[0]+2, this.position[1]-2]) ||
this.canOpponentJump([this.position[0]-2, this.position[1]+2]) ||
this.canOpponentJump([this.position[0]-2, this.position[1]-2])) {
return true;
} return false;
};
//tests if an opponent jump can be made to a specific place
this.canOpponentJump = function(newPosition) {
//find what the displacement is
var dx = newPosition[1] - this.position[1];
var dy = newPosition[0] - this.position[0];
//make sure object doesn't go backwards if not a king
if(this.player == 1 && this.king == false) {
if(newPosition[0] < this.position[0]) return false;
} else if (this.player == 2 && this.king == false) {
if(newPosition[0] > this.position[0]) return false;
}
//must be in bounds
if(newPosition[0] > 7 || newPosition[1] > 7 || newPosition[0] < 0 || newPosition[1] < 0) return false;
//middle tile where the piece to be conquered sits
var tileToCheckx = this.position[1] + dx/2;
var tileToChecky = this.position[0] + dy/2;
//if there is a piece there and there is no piece in the space after that
if(!Board.isValidPlacetoMove(tileToChecky, tileToCheckx) && Board.isValidPlacetoMove(newPosition[0], newPosition[1])) {
//find which object instance is sitting there
for(pieceIndex in pieces) {
if(pieces[pieceIndex].position[0] == tileToChecky && pieces[pieceIndex].position[1] == tileToCheckx) {
if(this.player != pieces[pieceIndex].player) {
//return the piece sitting there
return pieces[pieceIndex];
}
}
}
}
return false;
};
this.opponentJump = function (tile) {
var pieceToRemove = this.canOpponentJump(tile.position);
//if there is a piece to be removed, remove it
if(pieceToRemove) {
pieces[pieceIndex].remove();
return true;
}
return false;
};
this.remove = function () {
//remove it and delete it from the gameboard
this.element.css("display", "none");
if(this.player == 1) $('#player2').append("<div class='capturedPiece'></div>");
if(this.player == 2) $('#player1').append("<div class='capturedPiece'></div>");
Board.board[this.position[0]][this.position[1]] = 0;
//reset position so it doesn't get picked up by the for loop in the canOpponentJump method
this.position = [];
}
}
function Tile (element, position) {
//linked DOM element
this.element = element;
//position in gameboard
this.position = position;
//if tile is in range from the piece
this.inRange = function(piece) {
if(dist(this.position[0], this.position[1], piece.position[0], piece.position[1]) == Math.sqrt(2)) {
//regular move
return 'regular';
} else if(dist(this.position[0], this.position[1], piece.position[0], piece.position[1]) == 2*Math.sqrt(2)) {
//jump move
return 'jump';
}
};
}
//Board object - controls logistics of game
var Board = {
board: gameBoard,
playerTurn: 1,
tilesElement: $('div.tiles'),
//dictionary to convert position in Board.board to the viewport units
dictionary: ["0vmin", "10vmin", "20vmin", "30vmin", "40vmin", "50vmin", "60vmin", "70vmin", "80vmin", "90vmin"],
//initialize the 8x8 board
initalize: function () {
var countPieces = 0;
var countTiles = 0;
for (row in this.board) { //row is the index
for (column in this.board[row]) { //column is the index
//whole set of if statements control where the tiles and pieces should be placed on the board
if(row%2 == 1) {
if(column%2 == 0) {
this.tilesElement.append("<div class='tile' id='tile"+countTiles+"' style='top:"+this.dictionary[row]+";left:"+this.dictionary[column]+";'></div>");
tiles[countTiles] = new Tile($("#tile"+countTiles), [parseInt(row), parseInt(column)]);
countTiles += 1;
}
} else {
if(column%2 == 1) {
this.tilesElement.append("<div class='tile' id='tile"+countTiles+"' style='top:"+this.dictionary[row]+";left:"+this.dictionary[column]+";'></div>");
tiles[countTiles] = new Tile($("#tile"+countTiles), [parseInt(row), parseInt(column)]);
countTiles += 1;
}
}
if(this.board[row][column] == 1) {
$('.player1pieces').append("<div class='piece' id='"+countPieces+"' style='top:"+this.dictionary[row]+";left:"+this.dictionary[column]+";'></div>");
pieces[countPieces] = new Piece($("#"+countPieces), [parseInt(row), parseInt(column)]);
countPieces += 1;
} else if(this.board[row][column] == 2) {
$('.player2pieces').append("<div class='piece' id='"+countPieces+"' style='top:"+this.dictionary[row]+";left:"+this.dictionary[column]+";'></div>");
pieces[countPieces] = new Piece($("#"+countPieces), [parseInt(row), parseInt(column)]);
countPieces += 1;
}
}
}
},
//check if the location has an object
isValidPlacetoMove: function (row, column) {
console.log(row); console.log(column); console.log(this.board);
if(this.board[row][column] == 0) {
return true;
} return false;
},
//change the active player - also changes div.turn's CSS
changePlayerTurn: function () {
if(this.playerTurn == 1) {
this.playerTurn = 2;
$('.turn').css("background", "linear-gradient(to right, transparent 50%, #BEEE62 50%)");
return;
}
if(this.playerTurn == 2) {
this.playerTurn = 1;
$('.turn').css("background", "linear-gradient(to right, #BEEE62 50%, transparent 50%)");
}
},
//reset the game
clear: function () {
location.reload();
}
}
//initialize the board
Board.initalize();
/***
Events
***/
//select the piece on click if it is the player's turn
$('.piece').on("click", function () {
var selected;
var isPlayersTurn = ($(this).parent().attr("class").split(' ')[0] == "player"+Board.playerTurn+"pieces");
if(isPlayersTurn) {
if($(this).hasClass('selected')) selected = true;
$('.piece').each(function(index) {$('.piece').eq(index).removeClass('selected')});
if(!selected) {
$(this).addClass('selected');
}
}
});
//reset game when clear button is pressed
$('#cleargame').on("click", function () {
Board.clear();
});
//move piece when tile is clicked
$('.tile').on("click", function () {
//make sure a piece is selected
if($('.selected').length != 0) {
//find the tile object being clicked
var tileID = $(this).attr("id").replace(/tile/, '');
var tile = tiles[tileID];
//find the piece being selected
var piece = pieces[$('.selected').attr("id")];
//check if the tile is in range from the object
var inRange = tile.inRange(piece);
if(inRange) {
//if the move needed is jump, then move it but also check if another move can be made (double and triple jumps)
if(inRange == 'jump') {
if(piece.opponentJump(tile)) {
piece.move(tile);
if(piece.canJumpAny()) {
Board.changePlayerTurn(); //change back to original since another turn can be made
piece.element.addClass('selected');
}
}
//if it's regular then move it if no jumping is available
} else if(inRange == 'regular') {
if(!piece.canJumpAny()) {
piece.move(tile);
} else {
alert("You must jump when possible!");
}
}
}
}
});
}