if (window.Node && Node.prototype && !Node.prototype.contains) { Node.prototype.contains = function (arg) { return !!(this.compareDocumentPosition(arg) & 16) } }
(function($) { //Make nodes selectable by expression $.extend($.expr[':'], { chess: "(' '+a.className+' ').indexOf(' game- chess ')" }); $.fn.chess = function(o) { return this.each(function() { new $.chess(this,o); }); } //Macros for external methods that support chaining var methods = "destroy,enable,disable,refresh".split(","); for(var i=0;i<methods.length;i++) { var cur = methods[i], f; eval('f = function() { var a = arguments; return this.each(function() { if(jQuery(this).is(".game-chess")) jQuery.data(this, "game-chess")["'+cur+'"](a); }); }'); $.fn["chess"+cur.substr(0,1).toUpperCase()+cur.substr(1)] = f; }; //get instance method $.fn.chessInstance = function() { if($(this[0]).is(".game-chess")) return $.data(this[0], "game- chess"); return false; }; $.chess = function(el,o) { this.element = el; this.set = []; var options = {}; var layout = []; layout["standard"] = { gridSize: 8, description: "A standard game of chess on an 8x8 board", pieces: ["rook", "knight", "bishop", "queen", "king","pawn"], startgrid: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" } layout["grand"] = { gridSize:10, description: "A game of Grand chess on an special 10x10 board", pieces: ["rook", "knight", "bishop", "queen", "king","pawn","cardinal","marshall"], startgrid: "r8r/1nbqkmcbn1/pppppppppp/10/10/10/10/PPPPPPPPPP/ 1NBQKMCBN1/R8R" } layout["omega"] = { gridSize: 10, description: "A game of Omega chess on an special 10x10 board", pieces:["rook", "knight", "bishop", "queen", "king","pawn","champion","wizard"], specials: ["W","W","w","w"], startgrid: "crnbqkbnrc/pppppppppp/10/10/10/10/10/10/PPPPPPPPPP/ CRNBQKBNRC" } var images = []; var self = this; $.data(this.element, "game-chess", this); $(el).addClass("game-chess"); $.extend(options, o); $.extend(options, { serverURL:null, highlight:[], imagePath:options.imagePath != undefined ? options.imagePath: "standard", imageSize:options.imageSize != undefined ? options.imageSize: 24, check: [], moveID:1, pawnDirection:1, checkRoute:[], incheck: [], KingCoords: [], previousPiece:"", whosmove:"white", letters:[], revletters: ",A,B,C,D,E,F,G,H,I,J,K,L,M,N,O".split(","), items: options.items || '> table tr td.square', gametype: options.gametype != undefined ? options.gametype: "standard", containment: options.containment ? (options.containment == 'game- chess' ? el : options.containment) : null, zIndex: options.zIndex || 1000, _start: function(h,p,c,t,e) { self.start.apply(t, [self, e]); // Trigger the onStart callback }, _beforeStop: function(h,p,c,t,e) { self.stop.apply(t, [self, e]); // Trigger the onStart callback }, _drag: function(h,p,c,t,e) { self.drag.apply(t, [self, e]); // Trigger the onStart callback }, startCondition: function() { return !self.disabled; } }); //Get the items var items = $(options.items, el); for (i=0;i<options.revletters.length;i++){ options.letters[options.revletters[i]]=i; } //Add current items to the set items.each(function() { self.set.push([this,null]); }); this.options = options; this.layout = layout; this.images = images; this.start(); } $.extend($.chess.prototype, { plugins: {}, currentTarget: null, lastTarget: null, prepareCallbackObj: function(self, that) { return { draggable: self, current: that, options: self.options } }, refresh: function() { //Get the items var self = this; var items = $(this.options.items, this.element); var unique = []; items.each(function() { old = false; for(var i=0;i<self.set.length;i++) { if(self.set[i][0] == this) old = true; } if(!old) unique.push(this); }); for(var i=0;i<unique.length;i++) { new $.game.mouseInteraction(unique[i],self.options); } //Add current items to the set this.set = []; items.each(function() { self.set.push([this,null]); }); }, destroy: function() { $(this.element).removeClass("game-chess").removeClass("game-chess- disabled"); $(this.options.items, this.element).mouseInteractionDestroy(); }, enable: function() { $(this.element).removeClass("game-chess-disabled"); this.disabled = false; }, disable: function() { $(this.element).addClass("game-chess-disabled"); this.disabled = true; }, start: function(that, e) { var o = this.options; this.StartQuestion(); //if (confirm("Would you like a game of Thermal Global Nuclear chess")){ // this.generateBoard(); //} return false; }, StartQuestion: function(){ var el = this.element; strHTML = "<h1>What type of chess would you like to play?</h1><ul id='gameoptions'>"; for(game in this.layout){ strHTML += "<li gametype='"+game +"'>"+this.layout[game].description+"</li>" } strHTML += "</ul>" el.innerHTML = strHTML var chessboard = this $("#"+el.id+" ul#gameoptions li").click(function(){ chessboard.options.gametype = this.getAttribute("gametype"); chessboard.generateBoard(el); }); }, generateBoard: function(el){ var gridSize = this.layout[this.options.gametype].gridSize; var HTMLTable = "<h1 id='whosmove'>Whites Move</h1><table class='board'>" var startColour = "white" var found = true; var rowspan = gridSize+1 if(this.options.gametype=="omega"){ rowspan = gridSize+2 } Writeup = "<td rowspan='"+(rowspan)+"' class='whiteMove'><strong>White</strong><br/></td>"; Writeup += "<td rowspan='"+(rowspan)+"' class='blackMove'><strong>Black</strong><br/></td>"; if(this.options.gametype=="omega"){ HTMLTable += "<tr><td class='legend'> </td>"; HTMLTable += "<td id='W-4' class='square white'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td id='W-3' class='square black'> </ td>"+Writeup+"</tr>"; Writeup = "" } for(colIndex=gridSize;colIndex>0;colIndex--){ HTMLTable += "<tr><td class='legend'>"+colIndex+"</td>"; if(this.options.gametype=="omega"){ HTMLTable += "<td class='legend'> </td>"; } for(rowIndex=1;rowIndex<=gridSize;rowIndex++){ HTMLTable += "<td id='"+this.options.revletters[rowIndex]+"-"+ (colIndex)+"' class='square "+startColour+"'> </td>"; if(startColour=="white"){ startColour="black"; }else{ startColour="white"; } } if(this.options.gametype=="omega"){ HTMLTable += "<td class='legend'> </td>"; } if (found){ HTMLTable += Writeup found=false } HTMLTable += "</tr>"; if(startColour=="white"){ startColour="black"; }else{ startColour="white"; } } if(this.options.gametype=="omega"){ HTMLTable += "<tr><td class='legend'> </td>"; HTMLTable += "<td id='W-1' class='square black'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td class='legend'> </td>"; HTMLTable += "<td id='W-2' class='square white'> </td></tr>"; } HTMLTable += "<tr><td class='legend'> </td>"; if(this.options.gametype=="omega"){ HTMLTable += "<td class='legend'> </td>"; } for(rowIndex=1;rowIndex<=gridSize;rowIndex++){ HTMLTable += "<td class='legend'>"+this.options.revletters[rowIndex]+"</td>"; } HTMLTable += "</table>" el.innerHTML = HTMLTable for (i=0;i<this.layout[this.options.gametype].pieces.length;i++){ this.images["white-"+this.layout[this.options.gametype].pieces[i]] = new Image; this.images["white-"+this.layout[this.options.gametype].pieces[i]].src = "images/"+this.options.imagePath+"/"+this.options.imageSize+"/ white-"+this.layout[this.options.gametype].pieces[i]+".gif"; this.images["black-"+this.layout[this.options.gametype].pieces[i]] = new Image; this.images["black-"+this.layout[this.options.gametype].pieces[i]].src = "images/"+this.options.imagePath+"/"+this.options.imageSize+"/ black-"+this.layout[this.options.gametype].pieces[i]+".gif"; } $("table.board td").css("width",(this.options.imageSize+6)+"px"); $("table.board td").css("height",(this.options.imageSize+6)+"px"); renderBoard(this.layout[this.options.gametype].startgrid,this); }, placePiece: function(square, piece, chessboard){ var obj = $("#"+square).get(0); var idInfo = obj.id.split("-"); if(piece!=""){ var partsOfPiece = piece.split("-"); if(partsOfPiece[1]=="king"){ chessboard.options.KingCoords[partsOfPiece[0]] = square; } obj.innerHTML = "<img title='"+piece+"' src='"+ chessboard.images[piece].src +"'>"; obj.setAttribute("piece",piece); } else { obj.innerHTML = " "; } $(obj).click(function(e){ addClickToSquare(this,chessboard) }); } }); function renderBoard(grid,chessboard){ var tmpGrid = grid.split("/"); var newGrid = []; for(i=0;i<chessboard.layout[chessboard.options.gametype].gridSize;i+ +){ newGrid[newGrid.length]=[]; } var c=0; for(i=tmpGrid.length;i>0;i--){ for (z=0;z<tmpGrid[i-1].length;z++){ var key = tmpGrid[i-1].substr(z,1) if(chessboard.options.gametype!="standard"){ var tmpKey = tmpGrid[i-1].substr(z,2) if((tmpKey*1)+""==tmpKey){ key = tmpKey; } } if((key*1)+""==key){ for(x=0;x<key*1;x++){ newGrid[c][newGrid[c].length] ="" } } if(key=="r"){ newGrid[c][newGrid[c].length] = "black-rook"; } if(key=="n"){ newGrid[c][newGrid[c].length] = "black-knight"; } if(key=="b"){ newGrid[c][newGrid[c].length] = "black-bishop"; } if(key=="q"){ newGrid[c][newGrid[c].length] = "black-queen"; } if(key=="k"){ newGrid[c][newGrid[c].length] = "black-king"; } if(key=="p"){ newGrid[c][newGrid[c].length] = "black-pawn"; } if(key=="R"){ newGrid[c][newGrid[c].length] = "white-rook"; } if(key=="N"){ newGrid[c][newGrid[c].length] = "white-knight"; } if(key=="B"){ newGrid[c][newGrid[c].length] = "white-bishop"; } if(key=="Q"){ newGrid[c][newGrid[c].length] = "white-queen"; } if(key=="K"){ newGrid[c][newGrid[c].length] = "white-king"; } if(key=="P"){ newGrid[c][newGrid[c].length] = "white-pawn"; } if(chessboard.options.gametype=="omega"){ if(key=="c"){ newGrid[c][newGrid[c].length] = "black-champion"; } if(key=="w"){ newGrid[c][newGrid[c].length] = "black-wizard"; } if(key=="C"){ newGrid[c][newGrid[c].length] = "white-champion"; } if(key=="W"){ newGrid[c][newGrid[c].length] = "white-wizard"; } } if(chessboard.options.gametype=="grand"){ if(key=="c"){ newGrid[c][newGrid[c].length] = "black-cardinal"; } if(key=="m"){ newGrid[c][newGrid[c].length] = "black-marshall"; } if(key=="C"){ newGrid[c][newGrid[c].length] = "white-cardinal"; } if(key=="M"){ newGrid[c][newGrid[c].length] = "white-marshall"; } } } // alert(newGrid[c].join("','")) c++; } grid = newGrid; for(file=0;file<grid.length;file++){ for(r=0;r<grid[file].length;r++){ chessboard.placePiece(chessboard.options.revletters[file+1]+"-"+(r +1),grid[r][file],chessboard); } } if(chessboard.options.gametype=="omega"){ chessboard.placePiece("W-4","black-wizard",chessboard); chessboard.placePiece("W-3","black-wizard",chessboard); chessboard.placePiece("W-2","white-wizard",chessboard); chessboard.placePiece("W-1","white-wizard",chessboard); } } function addClickToSquare(obj,chessboard){ //var obj = $("#"+square).get(0); chessboard.options.highlight=[]; if(!chessboard.options.GameOver){ var gridLocation = obj.id.split("-"); var piece = obj.getAttribute("piece"); if(obj.className.indexOf("highlight")>=0){ // work out encoding var MovingPiece = $ ("#"+chessboard.options.previousPiece).attr("piece") if (MovingPiece){ var partsOfPiece =MovingPiece.split("-"); $("#"+obj.id).attr("tmppiece",MovingPiece); // mark destination ans holding this piece for checking purposes $ ("#"+chessboard.options.previousPiece).attr("hiddenpiece","1"); /// mark this piece as hidden if(partsOfPiece[1]=="king"){ tmp = chessboard.options.KingCoords[partsOfPiece[0]]; chessboard.options.KingCoords[partsOfPiece[0]] = obj.id; } var yourKingInCheck = checkMyKing(partsOfPiece[0],chessboard); $("#"+obj.id).attr("tmppiece",""); $("#"+chessboard.options.previousPiece).attr("hiddenpiece",""); if(yourKingInCheck){ alert("That move puts you in check"); if(partsOfPiece[1]=="king"){ chessboard.options.KingCoords[partsOfPiece[0]] = tmp; } } else { mark(partsOfPiece[1], obj.id.split("-"), "check", chessboard); // work out move and check that king not in check if(partsOfPiece[1]=="knight"){ partsOfPiece[1]="N" } var piece = partsOfPiece[1].substr(0,1).toUpperCase(); if(piece=="P"){ piece=""; } var moveid = partsOfPiece[0]+"-"+chessboard.options.moveID; chessboard.options.moveID++; var taking = $("#"+obj.id).attr("piece"); if(taking) { takingParts = taking.split("-"); if(takingParts[0]!=partsOfPiece[0] && takingParts[1]=="king"){ GameOver = true; } var Move = piece+"x"+ (obj.id.split("-").join("").toLowerCase()); } else { var Move = piece+""+ (obj.id.split("-").join("").toLowerCase()); } if(chessboard.options.check.length>0){ Move+="+"; } if(chessboard.options.serverURL){ $.getJSON(chessboard.options.serverURL+"? cmd=move&colour="+partsOfPiece[0]+"&move="+Move) } // end work out encoding // add code here for EnPasson var square = $ ("#"+chessboard.options.previousPiece).get(0).innerHTML; // upgrade pawn to piece select Queen as default if(partsOfPiece[1]=="pawn"){ if(partsOfPiece[0]=="black" && obj.id.split("-")[1] == "1"){ square = "<img title='"+partsOfPiece[0]+"-queen' src='"+ chessboard.images[partsOfPiece[0]+"-queen"].src +"'>"; MovingPiece="black-queen"; } if(partsOfPiece[0]=="white" && obj.id.split("-")[1] == chessboard.layout[chessboard.options.gametype].gridSize){ square = "<img title='"+partsOfPiece[0]+"-queen' src='"+ chessboard.images[partsOfPiece[0]+"-queen"].src +"'>"; MovingPiece="white-queen"; } } if (partsOfPiece[1]=="king"){ chessboard.options.KingCoords[partsOfPiece[0]] = obj.id; } $("#"+obj.id).get(0).innerHTML = square; $("#"+obj.id).attr("piece",MovingPiece); $("#"+chessboard.options.previousPiece).get(0).innerHTML= " "; $("#"+chessboard.options.previousPiece).attr("piece",""); chessboard.options.previousPiece = ""; checkMsg=""; if(chessboard.options.check.length>0){ chessboard.options.pawnDirection *= -1; // if in check then check for mate var checkmate = checkForMate(chessboard,chessboard.options.whosmove); if(checkmate){ checkMsg = " <blink>(Checkmate)</blink>"; chessboard.options.GameOver=true; } else { checkMsg = " <blink>(Check)</blink>"; } } if(!chessboard.options.GameOver){ var msg = "" if(chessboard.options.whosmove=="white"){ chessboard.options.whosmove="black" msg = "Blacks Move"; } else { chessboard.options.whosmove="white" msg = "Whites Move"; } $("#whosmove").get(0).innerHTML =msg + checkMsg; } else { if(chessboard.options.whosmove=="white"){ $("#whosmove").get(0).innerHTML ="<blink>White Wins</blink>"; } else { $("#whosmove").get(0).innerHTML ="<blink>Black Wins</blink>"; } } $("."+partsOfPiece[0]+"Move").get(0).innerHTML += "<abbr id='"+moveid+"' title='"+generateBoardDescription(chessboard)+"'>"+Move +"</abbr><br>"; $("."+partsOfPiece[0]+"Move abbr").click(function(e){ renderBoard(this.title,chessboard) }); } } } else { if(piece){ chessboard.options.previousPiece = obj.id pieces = piece.split("-") myColour = pieces[0]; if(chessboard.options.whosmove==myColour){ if(myColour=="black"){ chessboard.options.pawnDirection = 1; } else { chessboard.options.pawnDirection = -1; } mark(pieces[1], obj.id.split("-"), "movement", chessboard); } } } } // unhighlight $("td.square").removeClass("highlight"); $("td.square").removeClass("incheck"); // highlight location we can move to for(var i =0;i<chessboard.options.highlight.length;i++){ $(chessboard.options.highlight[i]).addClass("highlight"); } for(var i =0;i<chessboard.options.check.length;i++){ $(chessboard.options.check[i]).addClass("incheck"); } chessboard.options.check.length=0; } function removeHighlight(i){ $(this).removeClass("highlight"); } function generateCoordList (gridLocation,paths,chessboard){ var coordList = { endlist:[], CList:paths, coordinate:[] }; var CList= ["MM","MP","PM","PP","LEFT","RIGHT","UP","DOWN"]; for (index =0; index<CList.length;index++){ coordList.endlist[CList[index]] = "0"; coordList.coordinate[CList[index]] = []; } if(chessboard.options.gametype=="omega"){ if(gridLocation[0]=="W"){ for(var c =1;c<=chessboard.layout[chessboard.options.gametype].gridSize;c++){ if(gridLocation[1]=="1"){ x=-1; y=-1; if(x-c>0 && x- c<chessboard.layout[chessboard.options.gametype].gridSize && y-c>=0 && y-c<chessboard.layout[chessboard.options.gametype].gridSize){ coordList.coordinate["MM"][coordList.coordinate["MM"].length] = "#"+chessboard.options.revletters[x-c]+"-"+(y-c); // if omega add rules if(chessboard.options.gametype=="omega"){ if("#"+chessboard.options.revletters[x-c]+"-"+(y-c) == "#A-1") { coordList.coordinate["MM"] [coordList.coordinate["MM"].length] = "#W-1"; } } } } if(gridLocation[1]=="4"){ x=-1; y=11; if(x-c>=0 && x- c<chessboard.layout[chessboard.options.gametype].gridSize && y+c>=0 && y+c<=chessboard.layout[chessboard.options.gametype].gridSize){ coordList.coordinate["MP"][coordList.coordinate["MP"].length] = "#"+chessboard.options.revletters[x-c]+"-"+(y+c); // if omega add rules if(chessboard.options.gametype=="omega"){ if("#"+chessboard.options.revletters[x-c]+"-"+(y+c) == "#A-10"){ coordList.coordinate["MM"] [coordList.coordinate["MM"].length] = "#W-4"; } } } } if(gridLocation[1]=="2"){ x=11; y=-1; if(x+c>=0 && x +c<chessboard.layout[chessboard.options.gametype].gridSize && y-c>=0 && y-c<chessboard.layout[chessboard.options.gametype].gridSize){ coordList.coordinate["PM"][coordList.coordinate["PM"].length] = "#"+chessboard.options.revletters[x+c]+"-"+(y-c); // if omega add rules if(chessboard.options.gametype=="omega"){ if("#"+chessboard.options.revletters[x+c]+"-"+(y-c) == "#J-10"){ coordList.coordinate["MM"] [coordList.coordinate["MM"].length] = "#W-2"; } } } } if(gridLocation[1]=="3"){ x=11; y=11; if(x+c>=0 && x +c<chessboard.layout[chessboard.options.gametype].gridSize && y+c>=0 && y+c<chessboard.layout[chessboard.options.gametype].gridSize){ coordList.coordinate["PP"][coordList.coordinate["PP"].length] = "#"+chessboard.options.revletters[x+c]+"-"+(y+c); // if omega add rules if(chessboard.options.gametype=="omega"){ if("#"+chessboard.options.revletters[x+c]+"-"+(y+c) == "#J-1") { coordList.coordinate["MM"] [coordList.coordinate["MM"].length] = "#W-3"; } } } } } } } var x = chessboard.options.letters[gridLocation[0]]; var y = (gridLocation[1]*1); for(var c =1;c<=chessboard.layout[chessboard.options.gametype].gridSize;c++){ if(x-c>0 && x- c<=chessboard.layout[chessboard.options.gametype].gridSize && y-c>0 && y-c<=chessboard.layout[chessboard.options.gametype].gridSize){ coordList.coordinate["MM"][coordList.coordinate["MM"].length] = "#"+chessboard.options.revletters[x-c]+"-"+(y-c); // if omega add rules if(chessboard.options.gametype=="omega"){ if("#"+chessboard.options.revletters[x-c]+"-"+(y-c) == "#A-1"){ coordList.coordinate["MM"][coordList.coordinate["MM"].length] = "#W-1"; } } } if(x-c>0 && x- c<=chessboard.layout[chessboard.options.gametype].gridSize && y+c>0 && y+c<=chessboard.layout[chessboard.options.gametype].gridSize){ coordList.coordinate["MP"][coordList.coordinate["MP"].length] = "#"+chessboard.options.revletters[x-c]+"-"+(y+c); // if omega add rules if(chessboard.options.gametype=="omega"){ if("#"+chessboard.options.revletters[x-c]+"-"+(y+c) == "#A-10"){ coordList.coordinate["MM"][coordList.coordinate["MM"].length] = "#W-4"; } } } if(x+c>=0 && x +c<=chessboard.layout[chessboard.options.gametype].gridSize && y-c>0 && y-c<=chessboard.layout[chessboard.options.gametype].gridSize){ coordList.coordinate["PM"][coordList.coordinate["PM"].length] = "#"+chessboard.options.revletters[x+c]+"-"+(y-c); // if omega add rules if(chessboard.options.gametype=="omega"){ if("#"+chessboard.options.revletters[x+c]+"-"+(y-c) == "#J-10"){ coordList.coordinate["MM"][coordList.coordinate["MM"].length] = "#W-2"; } } } if(x+c>=0 && x +c<=chessboard.layout[chessboard.options.gametype].gridSize && y+c>0 && y+c<=chessboard.layout[chessboard.options.gametype].gridSize){ coordList.coordinate["PP"][coordList.coordinate["PP"].length] = "#"+chessboard.options.revletters[x+c]+"-"+(y+c); // if omega add rules if(chessboard.options.gametype=="omega"){ if("#"+chessboard.options.revletters[x+c]+"-"+(y+c) == "#J-1"){ coordList.coordinate["MM"][coordList.coordinate["MM"].length] = "#W-3"; } } } if(x-c>0 && x- c<=chessboard.layout[chessboard.options.gametype].gridSize){ coordList.coordinate["LEFT"][coordList.coordinate["LEFT"].length] = "#"+chessboard.options.revletters[x-c]+"-"+(y); } if(x+c>=0 && x +c<=chessboard.layout[chessboard.options.gametype].gridSize){ coordList.coordinate["RIGHT"] [coordList.coordinate["RIGHT"].length] = "#"+chessboard.options.revletters[x+c]+"-"+(y); } if(y+c>0 && y +c<=chessboard.layout[chessboard.options.gametype].gridSize){ coordList.coordinate["UP"][coordList.coordinate["UP"].length] = "#"+chessboard.options.revletters[x]+"-"+(y+c); } if(y-c>0 && y- c<=chessboard.layout[chessboard.options.gametype].gridSize){ coordList.coordinate["DOWN"][coordList.coordinate["DOWN"].length] = "#"+chessboard.options.revletters[x]+"-"+(y-c); } } return coordList; } function checkPaths(gridLocation,TypeOfCheck,chessboard, checkPathsInfo,CList){ var checkRoute = ""; for (index =0; index<CList.length;index++){ for (coordIndex =0; coordIndex<checkPathsInfo.coordinate[CList[index]].length;coordIndex++) { if(checkPathsInfo.endlist[CList[index]] == "0"){ var key = checkPathsInfo.coordinate[CList[index]][coordIndex]; var checkPiece = $(key).attr("piece"); if(checkPiece){ checkPathsInfo.endlist[CList[index]] = "1"; } previousLengthOfCheckList = chessboard.options.check.length; setMarker(key,checkPiece,TypeOfCheck,chessboard); if(TypeOfCheck=="check" && chessboard.options.check.length > previousLengthOfCheckList){ checkRoute = CList[index]; } } } } if(TypeOfCheck=="check" && checkRoute!=""){ chessboard.options.checkRoute.length =0; chessboard.options.checkRoute[chessboard.options.checkRoute.length] = "#"+gridLocation.join("-"); for (coordIndex =0; coordIndex<checkPathsInfo.coordinate[checkRoute].length;coordIndex++){ var key = checkPathsInfo.coordinate[checkRoute][coordIndex]; var checkPiece = $(key).attr("piece"); if(!checkPiece){ chessboard.options.checkRoute[chessboard.options.checkRoute.length] = key; } } } } function checkJumps(gridLocation, TypeOfCheck,chessboard,coords){ for(var c =0;c<coords.length;c++){ var x = chessboard.options.letters[gridLocation[0]]+coords[c][0]; var y = (gridLocation[1]*1)+coords[c][1]; if(x>=0 && x<=chessboard.layout[chessboard.options.gametype].gridSize && y>0 && y<=chessboard.layout[chessboard.options.gametype].gridSize){ var coordinate = "#"+chessboard.options.revletters[x]+"-"+(y) var checkPiece = $(coordinate).attr("piece"); previousLengthOfCheckList = chessboard.options.check.length; setMarker(coordinate,checkPiece,TypeOfCheck,chessboard); if(TypeOfCheck=="check" && chessboard.options.check.length > previousLengthOfCheckList){ chessboard.options.checkRoute.length =0; chessboard.options.checkRoute[chessboard.options.checkRoute.length] = "#"+gridLocation.join("-"); } } } } function CheckBishop(gridLocation,TypeOfCheck,chessboard){ var CList= ["MM","MP","PM","PP"]; var checkPathList = generateCoordList (gridLocation,CList,chessboard); checkPaths(gridLocation,TypeOfCheck,chessboard,checkPathList,CList) // if omega add rules if(chessboard.options.gametype=="omega1"){ var wizardPoints = []; wizardPoints["W-1"] = ["#W-3"]; wizardPoints["W-2"] = ["#W-4"]; wizardPoints["W-3"] = ["#W-1"]; wizardPoints["W-4"] = ["#W-2"]; var gSize = chessboard.layout[chessboard.options.gametype].gridSize; for(i = 1; i<gSize;i++){ wizardPoints["W-1"][wizardPoints["W-1"].length] = "#"+chessboard.options.revletters[i]+"-"+(i); wizardPoints["W-2"][wizardPoints["W-2"].length] = "#"+chessboard.options.revletters[i]+"-"+(gSize-i); wizardPoints["W-3"][wizardPoints["W-3"].length] = "#"+chessboard.options.revletters[gSize-i]+"-"+(gSize-i); wizardPoints["W-4"][wizardPoints["W-4"].length] = "#"+chessboard.options.revletters[gSize-i]+"-"+(i); } checkWizardPointsPath(wizardPoints,gridLocation, TypeOfCheck,chessboard); } } function CheckQueen(gridLocation,TypeOfCheck,chessboard){ var CList= ["MM","MP","PM","PP","LEFT","RIGHT","UP","DOWN"]; var checkPathList = generateCoordList (gridLocation,CList,chessboard); checkPaths(gridLocation,TypeOfCheck,chessboard,checkPathList,CList) if(chessboard.options.gametype=="omega1"){ var wizardPoints = []; wizardPoints["W-1"] = ["#W-3"]; wizardPoints["W-2"] = ["#W-4"]; wizardPoints["W-3"] = ["#W-1"]; wizardPoints["W-4"] = ["#W-2"]; var gSize = chessboard.layout[chessboard.options.gametype].gridSize; for(i = 1; i<gSize;i++){ wizardPoints["W-1"][wizardPoints["W-1"].length] = "#"+chessboard.options.revletters[i]+"-"+(i); wizardPoints["W-2"][wizardPoints["W-2"].length] = "#"+chessboard.options.revletters[i]+"-"+(gSize-i); wizardPoints["W-3"][wizardPoints["W-3"].length] = "#"+chessboard.options.revletters[gSize-i]+"-"+(gSize-i); wizardPoints["W-4"][wizardPoints["W-4"].length] = "#"+chessboard.options.revletters[gSize-i]+"-"+(i); } checkWizardPointsPath(wizardPoints,gridLocation, TypeOfCheck,chessboard); } } function CheckChampion(gridLocation, TypeOfCheck,chessboard){ var coords = [[-2,-2],[-2,0],[-2,+2],[+2,-2],[+2,0],[+2,+2],[0,-2], [0,+2],[+1,0],[-1,0],[0,-1],[0,+1]]; checkJumps(gridLocation, TypeOfCheck,chessboard,coords); // if omega add rules if(chessboard.options.gametype=="omega"){ var wizardPoints = []; wizardPoints["W-1"] = ["#B-2"]; wizardPoints["W-2"] = ["#I-2"]; wizardPoints["W-3"] = ["#I-9"]; wizardPoints["W-4"] = ["#B-9"]; checkWizardPoints(wizardPoints,gridLocation, TypeOfCheck,chessboard); } } function CheckKnight(gridLocation, TypeOfCheck,chessboard){ var coords = [[-2,-1],[-2,+1],[+2,-1],[+2,+1],[-1,-2],[-1,+2], [+1,-2],[+1,+2]]; checkJumps(gridLocation, TypeOfCheck,chessboard,coords); // if omega add rules if(chessboard.options.gametype=="omega"){ var wizardPoints = []; wizardPoints["W-1"] = ["#A-2", "#B-1"]; wizardPoints["W-2"] = ["#J-2", "#I-1"]; wizardPoints["W-3"] = ["#J-9", "#I-10"]; wizardPoints["W-4"] = ["#A-9", "#B-10"]; checkWizardPoints(wizardPoints,gridLocation, TypeOfCheck,chessboard); } } function CheckKing(gridLocation, TypeOfCheck,chessboard){ var coords = [[-1,-1],[-1,0],[-1,+1],[0,+1],[+1,+1],[+1,0],[+1,-1], [0,-1]]; checkJumps(gridLocation, TypeOfCheck,chessboard,coords); // if omega add rules if(chessboard.options.gametype=="omega"){ var wizardPoints = []; wizardPoints["W-1"] = ["#A-1"]; wizardPoints["W-2"] = ["#J-1"]; wizardPoints["W-3"] = ["#J-10"]; wizardPoints["W-4"] = ["#A-10"]; checkWizardPoints(wizardPoints,gridLocation, TypeOfCheck,chessboard); } } function CheckWizard(gridLocation, TypeOfCheck,chessboard){ var coords = [[-3,-1],[-3,+1],[+3,-1],[+3,+1],[-1,-3],[-1,+3], [+1,-3],[+1,+3],[+1,+1],[-1,+1],[+1,-1],[-1,-1]]; checkJumps(gridLocation, TypeOfCheck,chessboard,coords); var wizardPoints = []; wizardPoints["W-1"] = ["#A-3", "#A-1", "#C-1"]; wizardPoints["W-2"] = ["#J-3", "#J-1", "#H-1"]; wizardPoints["W-3"] = ["#J-8", "#J-10", "#H-10"]; wizardPoints["W-4"] = ["#A-8", "#A-10", "#C-10"]; checkWizardPoints(wizardPoints,gridLocation, TypeOfCheck,chessboard); } function CheckRook(gridLocation,TypeOfCheck,chessboard){ var CList= ["LEFT","RIGHT","UP","DOWN"]; var checkPathList = generateCoordList (gridLocation,CList,chessboard); checkPaths(gridLocation,TypeOfCheck,chessboard,checkPathList,CList) } function CheckPawn(gridLocation, TypeOfCheck, chessboard){ // if starting then move forward one or two squares if(TypeOfCheck=="movement"){ if(chessboard.options.gametype=="grand"){ var startWhite = "3" var startBlack = chessboard.layout[chessboard.options.gametype].gridSize-2 } else { var startWhite = "2" var startBlack = chessboard.layout[chessboard.options.gametype].gridSize-1 } if(gridLocation[1]==startWhite || gridLocation[1]==startBlack){ // can we actually move forward one square if($("#"+gridLocation[0]+"-"+((gridLocation[1]*1)+ (chessboard.options.pawnDirection * -1))).attr("piece")==null){ chessboard.options.highlight[chessboard.options.highlight.length] = "#"+gridLocation[0]+"-"+((gridLocation[1]*1)+ (chessboard.options.pawnDirection * -1)); // can we actually move forward two squares if($("#"+gridLocation[0]+"-"+((gridLocation[1]*1)+ (chessboard.options.pawnDirection * -2))).attr("piece")==null){ chessboard.options.highlight[chessboard.options.highlight.length] = "#"+gridLocation[0]+"-"+((gridLocation[1]*1)+ (chessboard.options.pawnDirection * -2)); } if(chessboard.options.gametype=="omega"){ if($("#"+gridLocation[0]+"-"+((gridLocation[1]*1)+ (chessboard.options.pawnDirection * -3))).attr("piece")==null){ chessboard.options.highlight[chessboard.options.highlight.length] = "#"+gridLocation[0]+"-"+((gridLocation[1]*1)+ (chessboard.options.pawnDirection * -3)); } } } }else { // can we actually move forward one square if($("#"+gridLocation[0]+"-"+((gridLocation[1]*1)+ (chessboard.options.pawnDirection * -1))).attr("piece")==null){ chessboard.options.highlight[chessboard.options.highlight.length] = "#"+gridLocation[0]+"-"+((gridLocation[1]*1)+ (chessboard.options.pawnDirection * -1)); } } } // can we attack diagonally if(chessboard.options.letters[gridLocation[0]]-1>=0){ var leftkey = "#"+chessboard.options.revletters[chessboard.options.letters[gridLocation[0]]-1]+"-"+ ((gridLocation[1]*1)+(chessboard.options.pawnDirection*-1)); var leftPiece = $(leftkey).attr("piece"); if (leftPiece){ setMarker(leftkey,leftPiece,TypeOfCheck, chessboard); } } if(chessboard.options.letters[gridLocation[0]] +1<chessboard.layout[chessboard.options.gametype].gridSize){ var rightKey = "#"+chessboard.options.revletters[chessboard.options.letters[gridLocation[0]] +1]+"-"+((gridLocation[1]*1)+(chessboard.options.pawnDirection*-1)); var rightPiece = $(rightKey).attr("piece"); if (rightPiece){ setMarker(rightKey,rightPiece,TypeOfCheck, chessboard); } } } function CheckCardinal(gridLocation,TypeOfCheck,chessboard){ var CList= ["MM","MP","PM","PP"]; var checkPathList = generateCoordList (gridLocation,CList,chessboard); checkPaths(gridLocation,TypeOfCheck,chessboard,checkPathList,CList); var coords = [[-2,-1],[-2,+1],[+2,-1],[+2,+1],[-1,-2],[-1,+2], [+1,-2],[+1,+2]]; checkJumps(gridLocation, TypeOfCheck,chessboard,coords); } function CheckMarshall(gridLocation,TypeOfCheck,chessboard){ var CList= ["LEFT","RIGHT","UP","DOWN"]; var checkPathList = generateCoordList (gridLocation,CList,chessboard); checkPaths(gridLocation,TypeOfCheck,chessboard,checkPathList,CList); var coords = [[-2,-1],[-2,+1],[+2,-1],[+2,+1],[-1,-2],[-1,+2], [+1,-2],[+1,+2]]; checkJumps(gridLocation, TypeOfCheck,chessboard,coords); } // check that the king is not in check function checkMyKing(colour, chessboard){ gridLocation = chessboard.options.KingCoords[colour].split("-"); var incheck = []; var coords = [ [-2,-1, ",knight",""], [-2,+1, ",knight",""], [+2,-1, ",knight",""], [+2,+1, ",knight",""], [-1,-2, ",knight",""], [-1,+2, ",knight",""], [+1,-2, ",knight",""], [+1,+2, ",knight",""], [-1,-1, ",pawn,king",""], [-1,0, ",king",""], [-1,+1, ",pawn,king",""], [0,+1, ",king",""], [+1,+1, ",pawn,king",""], [+1,0, ",king",""], [+1,-1, ",pawn,king",""], [0,-1, ",king",""] ]; for(i=1;i<chessboard.layout[chessboard.options.gametype].gridSize;i+ +){ coords[coords.length] = [-i,0, ",rook,queen","LEFT"]; coords[coords.length] = [i,0, ",rook,queen","RIGHT"]; coords[coords.length] = [0,-i, ",rook,queen","DOWN"]; coords[coords.length] = [0,i, ",rook,queen","UP"]; coords[coords.length] = [-i,-i, ",bishop,queen","LEFT-DOWN"]; coords[coords.length] = [-i,i, ",bishop,queen","LEFT-UP"]; coords[coords.length] = [i,-i, ",bishop,queen","RIGHT-DOWN"]; coords[coords.length] = [i,i, ",bishop,queen","RIGHT-UP"]; } var endlist = []; endlist[""]=0; endlist["LEFT"] = 0; endlist["RIGHT"] = 0; endlist["DOWN"] = 0; endlist["UP"] = 0; endlist["LEFT-UP"] = 0; endlist["LEFT-DOWN"] = 0; endlist["RIGHT-UP"] = 0; endlist["RIGHT-DOWN"] = 0; for(var c =0;c<coords.length;c++){ var x = chessboard.options.letters[gridLocation[0]]+coords[c][0]; var y = (gridLocation[1]*1)+coords[c][1]; var checkFor = coords[c][2]; var endCode = coords[c][3]; if(x>=0 && x<=chessboard.layout[chessboard.options.gametype].gridSize && y>=0 && y<=chessboard.layout[chessboard.options.gametype].gridSize){ var coordinate = "#"+chessboard.options.revletters[x]+"-"+(y) // if were are movign a piece temporally move it to this loacation var checkPiece = $(coordinate).attr("tmppiece"); if(!checkPiece){ var checkPiece = $(coordinate).attr("hiddenpiece"); if(!checkPiece){ var checkPiece = $(coordinate).attr("piece"); } else { var checkPiece=null; } } if(checkPiece && endlist[endCode]==0){ if(endCode!=""){ endlist[endCode] = 1; } var checkPieces = checkPiece.split("-"); if(checkPieces[0]!=colour){ if (checkFor.indexOf(checkPieces[1])>-1){ ok = true if(checkPieces[1]=="pawn"){ if(colour=="black") { var direction = -1 } else { var direction = 1 } if(coords[c][1]!= direction) {ok = false;} } if (ok){ chessboard.options.incheck[chessboard.options.incheck.length] = coordinate; } } } } } } if (chessboard.options.incheck.length>0){ chessboard.options.incheck.length=0; return true; } else { return false; } } function checkWizardPoints(wizardPoints,gridLocation, TypeOfCheck,chessboard){ var locationId = gridLocation.join("-"); if (gridLocation[0]=="W"){ var coordinates = wizardPoints[locationId]; for(i=0;i<coordinates.length;i++){ var checkPiece = $(coordinates[i]).attr("piece"); setMarker(coordinates[i],checkPiece,TypeOfCheck,chessboard); } } else { // check if you are on a point that you can jump to from a wizard point do reverse move for(wpIndex in wizardPoints){ var coordinates = wizardPoints[wpIndex]; for(i=0;i<coordinates.length;i++){ if(coordinates[i]=="#"+locationId){ var checkPiece = $("#"+wpIndex).attr("piece"); setMarker("#"+wpIndex,checkPiece,TypeOfCheck,chessboard); } } } } } function checkWizardPointsPath(wizardPoints,gridLocation, TypeOfCheck,chessboard){ var locationId = gridLocation.join("-"); if (gridLocation[0]=="W"){ var coordinates = wizardPoints[locationId]; for(i=0;i<coordinates.length;i++){ var checkPiece = $(coordinates[i]).attr("piece"); setMarker(coordinates[i],checkPiece,TypeOfCheck,chessboard); } } else { // check if you are on a point that you can jump to from a wizard point do reverse move for(wpIndex in wizardPoints){ var coordinates = wizardPoints[wpIndex]; for(i=0;i<coordinates.length;i++){ if(coordinates[i]=="#"+locationId){ var checkPiece = $("#"+wpIndex).attr("piece"); setMarker("#"+wpIndex,checkPiece,TypeOfCheck,chessboard); } } } } } function setMarker(coordinate,checkPiece,TypeOfCheck,chessboard){ if(checkPiece){ var checkPieces = checkPiece.split("-"); if(TypeOfCheck=="movement"){ if(checkPieces[0]!= chessboard.options.whosmove){ chessboard.options.highlight[chessboard.options.highlight.length] = coordinate; } } if(TypeOfCheck=="check"){ if(checkPieces[0]!= chessboard.options.whosmove && checkPieces[1]=="king"){ chessboard.options.check[chessboard.options.check.length] = coordinate; } } } else { if(TypeOfCheck=="movement"){ chessboard.options.highlight[chessboard.options.highlight.length] = coordinate; } } } function generateBoardDescription(chessboard){ var row = []; for(cols = chessboard.layout[chessboard.options.gametype].gridSize-1;cols>=0;cols--) { row[cols] = ""; var blanks = 0; for(rows = 1;rows<=chessboard.layout[chessboard.options.gametype].gridSize;rows++) { var piece = $("#"+chessboard.options.revletters[rows]+"-"+(cols +1)).attr("piece"); if(piece){ if (blanks>0){ row[cols]+=(blanks+""); } var part = piece.split("-"); if(part[1]=="knight"){letter="n"} else {letter =piece.split("-") [1].substr(0,1);} if(part[0]=="white"){letter= letter.toUpperCase();} row[cols]+=letter+""; blanks=0; } else { blanks++; } } if (blanks>0){ row[cols]+=(blanks+""); } } strRow =""; for(rowIndex = chessboard.layout[chessboard.options.gametype].gridSize-1;rowIndex>=0;rowIndex--) { if(rowIndex<chessboard.layout[chessboard.options.gametype].gridSize-1) { strRow += "/"; } strRow += row[rowIndex]; } return strRow; } function startGame(gametype,t){ } function checkForMate(chessboard, colour){ if(colour=="white"){ ProtectColour="black" }else { ProtectColour="white" } var tmpColour = chessboard.options.whosmove; var kingsPosition = chessboard.options.KingCoords[ProtectColour]; // get the list of locations that the king under attack can move to chessboard.options.whosmove = ProtectColour; CheckKing(kingsPosition.split("-"), "movement",chessboard); var highlight = copyArray(chessboard.options.highlight); chessboard.options.highlight.length=0 var checkRoute = []; // get all of the locations that the attacker is covering chessboard.options.whosmove = colour; $("td.square").each(function(i){ var piece = this.getAttribute("piece") if(piece){ var partsOfPiece = piece.split("-"); if (partsOfPiece[0] == colour){ mark(partsOfPiece[1], this.id.split("-"), "movement", chessboard); } } }); var route = copyArray(chessboard.options.highlight); //alert(highlight.join(", ")) //alert(route.join(", ")); // can the king escape himself var escaperoute = true if(highlight.length>0){ for(ih=0;ih<highlight.length;ih++){ escaperoute = true for(ir=0;ir<route.length;ir++){ if(escaperoute && highlight[ih]!=route[ir]){ escaperoute = false; // no escape going along this path } } } } chessboard.options.highlight.length=0; if (!escaperoute){ // check to see if the other pieces can break the check chessboard.options.whosmove = ProtectColour; $("td.square").each(function(i){ var piece = this.getAttribute("piece") if(piece){ var partsOfPiece = piece.split("-"); if (partsOfPiece[0] != colour && partsOfPiece[1]!="king"){ mark(partsOfPiece[1], this.id.split("-"), "movement", chessboard); } } }); var highlight = copyArray(chessboard.options.highlight); var route = copyArray(chessboard.options.checkRoute); escaperoute = false if(highlight.length>0){ for(ih=0;ih<highlight.length;ih++){ for(ir=0;ir<route.length;ir++){ if(highlight[ih]==route[ir]){ escaperoute = true } } } } chessboard.options.highlight.length=0; chessboard.options.whosmove = tmpColour; if(escaperoute){ return false; // king rescued } else { return true; //checkmate } } else { chessboard.options.whosmove = tmpColour; return false; // king can escape } } function mark(typeOfPiece,gridLocation, typeOfCheck, chessboard){ if(typeOfPiece=="knight"){ CheckKnight(gridLocation, typeOfCheck,chessboard); } if(typeOfPiece=="rook"){ CheckRook(gridLocation, typeOfCheck,chessboard); } if(typeOfPiece=="bishop"){ CheckBishop(gridLocation, typeOfCheck,chessboard); } if(typeOfPiece=="cardinal"){ CheckCardinal(gridLocation, typeOfCheck,chessboard); } if(typeOfPiece=="marshall"){ CheckMarshall(gridLocation, typeOfCheck,chessboard); } if(typeOfPiece=="queen"){ CheckQueen(gridLocation, typeOfCheck,chessboard); } if(typeOfPiece=="pawn"){ CheckPawn(gridLocation, typeOfCheck,chessboard); } if (typeOfPiece=="champion"){ CheckChampion(gridLocation, typeOfCheck,chessboard); } if (typeOfPiece=="wizard"){ CheckWizard(gridLocation, typeOfCheck,chessboard); } if (typeOfPiece=="king"){ CheckKing(gridLocation, typeOfCheck,chessboard); } return true; } // this function is because I was having problems resetting the list function copyArray(list){ returnData = []; for(index in list){ returnData[index] = list[index]; } return returnData; } })($)