PageRenderTime 91ms CodeModel.GetById 1ms app.highlight 80ms RepoModel.GetById 1ms app.codeStats 1ms

/script.js

https://bitbucket.org/Spuntagano/chess
JavaScript | 762 lines | 625 code | 131 blank | 6 comment | 446 complexity | 93b0e287b98af8405d546dcf6ec70ad8 MD5 | raw file
  1(function($) {
  2
  3	Chess = {
  4
  5		grid : 8,
  6		tiles : [],
  7
  8		Piece : function(x, y, color, alive, type){
  9			 this.x = x;
 10			 this.y = y;
 11			 this.color = color;
 12			 this.alive = alive;
 13			 this.type = type;
 14		},
 15
 16		init : function(){
 17			var gridstr = "";
 18			gridstr += "<table>";
 19			for (var i=0; i<this.grid; i++){
 20				gridstr += "<tr>";
 21				for (var j=0; j<this.grid; j++){
 22					gridstr += "<td class='tile' id="+j+"-"+i+" data-x="+j+" data-y="+i+"></td>";
 23				}
 24				gridstr += "</tr>";
 25			}
 26			gridstr += "</table>"
 27			$("#wrapper").append(gridstr);
 28
 29			for (var i=0; i<this.grid; i++){
 30				this.tiles[i] = new Array();
 31			}
 32
 33			this.tiles[0][0] = new this.Piece(0,0,"black",true, "rook");
 34			this.tiles[7][0] = new this.Piece(7,0,"black",true, "rook");
 35			this.tiles[0][7] = new this.Piece(0,7,"white",true, "rook");
 36			this.tiles[7][7] = new this.Piece(7,7,"white",true, "rook");
 37
 38			this.tiles[1][0] = new this.Piece(1,0,"black",true, "knight");
 39			this.tiles[6][0] = new this.Piece(6,0,"black",true, "knight");
 40			this.tiles[1][7] = new this.Piece(1,7,"white",true, "knight");
 41			this.tiles[6][7] = new this.Piece(6,7,"white",true, "knight");
 42
 43			this.tiles[2][0] = new this.Piece(2,0,"black",true, "bishop");
 44			this.tiles[5][0] = new this.Piece(5,0,"black",true, "bishop");
 45			this.tiles[2][7] = new this.Piece(2,7,"white",true, "bishop");
 46			this.tiles[5][7] = new this.Piece(5,7,"white",true, "bishop");
 47
 48			this.tiles[3][0] = new this.Piece(3,0,"black",true, "queen");
 49			this.tiles[3][7] = new this.Piece(3,7,"white",true, "queen");
 50
 51			this.tiles[4][0] = new this.Piece(4,0,"black",true, "king");
 52			this.tiles[4][7] = new this.Piece(4,7,"white",true, "king");
 53
 54			for (var i=0; i<this.grid; i++){
 55				for(var j=0; j<this.grid; j++){
 56					if (i == 1){
 57						this.tiles[j][i] = new this.Piece(j,1,"black",true, "pawn");
 58					}
 59					if (i == this.grid-2){
 60						this.tiles[j][i] = new this.Piece(j,this.grid-2,"white",true, "pawn");
 61					}
 62					if (this.tiles[j][i] == undefined){
 63						this.tiles[j][i] = "empty";
 64					}
 65				}
 66			}
 67
 68			Chess.HTMLsync();
 69
 70		},
 71
 72		HTMLsync : function(){
 73			$(".white").removeClass("white");
 74			$(".black").removeClass("black");
 75			$("td").html("").unbind();
 76			for (i in Chess.tiles){
 77				if (Chess.tiles[i] != undefined){
 78					for (j in Chess.tiles[i]){
 79						if (Chess.tiles[i][j] != undefined){
 80							if (Chess.tiles[i][j].alive == true && Chess.tiles[i][j].type == "pawn"){
 81								Chess.syncPawn(Chess.tiles[i][j]);
 82								//console.log(this.tiles[i][j]);
 83							}
 84							if (Chess.tiles[i][j].alive == true && Chess.tiles[i][j].type == "rook"){
 85								Chess.syncRook(Chess.tiles[i][j]);
 86								//console.log(this.tiles[i][j]);
 87							}
 88							if (Chess.tiles[i][j].alive == true && Chess.tiles[i][j].type == "knight"){
 89								Chess.syncKnight(Chess.tiles[i][j]);
 90								//console.log(this.tiles[i][j]);
 91							}
 92							if (Chess.tiles[i][j].alive == true && Chess.tiles[i][j].type == "bishop"){
 93								Chess.syncBishop(Chess.tiles[i][j]);
 94								//console.log(this.tiles[i][j]);
 95							}
 96							if (Chess.tiles[i][j].alive == true && Chess.tiles[i][j].type == "queen"){
 97								Chess.syncQueen(Chess.tiles[i][j]);
 98								//console.log(this.tiles[i][j]);
 99							}
100							if (Chess.tiles[i][j].alive == true && Chess.tiles[i][j].type == "king"){
101								Chess.syncKing(Chess.tiles[i][j]);
102								//console.log(this.tiles[i][j]);
103							}
104						}
105					}
106				}
107			}
108		},
109
110		syncPawn : function(piece){
111			Chess.getTileHTML(piece.x, piece.y).html("pawn").addClass("pawn");
112			if (piece.color == "white"){
113				Chess.getTileHTML(piece.x, piece.y).addClass("white");
114			}
115			if (piece.color == "black"){
116				Chess.getTileHTML(piece.x, piece.y).addClass("black");
117			}
118
119			Chess.getTileHTML(piece.x, piece.y).on('click', function(){
120
121				if (piece.color == "white"){
122					Chess.getTileHTML(piece.x, piece.y-1).addClass("move");
123
124					if (Chess.tiles[piece.x+1] != undefined){
125						if (Chess.tiles[piece.x+1][piece.y-1] != undefined){
126							if (Chess.tiles[piece.x+1][piece.y-1].color == "black" && Chess.tiles[piece.x+1][piece.y-1].alive){
127								Chess.getTileHTML(piece.x+1,piece.y-1).addClass("kill");
128							}
129						}
130					}
131
132					if (Chess.tiles[piece.x-1] != undefined){
133						if (Chess.tiles[piece.x-1][piece.y-1] != undefined){
134							if (Chess.tiles[piece.x-1][piece.y-1].color == "black" && Chess.tiles[piece.x-1][piece.y-1].alive){
135								Chess.getTileHTML(piece.x-1, piece.y-1).addClass("kill");
136							}
137						}
138					}
139
140					if (Chess.tiles[piece.x] != undefined){
141						if (Chess.tiles[piece.x][piece.y-1] != undefined){
142							if (Chess.tiles[piece.x][piece.y-1].alive){
143								Chess.getTileHTML(piece.x, piece.y-1).removeClass("move");
144							}
145						}
146					}
147
148				}
149				else if (piece.color == "black"){
150					Chess.getTileHTML(piece.x, piece.y+1).addClass("move");
151
152					if (Chess.tiles[piece.x+1] != undefined){
153						if (Chess.tiles[piece.x+1][piece.y+1] != undefined){
154							if (Chess.tiles[piece.x+1][piece.y+1].color == "white" && Chess.tiles[piece.x+1][piece.y+1].alive){
155								Chess.getTileHTML(piece.x+1, piece.y+1).addClass("kill");
156							}
157						}
158					}
159
160					if (Chess.tiles[piece.x-1] != undefined){
161						if (Chess.tiles[piece.x-1][piece.y+1] != undefined){
162							if (Chess.tiles[piece.x-1][piece.y+1].color == "white" && Chess.tiles[piece.x-1][piece.y+1].alive){
163								Chess.getTileHTML(piece.x-1, piece.y+1).addClass("kill");
164							}
165						}
166					}
167
168					if (Chess.tiles[piece.x] != undefined){
169						if (Chess.tiles[piece.x][piece.y+1] != undefined){
170							if (Chess.tiles[piece.x][piece.y+1].alive){
171								Chess.getTileHTML(piece.x, piece.y+1).removeClass("move");
172							}
173						}
174					}
175				}
176				$(".tile").on('click', function(){
177					if ($(this).hasClass("move")){
178						Chess.movePieces(piece, $(this).attr("data-x"), $(this).attr("data-y"));
179					}
180					else if ($(this).hasClass("kill")){
181						Chess.destroyPieces(Chess.tiles[$(this).attr("data-x")][$(this).attr("data-y")]);
182						Chess.movePieces(piece, $(this).attr("data-x"), $(this).attr("data-y"));
183					}
184					else{
185						Chess.HTMLsync();
186					}
187					$(".move").removeClass("move");
188					$(".kill").removeClass("kill");
189				})
190
191			})
192
193		},
194
195		syncRook : function(piece){
196			Chess.getTileHTML(piece.x, piece.y).html("rook").addClass("rook");
197			if (piece.color == "white"){
198				Chess.getTileHTML(piece.x, piece.y).addClass("white");
199			}
200			if (piece.color == "black"){
201				Chess.getTileHTML(piece.x, piece.y).addClass("black");
202			}
203
204			Chess.getTileHTML(piece.x, piece.y).on('click', function(){
205
206				var i = piece.x;
207				var j = piece.y;
208
209				i++;
210				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
211					Chess.getTileHTML(i, j).addClass("move");
212					i++;
213				}
214				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
215					Chess.getTileHTML(i, j).addClass("kill");
216				}
217
218
219				i = piece.x;
220				j = piece.y;
221
222				i--;
223				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
224					Chess.getTileHTML(i, j).addClass("move");
225					i--;
226				}
227				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
228					Chess.getTileHTML(i, j).addClass("kill");
229				}
230
231
232				i = piece.x;
233				j = piece.y;
234				
235				j++;
236				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
237					Chess.getTileHTML(i, j).addClass("move");
238					j++;
239				}
240				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
241					Chess.getTileHTML(i, j).addClass("kill");
242				}
243
244
245				i = piece.x;
246				j = piece.y;
247
248				j--;
249				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
250					Chess.getTileHTML(i, j).addClass("move");
251					j--;
252				}
253				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
254					Chess.getTileHTML(i, j).addClass("kill");
255				}
256
257
258				$(".tile").on('click', function(){
259					if ($(this).hasClass("move")){
260						Chess.movePieces(piece, $(this).attr("data-x"), $(this).attr("data-y"));
261					}
262					else if ($(this).hasClass("kill")){
263						Chess.destroyPieces(Chess.tiles[$(this).attr("data-x")][$(this).attr("data-y")]);
264						Chess.movePieces(piece, $(this).attr("data-x"), $(this).attr("data-y"));
265					}
266					else{
267						Chess.HTMLsync();
268					}
269					$(".move").removeClass("move");
270					$(".kill").removeClass("kill");
271				})
272					
273
274			})
275
276		},
277
278		syncKnight : function(piece){
279			Chess.getTileHTML(piece.x, piece.y).html("knight").addClass("knight");
280			if (piece.color == "white"){
281				Chess.getTileHTML(piece.x, piece.y).addClass("white");
282			}
283			if (piece.color == "black"){
284				Chess.getTileHTML(piece.x, piece.y).addClass("black");
285			}
286
287			Chess.getTileHTML(piece.x, piece.y).on('click', function(){
288
289				if (Chess.tiles[piece.x+2] != undefined){
290					if (Chess.tiles[piece.x+2][piece.y+1] != undefined){
291						if (Chess.tiles[piece.x+2][piece.y+1] == "empty"){
292							Chess.getTileHTML(piece.x+2, piece.y+1).addClass("move");
293						}
294						else if (Chess.tiles[piece.x+2][piece.y+1].color != piece.color){
295							Chess.getTileHTML(piece.x+2, piece.y+1).addClass("kill");
296						}
297					}
298				}
299
300				if (Chess.tiles[piece.x+1] != undefined){
301					if (Chess.tiles[piece.x+1][piece.y+2] != undefined){
302						if (Chess.tiles[piece.x+1][piece.y+2] == "empty"){
303							Chess.getTileHTML(piece.x+1, piece.y+2).addClass("move");
304						}
305						else if (Chess.tiles[piece.x+1][piece.y+2].color != piece.color){
306							Chess.getTileHTML(piece.x+1, piece.y+2).addClass("kill");
307						}
308					}
309				}
310
311				if (Chess.tiles[piece.x-2] != undefined){
312					if (Chess.tiles[piece.x-2][piece.y-1] != undefined){
313						if (Chess.tiles[piece.x-2][piece.y-1] == "empty"){
314							Chess.getTileHTML(piece.x-2, piece.y-1).addClass("move");
315						}
316						else if (Chess.tiles[piece.x-2][piece.y-1].color != piece.color){
317							Chess.getTileHTML(piece.x-2, piece.y-1).addClass("kill");
318						}
319					}
320				}
321
322				if (Chess.tiles[piece.x-1] != undefined){
323					if (Chess.tiles[piece.x-1][piece.y-2] != undefined){
324						if (Chess.tiles[piece.x-1][piece.y-2] == "empty"){
325							Chess.getTileHTML(piece.x-1, piece.y-2).addClass("move");
326						}
327						else if (Chess.tiles[piece.x-1][piece.y-2].color != piece.color){
328							Chess.getTileHTML(piece.x-1, piece.y-2).addClass("kill");
329						}
330					}
331				}
332
333				if (Chess.tiles[piece.x-2] != undefined){
334					if (Chess.tiles[piece.x-2][piece.y+1] != undefined){
335						if (Chess.tiles[piece.x-2][piece.y+1] == "empty"){
336							Chess.getTileHTML(piece.x-2, piece.y+1).addClass("move");
337						}
338						else if (Chess.tiles[piece.x-2][piece.y+1].color != piece.color){
339							Chess.getTileHTML(piece.x-2, piece.y+1).addClass("kill");
340						}
341					}
342				}
343
344				if (Chess.tiles[piece.x-1] != undefined){
345					if (Chess.tiles[piece.x-1][piece.y+2] != undefined){
346						if (Chess.tiles[piece.x-1][piece.y+2] == "empty"){
347							Chess.getTileHTML(piece.x-1, piece.y+2).addClass("move");
348						}
349						else if (Chess.tiles[piece.x-1][piece.y+2].color != piece.color){
350							Chess.getTileHTML(piece.x-1, piece.y+2).addClass("kill");
351						}
352					}
353				}
354
355				if (Chess.tiles[piece.x+2] != undefined){
356					if (Chess.tiles[piece.x+2][piece.y-1] != undefined){
357						if (Chess.tiles[piece.x+2][piece.y-1] == "empty"){
358							Chess.getTileHTML(piece.x+2, piece.y-1).addClass("move");
359						}
360						else if (Chess.tiles[piece.x+2][piece.y-1].color != piece.color){
361							Chess.getTileHTML(piece.x+2, piece.y-1).addClass("kill");
362						}
363					}
364				}
365
366				if (Chess.tiles[piece.x+1] != undefined){
367					if (Chess.tiles[piece.x+1][piece.y-2] != undefined){
368						if (Chess.tiles[piece.x+1][piece.y-2] == "empty"){
369							Chess.getTileHTML(piece.x+1, piece.y-2).addClass("move");
370						}
371						else if (Chess.tiles[piece.x+1][piece.y-2].color != piece.color){
372							Chess.getTileHTML(piece.x+1, piece.y-2).addClass("kill");
373						}
374					}
375				}
376
377
378				$(".tile").on('click', function(){
379					if ($(this).hasClass("move")){
380						Chess.movePieces(piece, $(this).attr("data-x"), $(this).attr("data-y"));
381					}
382					else if ($(this).hasClass("kill")){
383						Chess.destroyPieces(Chess.tiles[$(this).attr("data-x")][$(this).attr("data-y")]);
384						Chess.movePieces(piece, $(this).attr("data-x"), $(this).attr("data-y"));
385					}
386					else{
387						Chess.HTMLsync();
388					}
389					$(".move").removeClass("move");
390					$(".kill").removeClass("kill");
391				})
392					
393
394			})
395
396		},
397
398		syncBishop : function(piece){
399			Chess.getTileHTML(piece.x, piece.y).html("bishop").addClass("bishop");
400			if (piece.color == "white"){
401				Chess.getTileHTML(piece.x, piece.y).addClass("white");
402			}
403			if (piece.color == "black"){
404				Chess.getTileHTML(piece.x, piece.y).addClass("black");
405			}
406
407			Chess.getTileHTML(piece.x, piece.y).on('click', function(){
408
409
410				var i = piece.x;
411				var j = piece.y;
412
413				i++;
414				j++;
415				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
416					Chess.getTileHTML(i, j).addClass("move");
417					i++;
418					j++;
419				}
420				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
421					Chess.getTileHTML(i, j).addClass("kill");
422				}
423
424
425				i = piece.x;
426				j = piece.y;
427
428				i--;
429				j--;
430				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
431					Chess.getTileHTML(i, j).addClass("move");
432					i--;
433					j--;
434				}
435				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
436					Chess.getTileHTML(i, j).addClass("kill");
437				}
438
439
440				i = piece.x;
441				j = piece.y;
442				
443				i--;
444				j++;
445				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
446					Chess.getTileHTML(i, j).addClass("move");
447					i--;
448					j++;
449				}
450				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
451					Chess.getTileHTML(i, j).addClass("kill");
452				}
453
454
455				i = piece.x;
456				j = piece.y;
457
458				i++
459				j--;
460				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
461					Chess.getTileHTML(i, j).addClass("move");
462					i++;
463					j--;
464				}
465				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
466					Chess.getTileHTML(i, j).addClass("kill");
467				}
468
469
470				$(".tile").on('click', function(){
471					if ($(this).hasClass("move")){
472						Chess.movePieces(piece, $(this).attr("data-x"), $(this).attr("data-y"));
473					}
474					else if ($(this).hasClass("kill")){
475						Chess.destroyPieces(Chess.tiles[$(this).attr("data-x")][$(this).attr("data-y")]);
476						Chess.movePieces(piece, $(this).attr("data-x"), $(this).attr("data-y"));
477					}
478					else{
479						Chess.HTMLsync();
480					}
481					$(".move").removeClass("move");
482					$(".kill").removeClass("kill");
483				})
484					
485
486			})
487
488		},
489
490		syncQueen : function(piece){
491			Chess.getTileHTML(piece.x, piece.y).html("queen").addClass("queen");
492			if (piece.color == "white"){
493				Chess.getTileHTML(piece.x, piece.y).addClass("white");
494			}
495			if (piece.color == "black"){
496				Chess.getTileHTML(piece.x, piece.y).addClass("black");
497			}
498
499			Chess.getTileHTML(piece.x, piece.y).on('click', function(){
500
501
502				var i = piece.x;
503				var j = piece.y;
504
505				i++;
506				j++;
507				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
508					Chess.getTileHTML(i, j).addClass("move");
509					i++;
510					j++;
511				}
512				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
513					Chess.getTileHTML(i, j).addClass("kill");
514				}
515
516
517				i = piece.x;
518				j = piece.y;
519
520				i--;
521				j--;
522				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
523					Chess.getTileHTML(i, j).addClass("move");
524					i--;
525					j--;
526				}
527				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
528					Chess.getTileHTML(i, j).addClass("kill");
529				}
530
531
532				i = piece.x;
533				j = piece.y;
534				
535				i--;
536				j++;
537				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
538					Chess.getTileHTML(i, j).addClass("move");
539					i--;
540					j++;
541				}
542				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
543					Chess.getTileHTML(i, j).addClass("kill");
544				}
545
546
547				i = piece.x;
548				j = piece.y;
549
550				i++;
551				j--;
552				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
553					Chess.getTileHTML(i, j).addClass("move");
554					i++;
555					j--;
556				}
557				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
558					Chess.getTileHTML(i, j).addClass("kill");
559				}
560
561				i = piece.x;
562				j = piece.y;
563
564				i++;
565				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
566					Chess.getTileHTML(i, j).addClass("move");
567					i++;
568				}
569				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
570					Chess.getTileHTML(i, j).addClass("kill");
571				}
572
573
574				i = piece.x;
575				j = piece.y;
576
577				i--;
578				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
579					Chess.getTileHTML(i, j).addClass("move");
580					i--;
581				}
582				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
583					Chess.getTileHTML(i, j).addClass("kill");
584				}
585
586
587				i = piece.x;
588				j = piece.y;
589				
590				j++;
591				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
592					Chess.getTileHTML(i, j).addClass("move");
593					j++;
594				}
595				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
596					Chess.getTileHTML(i, j).addClass("kill");
597				}
598
599
600				i = piece.x;
601				j = piece.y;
602
603				j--;
604				while(Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && Chess.tiles[i][j] == "empty"){
605					Chess.getTileHTML(i, j).addClass("move");
606					j--;
607				}
608				if (Chess.tiles[i] != undefined && Chess.tiles[i][j] != undefined && piece.color != Chess.tiles[i][j].color){
609					Chess.getTileHTML(i, j).addClass("kill");
610				}
611
612
613				$(".tile").on('click', function(){
614					if ($(this).hasClass("move")){
615						Chess.movePieces(piece, $(this).attr("data-x"), $(this).attr("data-y"));
616					}
617					else if ($(this).hasClass("kill")){
618						Chess.destroyPieces(Chess.tiles[$(this).attr("data-x")][$(this).attr("data-y")]);
619						Chess.movePieces(piece, $(this).attr("data-x"), $(this).attr("data-y"));
620					}
621					else{
622						Chess.HTMLsync();
623					}
624					$(".move").removeClass("move");
625					$(".kill").removeClass("kill");
626				})
627					
628
629			})
630
631		},
632
633		syncKing : function(piece){
634			Chess.getTileHTML(piece.x, piece.y).html("king").addClass("king");
635			if (piece.color == "white"){
636				Chess.getTileHTML(piece.x, piece.y).addClass("white");
637			}
638			if (piece.color == "black"){
639				Chess.getTileHTML(piece.x, piece.y).addClass("black");
640			}
641
642			Chess.getTileHTML(piece.x, piece.y).on('click', function(){
643
644				i = piece.x;
645				j = piece.y;
646
647
648				if (Chess.tiles[i+1] != undefined && Chess.tiles[i+1][j] != undefined){
649					if (Chess.tiles[i+1][j] == "empty"){
650						Chess.getTileHTML(i+1, j).addClass("move");
651					}
652					else if (Chess.tiles[i+1][j].color != piece.color){
653						Chess.getTileHTML(i+1, j).addClass("kill");
654					}
655				}
656
657				if (Chess.tiles[i+1] != undefined && Chess.tiles[i+1][j+1] != undefined){
658					if (Chess.tiles[i+1][j+1] == "empty"){
659						Chess.getTileHTML(i+1, j+1).addClass("move");
660					}
661					else if (Chess.tiles[i+1][j+1].color != piece.color){
662						Chess.getTileHTML(i+1, j+1).addClass("kill");
663					}
664				}
665
666				if (Chess.tiles[i] != undefined && Chess.tiles[i][j+1] != undefined){
667					if (Chess.tiles[i][j+1] == "empty"){
668						Chess.getTileHTML(i, j+1).addClass("move");
669					}
670					else if (Chess.tiles[i][j+1].color != piece.color){
671						Chess.getTileHTML(i, j+1).addClass("kill");
672					}
673				}
674
675				if (Chess.tiles[i-1] != undefined && Chess.tiles[i-1][j] != undefined){
676					if (Chess.tiles[i-1][j] == "empty"){
677						Chess.getTileHTML(i-1, j).addClass("move");
678					}
679					else if (Chess.tiles[i-1][j].color != piece.color){
680						Chess.getTileHTML(i-1, j).addClass("kill");
681					}
682				}
683
684				if (Chess.tiles[i-1] != undefined && Chess.tiles[i-1][j+1] != undefined){
685					if (Chess.tiles[i-1][j+1] == "empty"){
686						Chess.getTileHTML(i-1, j+1).addClass("move");
687					}
688					else if (Chess.tiles[i-1][j+1].color != piece.color){
689						Chess.getTileHTML(i-1, j+1).addClass("kill");
690					}
691				}
692
693				if (Chess.tiles[i-1] != undefined && Chess.tiles[i-1][j-1] != undefined){
694					if (Chess.tiles[i-1][j-1] == "empty"){
695						Chess.getTileHTML(i-1, j-1).addClass("move");
696					}
697					else if (Chess.tiles[i-1][j-1].color != piece.color){
698						Chess.getTileHTML(i-1, j-1).addClass("kill");
699					}
700				}
701
702				if (Chess.tiles[i+1] != undefined && Chess.tiles[i+1][j-1] != undefined){
703					if (Chess.tiles[i+1][j-1] == "empty"){
704						Chess.getTileHTML(i+1, j-1).addClass("move");
705					}
706					else if (Chess.tiles[i+1][j-1].color != piece.color){
707						Chess.getTileHTML(i+1, j-1).addClass("kill");
708					}
709				}
710
711				if (Chess.tiles[i] != undefined && Chess.tiles[i][j-1] != undefined){
712					if (Chess.tiles[i][j-1] == "empty"){
713						Chess.getTileHTML(i, j-1).addClass("move");
714					}
715					else if (Chess.tiles[i][j-1].color != piece.color){
716						Chess.getTileHTML(i, j-1).addClass("kill");
717					}
718				}
719
720				$(".tile").on('click', function(){
721					if ($(this).hasClass("move")){
722						Chess.movePieces(piece, $(this).attr("data-x"), $(this).attr("data-y"));
723					}
724					else if ($(this).hasClass("kill")){
725						Chess.destroyPieces(Chess.tiles[$(this).attr("data-x")][$(this).attr("data-y")]);
726						Chess.movePieces(piece, $(this).attr("data-x"), $(this).attr("data-y"));
727					}
728					else{
729						Chess.HTMLsync();
730					}
731					$(".move").removeClass("move");
732					$(".kill").removeClass("kill");
733				})
734					
735
736			})
737
738		},
739
740		movePieces : function(piece, x, y){
741			this.tiles[piece.x][piece.y] = "empty";
742			piece.x = parseInt(x, 10);
743			piece.y = parseInt(y, 10);
744			this.tiles[piece.x][piece.y] = piece;
745			this.HTMLsync();
746		},
747
748		destroyPieces : function(piece){
749			piece.alive = false;
750			this.HTMLsync;
751		},
752
753		getTileHTML : function(x, y){
754			return $("#"+x+"-"+y);
755		}
756
757	}
758
759
760})(jQuery);
761
762Chess.init();