-
Notifications
You must be signed in to change notification settings - Fork 4
/
backgammon.min.js
42 lines (42 loc) · 24.5 KB
/
backgammon.min.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
;var backgammon=(function(context){"use strict";var main=context.backgammon={};main.sequence=function(type){var id=0;return function(){return type+(id++);}};main.rand1=function(max){return Math.floor(Math.random()*max);};main.rand2=function(min,max){return Math.floor(Math.random()*(max-min+1))+min;};main.defaults={orientation:true,delay:505,position:'2W////5B/3B///5W5B///3W/5W////2B W 00 0 00'};return main;})(this);;(function(backgammon){var rand1=backgammon.rand1;var rand2=backgammon.rand2;backgammon.game=(function(){"use strict";var moveid=backgammon.sequence("move");var Move=function(die,source,target,hit){this.id=moveid();this.die=die;this.source=source;this.target=target;this.hit=hit;this.used=0;this.candidate=-999;};var gameid=backgammon.sequence("game");var Game=function(config){var self=this;self.id=gameid();self.wtm=true;self.rolls=[];self.moves=[];self.okmoves=[];self.movenum=0;self.pieces={white:new Array(24),black:new Array(24),offwhite:0,offblack:0,barwhite:0,barblack:0};self.toroll=null;self.board=config.ui||null;self.white=config.white||null;self.black=config.black||null;self.delay=config.delay||null;self.setup(config.position);if(self.white){self.white.init(self);}
if(self.black){self.black.init(self);}};Game.prototype.setup=function(positionstring){var self=this;var posarray=(positionstring||backgammon.defaults.position).toUpperCase().split(' ');var position=self.position=posarray[0];var c,n,p=23,i=0,l=position.length;var D='/'.charCodeAt(0),W='W'.charCodeAt(0),B='B'.charCodeAt(0);self.pieces.offwhite=0;self.pieces.offblack=0;self.pieces.barwhite=0;self.pieces.barblack=0;while(i<l&&p>-1){c=position.charCodeAt(i);n=position.charCodeAt(i+1);self.pieces.white[p]=0;self.pieces.black[p]=0;if(c===D){p--;}else if(n&&(c>=49||c<=57)){self.pieces.white[p]=n===W?c-48:0;self.pieces.black[p]=n===B?c-48:0;i++;p--;}
i++;}
self.wtm=(posarray[1]||'W')==='W';positionstring=posarray[2]||'00';self.pieces.barwhite=parseInt(positionstring.charAt(0),16);self.pieces.barblack=parseInt(positionstring.charAt(1),16);self.fullmove=parseInt(posarray[3]||'0');positionstring=posarray[4]||'00';if(positionstring!=='00'){self.toroll={die1:parseInt(positionstring.charAt(0)),die2:parseInt(positionstring.charAt(1))};}};Game.prototype.start=function(){var self=this;if(self.toroll){self.roll(self.toroll.die1,self.toroll.die2);self.toroll=null;}else{self.roll();}};Game.prototype.toString=function(){var self=this;var pos="";var whites=self.pieces.white;var blacks=self.pieces.black;var wtot=self.pieces.barwhite,btot=self.pieces.barblack;for(var point=23;point>=0;point--){if(whites[point])pos+=whites[point]+'W';else if(blacks[point])pos+=blacks[point]+'B';else pos+='/';wtot+=whites[point];btot+=blacks[point];}
pos+=' ';pos+=self.wtm?'W':'B';pos+=' ';pos+=self.pieces.barwhite;pos+=self.pieces.barblack;pos+=' ';pos+=self.fullmove;pos+=' ';pos+=self.rolls[self.movenum]?self.rolls[self.movenum].die1:'0';pos+=self.rolls[self.movenum]?self.rolls[self.movenum].die2:'0';return pos;};Game.prototype.point=function(p){var self=this;switch(p){case-1:return self.pieces.barblack;case 24:return self.pieces.barwhite;case 999:return self.canclear()!==false?1:0;default:return self.wtm?self.pieces.white[p]:self.pieces.black[p];}};Game.prototype.pointcolor=function(p){var self=this;switch(p){case-1:return self.wtm?false:true;case 24:return self.wtm?true:false;case 999:return self.canclear()!==false;default:return(self.pieces.white[p]&&self.wtm)||(self.pieces.black[p]&&!self.wtm);}};Game.prototype.nextmove=function(){var self=this;if(self.delay){setTimeout(function(){if(!self.wtm)self.fullmove++;self.wtm=!self.wtm;self.movenum++;self.roll();},self.delay);}else{if(!self.wtm)self.fullmove++;self.wtm=!self.wtm;self.movenum++;self.roll();}};Game.prototype.canmove=function(){var self=this;var okmoves=self.okmoves[self.movenum];var okmove=null;for(var i=0,l=okmoves.length;i<l;i++){okmove=okmoves[i];if(self.okmove(okmove.source,okmove.target)&&!okmove.moved)return true;}
return false;};Game.prototype.haswon=function(){var self=this;if(self.wtm&&self.pieces.barwhite>0)return false;if(!self.wtm&&self.pieces.barblack>0)return false;var pieces=self.wtm?self.pieces.white:self.pieces.black;for(var i=0,l=pieces.length;i<l;i++){if(pieces[i]>0)return false;}
return true;};Game.prototype.move=function(okmove){var self=this;var player=self.wtm?'white':'black';var waiter=self.wtm?'black':'white';okmove.color=player;okmove.roll=self.rolls[self.movenum];okmove.notation=' '+self.movenum+'.'+okmove.source+'/'+okmove.target;switch(okmove.source){case-1:self.pieces.barblack--;break;case 24:self.pieces.barwhite--;break;default:self.pieces[player][okmove.source]--;};self.pieces[player][okmove.target]++;if(okmove.hit){self.pieces[waiter][okmove.target]--;self.pieces['bar'+waiter]++;okmove.notation+='*';}
if(okmove.roll.die1===okmove.die)okmove.roll.used1++;else if(okmove.roll.die2===okmove.die)okmove.roll.used2++;self.moves.push(okmove);self.okmoves[self.movenum].moved=(self.okmoves[self.movenum].moved||0)+1;if(self.board)self.board.update(okmove);if(self.haswon()){console.log('WINNER!',self.id,player);self.winner=player;return null;}else if(self.okmoves[self.movenum].moved>(self.rolls[self.movenum].doubles?3:1)){self.nextmove();}else if(!self.canmove()){self.nextmove();}
return okmove;};Game.prototype.roll=function(die1,die2){var self=this;var roll=self.rolls[self.movenum];if(!roll){die1=parseInt(die1);die2=parseInt(die2);var val1=(!isNaN(die1)&&die1>=1&&die1<=6)?die1:rand2(1,6);var val2=(!isNaN(die2)&&die2>=1&&die2<=6)?die2:rand2(1,6);var roll={die1:val1,die2:val2,used1:0,used2:0,doubles:val1===val2,color:self.wtm?'white':'black'}
self.rolls[self.movenum]=roll;self.generatemoves();if(self.okmoves[self.movenum].length===0){self.nextmove();}}
if(self.board)self.board.onroll(roll);if(self.white)self.white.onroll(roll);if(self.black)self.black.onroll(roll);return roll;};Game.prototype.onbar=function(){var self=this;return((self.wtm&&self.pieces.barwhite>0)||(!self.wtm&&self.pieces.barblack>0));};Game.prototype.canclear=function(){var self=this;if(self.onbar())return false;var begin=self.wtm?6:0;var end=self.wtm?24:18;var pieces=self.wtm?self.pieces.white:self.pieces.black;for(var point=begin;point<end;point++)if(pieces[point]>0)return false;if(self.wtm){for(var point=5;point>=0;point--){if(pieces[point]>0)return point;}}else{for(var point=19;point<=23;point++){if(pieces[point]>0)return point;}}
return self.wtm?0:23;};Game.prototype.generatemoves=function(){var self=this;var moves=[];var roll=self.rolls[self.movenum];var die1=roll.die1;var die2=roll.die2;var die0;var player=self.wtm?self.pieces.white:self.pieces.black;var waiter=self.wtm?self.pieces.black:self.pieces.white;var direction=self.wtm?-1:1;var hittarget0,hittarget1,hittarget2,hittarget3,hittarget4;var oktarget0,oktarget1,oktarget2,oktarget3,oktarget4;var target0,target1,target2,target3,target4;var possible,doublesource,barsource,bartarget,bardie;if(self.onbar()){barsource=self.wtm?24:-1;for(var d=1;d<3;d++){bardie=(d===1?die1:die2)
bartarget=self.wtm?Math.abs(bardie-24):bardie-1;if(waiter[bartarget]<2){hittarget0=waiter[bartarget]===1?true:false;possible=new Move(bardie,barsource,bartarget,hittarget0);moves.push(possible);die0=(d===1?die2:die1)
target0=self.wtm?bartarget-die0:bartarget+die0;if(waiter[target0]<2){hittarget0=waiter[target0]===1?true:false;possible=new Move(die0,bartarget,target0,hittarget0);moves.push(possible);}}}
if(!moves.length){self.okmoves[self.movenum]=moves;return moves;}}
var check=function(source,target,die){if((0<=target&&target<=23)&&(waiter[target]<2)){var hit=waiter[target]===1?true:false;var possible=new Move(die,source,target,hit);moves.push(possible);return true;}
return false;}
for(var point=0;point<24;point++){oktarget1=false;oktarget2=false;oktarget3=false;if(player[point]>0){if(!roll.doubles){target1=point+die1*direction;target2=point+die2*direction;target3=point+die1*direction+die2*direction;oktarget1=check(point,target1,die1);oktarget2=check(point,target2,die2);if(oktarget1)oktarget3=check(target1,target3,die2);if(oktarget2)oktarget4=check(target2,target3,die1);}else{doublesource=point;target4=point+die1*direction;for(var d=0;d<4;d++){if(check(doublesource,target4,die1)){doublesource+=die1*direction;target4+=die1*direction;}}}}}
var canclear=self.canclear();if(canclear!==false){var greater=false;var maxdie=Math.max(roll.die1,roll.die2);var mindie=Math.min(roll.die1,roll.die2);if(self.wtm){var diepoint;var pieces=self.pieces.white;for(var point=canclear;point>=0;point--){diepoint=point+1;if(pieces[point]>0){if(!roll.doubles){if(diepoint===maxdie||(!greater&&diepoint<maxdie)){var possible=new Move(maxdie,point,999,false);moves.push(possible);}
if(diepoint===mindie){var possible=new Move(mindie,point,999,false);moves.push(possible);}}else{if(diepoint===roll.die1||(!greater&&diepoint<maxdie)){for(var i=0;i<4;i++){var possible=new Move(roll.die1,point,999,false);moves.push(possible);}}}
greater=true;}}}else{var diepoint;var pieces=self.pieces.black;for(var point=canclear;point<=23;point++){diepoint=24-point;if(pieces[point]>0){if(!roll.doubles){if(diepoint===maxdie||(!greater&&diepoint<maxdie)){var possible=new Move(maxdie,point,999,false);moves.push(possible);}
if(diepoint===mindie){var possible=new Move(mindie,point,999,false);moves.push(possible);}}else{if(diepoint===roll.die1||(!greater&&diepoint<maxdie)){for(var i=0;i<4;i++){var possible=new Move(roll.die1,point,999,false);moves.push(possible);}}}
greater=true;}}}}
self.okmoves[self.movenum]=moves;return moves;};Game.prototype.okmove=function(source,target){var self=this;source=parseInt(source);target=parseInt(target);var canclear=self.canclear()!==false&&target===999;if(isNaN(source)||isNaN(target)||source<-1||source>24||target<0||(target>24&&!canclear))return false;var pieces=self.wtm?self.pieces.white:self.pieces.black;if(self.onbar()){if(self.wtm&&source!==24)return null;if(!self.wtm&&source!==-1)return null;}else if(!pieces[source]){return null;}
var okmove=null;var okmoves=self.okmoves[self.movenum];var roll=self.rolls[self.movenum];var dist=Math.abs(target-source);var clear=self.wtm?source:24-source;var max=roll.doubles?3:1;var dist1=dist===roll.die1;var dist2=dist===roll.die2;var used1=roll.used1<max;var used2=roll.used2<max;var clear1=canclear&&clear<=roll.die1;var clear2=canclear&&clear<=roll.die2;if(((dist1||clear1)&&used1)||((dist2||clear2)&&used2)){for(var i=0,l=okmoves.length;i<l;i++){var move=okmoves[i];if(move.source===source&&move.target===target&&(move.used<max)){if(dist1||clear1){okmove=move;break;}else if(dist2||clear2){okmove=move;break;}}}}
return okmove;};var api=function(config){return new Game(config||{});};return api;})();})(backgammon);;(function(backgammon){var rand1=backgammon.rand1;var rand2=backgammon.rand2;backgammon.brain=(function(){"use strict";var nextid=backgammon.sequence("brain");var Brain=function(config){var self=this;this.id=nextid();self.white=config.color==='white'?true:false;self.level=config.level||0;};Brain.prototype.init=function(game){var self=this;self.game=game;};Brain.prototype.onroll=function(roll){var self=this;var game=self.game;var barpoint=game.wtm?24:-1;var wtm=game.wtm;var domove=function(game,move,delay){setTimeout(function(){game.move(move)},delay||50);};if(game.wtm===self.white){var movenum=0;var movetotal=roll.doubles?4:2;if(!(game.movenum>=0&&game.okmoves[game.movenum]))return false;var okmoves=game.okmoves[game.movenum].slice();var points=self.white?game.pieces.white:game.pieces.black;var possible=[];var clearstart=game.wtm?0:19;var clearend=game.wtm?5:23;var inclear=false;while(movenum<movetotal){var move=null;var okmove=null;var moveid=null;var onbar=game.onbar();var canclear=game.canclear();var compare=function(source){return(self.wtm?(source<=canclear):(source>=canclear))};possible=[];for(var p=0,poss,l=okmoves.length;p<l;p++){if(self.game.okmove(okmoves[p].source,okmoves[p].target)&&!okmoves[p].moved){poss=okmoves[p];poss.candidate=0;inclear=(poss.source>=clearstart&&poss.source<=clearend);if(game.point(poss.source)===1)poss.candidate+=10;if(game.point(poss.hit))poss.candidate+=8;if(game.point(poss.source)===2)poss.candidate-=5;if(game.point(poss.target)===1)poss.candidate+=12;if(game.point(poss.target)===0)poss.candidate-=7;if(inclear&&(game.point(poss.source)===2||game.point(poss.source)===3))poss.candidate-=15;if(poss.target===999)poss.candidate+=50;possible.push(poss);}}
if(!possible.length){return false;}
possible.sort(function(a,b){return a.candidate>b.candidate?-1:1});move=possible[0];self.game.move(self.game.okmove(move.source,move.target));movenum++;}}};var api=function(config){return new Brain(config||{color:'white'});};return api;})();})(backgammon);;(function(){"use strict";$.scalable={stylesheets:[]};var s,t,i,l,sheet,data,dims,rules,rule,selector,style,size,spec,repx=/^(\d+)px$/;for(s=0,t=document.styleSheets.length;s<t;s++){sheet=document.styleSheets[s];if(sheet.ownerNode.attributes.scalable){data={width:parseInt(sheet.ownerNode.attributes.scalable.value),selectors:{}};$.scalable.stylesheets.push(data);rules=sheet.cssRules||sheet.rules;for(i=0,l=rules.length;i<l;i++){rule=rules[i];selector=rule.selectorText;for(style in rule.style){spec=(typeof rule.style[style]==='string')?rule.style[style].match(repx):null;if(spec&&spec.length&&spec.length===2&&(size=parseInt(spec[1]))){data.selectors[selector]=data.selectors[selector]||{};data.selectors[selector][style]=size;}}}}};$.fn.scalable=(function(width){var $element;var $parent=$(this).width(width=width||window.innerWidth);$.each($.scalable.stylesheets,function(index,data){var scale=width/data.width,size;$.each(data.selectors,function(selector,styles){$element=$parent.find(selector).data("scalable",scale);if($element.length)$.each(styles,function(key,val){size=Math.floor((key==="fontSize")?Math.max(val*scale,8):val*scale);$element.css(key,size);});});});return $parent;});$.fn.fadeIn=function(text){var self=$(this);return self.css({"opacity":"0.0"}).show().text(text).animate({"opacity":"1.0"},"fast");};var nobubble=function(e){e&&e.stopPropagation&&e.stopPropagation();e&&e.preventDefault&&e.preventDefault();return false;};$.fn.draggable=(function(){var _zindex=1;var $document=$(document);var mover=function(item,target,offset){return function(e){var top=e.clientY-offset.top+"px";var left=e.clientX-offset.left+"px";item.css({top:top,left:left});target.trigger("onDragMove",item);return nobubble(e);};};var starter=function(target,delegate,ondrop){return function(e){var item=$(this);var pos=item.offset();var top=e.pageY-pos.top;var left=e.pageX-pos.left;item.data("origin",pos).data("drag",true).css({zIndex:_zindex++});$document.on("mousemove",mover(item,target,{top:top,left:left}));$document.on("mouseup",stopper(item,target,ondrop));target.trigger("onDragStart",item);return nobubble(e);};};var stopper=function(item,target,ondrop){return function(e){item.data("drag",false);$document.off("mousemove");$document.off("mouseup");target.trigger("onDrop",item);var okdrag=ondrop(item);if(okdrag==='snapback')item.animate({top:item.data("origin").top,left:item.data("origin").left},'fast');return nobubble(e);};};return function(delegate,ondrop){if(!delegate)return false;var target=$(this);target.on("mousedown",delegate,starter(target,delegate,ondrop));};})();})();(function(backgammon){var rand1=backgammon.rand1;var rand2=backgammon.rand2;backgammon.board=(function(){"use strict";var uiid=backgammon.sequence("backgammon-board-");var Board=function(container,width,config){var self=this;config=config||{};self.id=uiid();self.ui=(typeof config.ui==='object'?config.ui:window.jQuery)?true:false;self.container=$('#'+container);self.points=new Array(24);self.checkers=new Array(30);self.checkerslots=new Array(24);self.dice=new Array(5);self.width=width||self.container.width();self.orientation=config.orientation?false:backgammon.defaults.orientation;config.ui=self;self.game=backgammon.game(config);self.draw();setTimeout(function(){self.game.start()},1010);};Board.prototype.draw=function(){var self=this;if(self.ui){var point=0;var pieces=self.game.pieces;var white='<div class="backgammon-checker backgammon-white"></div>';var black='<div class="backgammon-checker backgammon-black"></div>';var html='<div class="backgammon-board" id="'+self.id+'"><table class="backgammon-table">';var makepoints=function(from,to,dir,vertical){var points='';for(point=from;(dir<0?point>to:point<to);point+=dir){points+='<td class="backgammon-point backgammon-point-'+vertical+' backgammon-point-'+((23-point)%2?'odd':'even')+'" id="backgammon-point-'+(23-point)+'" data-point="'+(23-point)+'">'+'<div id="backgammon-checkers-'+(23-point)+'" class="backgammon-checkers" data-point="'+(23-point)+'"></div></td>';}
return points;};html+='<tr><td class="backgammon-left"> </td>';for(point=11;point>5;point--){html+='<td class="backgammon-notation">'+(24-point)+'</td>';}
html+='<td class="backgammon-bar"> </td>';for(point=5;point>=0;point--){html+='<td class="backgammon-notation">'+(24-point)+'</td>';}
html+='<td class="backgammon-right"> </td></tr>';html+='<tr><td class="backgammon-left"> </td>';html+=makepoints(11,5,-1,'top');html+='<td class="backgammon-bar backgammon-bar-black" data-point="-1"><div class="backgammon-checkers" data-point="-1"></div></td>';html+=makepoints(5,-1,-1,'top');html+='<td class="backgammon-right">';html+='<div class="backgammon-die backgammon-die2 backgammon-black"></div>';html+='<div class="backgammon-die backgammon-die3 backgammon-black"></div>';html+='</td></tr>';html+='<tr><td class="backgammon-left"> </td>';html+=makepoints(12,18,1,'bottom');html+='<td class="backgammon-bar backgammon-bar-white" data-point="24"><div class="backgammon-checkers" data-point="24"></div></td>';html+=makepoints(18,24,1,'bottom');html+='<td class="backgammon-right">';html+='<div class="backgammon-die backgammon-die0 backgammon-white"></div>';html+='<div class="backgammon-die backgammon-die1 backgammon-white"></div>';html+='</td></tr>';html+='<tr><td class="backgammon-left"> </td>';for(point=12;point<18;point++){html+='<td class="backgammon-notation">'+(24-point)+'</td>';}
html+='<td class="backgammon-bar"> </td>';for(point=18;point<24;point++){html+='<td class="backgammon-notation">'+(24-point)+'</td>';}
html+='<td class="backgammon-right"> </td></tr>';html+='</table></div>';self.container.html(html);self.container.find(".backgammon-point").each(function(){var point=$(this);self.points[parseInt(point.attr("data-point"))]=point;});self.container.find(".backgammon-checkers").each(function(){var slot=$(this);self.checkerslots[parseInt(slot.attr("data-point"))]=slot;});self.dice[0]=self.container.find(".backgammon-die0");self.dice[1]=self.container.find(".backgammon-die1");self.dice[2]=self.container.find(".backgammon-die2");self.dice[3]=self.container.find(".backgammon-die3");self.barwhite=self.container.find(".backgammon-bar-white");self.barblack=self.container.find(".backgammon-bar-black");self.barwhiteslot=self.barwhite.find(".backgammon-checkers");self.barblackslot=self.barblack.find(".backgammon-checkers");for(var n=0;n<self.game.pieces.barwhite;n++)self.barwhiteslot.append(white).data('color','white');for(var n=0;n<self.game.pieces.barblack;n++)self.barblackslot.append(black).data('color','black');for(var p=0;p<24;p++){var $point=self.points[p];var $pieces=self.checkerslots[p];for(var n=pieces.white[p]||0;n;n--)$pieces.append(white).data('color','white');for(var n=pieces.black[p]||0;n;n--)$pieces.append(black).data('color','black');}
self.container.scalable(self.width);self.container.find('.backgammon-point').map(function(){self.placechildren($(this).attr("data-point"))});self.placechildren(-1);self.placechildren(24);self.container.draggable(".backgammon-checker",function(piece){var point=self.onpoint(piece);var source;var target;piece.attr("data-dirty","1");if(point){source=parseInt(piece.parent().attr('data-point'));target=parseInt(point.find(".backgammon-checkers").attr('data-point'));}else{source=parseInt(piece.parent().attr('data-point'));target=999;}
var move=self.move(source,target);if(move==='snapback')piece.attr("data-dirty","0");return move;});}};Board.prototype.toString=function(){return this.game.toString();};Board.prototype.placechildren=function(point,nodelay){var self=this;var $point,$pieces;point=parseInt(point);if(point===24){$point=self.barwhite;$pieces=self.barwhiteslot;}else if(point===-1){$point=self.barblack;$pieces=self.barblackslot;}else{$point=self.points[point];$pieces=self.checkerslots[point];}
var offset=$point.offset();var checker=$point.find(".backgammon-checker:first").outerHeight()*(point<12?-1:1);var fromtop=point<12?$point.outerHeight()+checker:0;var checkers=$pieces.find(".backgammon-checker");var n=0,x=0;checkers.each(function(){var pointleft=offset.left;var pointtop=offset.top+fromtop+(n++*checker);var $piece=$(this).attr("backgammon-data-top",pointtop).attr("backgammon-data-left",pointleft)
setTimeout(function(){$piece.animate({top:pointtop+'px',left:pointleft+'px'},'fast');},(!nodelay?n*50:0));});$point.data("dirty",false);};Board.prototype.place=function(piece,target){var self=this;var source=parseInt(piece.parent().attr("data-point"));var sourcepoint=self.points[source];var targetpoint=self.points[target];var targetpieces=self.checkerslots[target];if(isNaN(source))console.log(piece.parent().html());piece.remove();self.placechildren(source,true);if(target!==999){piece.appendTo(targetpieces.data("dirty",true));self.placechildren(target,true);}
self.placechildren(-1,true);self.placechildren(24,true);piece.attr("data-dirty","");};Board.prototype.update=function(mymove){var self=this;if(mymove.hit&&self.game.pieces.barwhite>0){var barred=self.points[mymove.target].find('.backgammon-checker.backgammon-white');barred.remove().appendTo(self.barwhite.find('.backgammon-checkers'));}
if(mymove.hit&&self.game.pieces.barblack>0){var barred=self.points[mymove.target].find('.backgammon-checker.backgammon-black');barred.remove().appendTo(self.barblack.find('.backgammon-checkers'));}
var piece=self.container.find(".backgammon-checker[data-dirty='1']");if(!piece.length){switch(mymove.source){case-1:piece=self.barblackslot.children(":first-child");break;case 24:piece=self.barwhiteslot.children(":first-child");break;default:piece=self.checkerslots[mymove.source].children(":last-child");break;}}
if(piece.length)self.place(piece,mymove.target);};Board.prototype.move=function(source,target){var self=this;var okmove=self.game.okmove(source,target);if(!okmove)return'snapback'
return self.game.move(okmove);};Board.prototype.roll=function(){var self=this;self.game.roll();};Board.prototype.onroll=function(roll){var self=this;if(roll.color==='white'){self.dice[0].fadeIn(roll.die1);self.dice[1].fadeIn(roll.die2);self.dice[2].hide();self.dice[3].hide();}else{self.dice[0].hide();self.dice[1].hide();self.dice[2].fadeIn(roll.die1);self.dice[3].fadeIn(roll.die2);}};Board.prototype.onpoint=function(piece){var self=this;var piecex=parseInt(piece.offset().left+piece.width()/2);var piecey=parseInt(piece.offset().top+piece.height()/2);var $point=null;self.container.find(".backgammon-point").each(function(){var point=$(this);var pointtop=parseInt(point.offset().top);var pointleft=parseInt(point.offset().left);var pointright=parseInt(pointleft+point.outerWidth());var pointbottom=parseInt(pointtop+point.outerHeight());if(piecey<pointbottom&&piecey>pointtop&&piecex<pointright&&piecex>pointleft){$point=point;return false;}});return $point;};var api=function(board,width,config){return new Board(board,width,config||{});};return api;})();})(backgammon);(function(backgammon){"use strict";var getBoardWidth=function(total){var ratio=1.2189;var cols=4;while(total%cols>0)cols--;var rows=total/cols;var x=window.innerWidth;var y=window.innerHeight;while(x/y>ratio)x--;var w=x;var h=w/ratio;while(w*cols>x)w--;return w-20;};backgammon.new0PGame=function(id,total,delay,position){var width=getBoardWidth(total);var whiteai=backgammon.brain({color:'white',level:0});var blackai=backgammon.brain({color:'black',level:1});var boardui=$("<div id='"+id+"' style='float:left;'><div>");if(isNaN(delay))delay=gammon.defaults.delay;var config={delay:delay,white:whiteai,black:blackai};if(position)config.position=position;$("#boards").append(boardui);return backgammon.board(id,width,config);};backgammon.new1PGame=function(delay,position){var id=nextid();var width=parseInt(window.innerWidth-10);var blackai=backgammon.brain({color:'black',level:1});var boardui=$("<div id='"+id+"' style='float:left;'><div>");if(isNaN(delay))delay=gammon.defaults.delay;var config={delay:delay,black:blackai};if(position)config.position=position;$("#boards").append(boardui);return backgammon.board(id,width,config);};backgammon.tournament=function(total,delay,position){total=total||1;var boards={};var nextid=backgammon.sequence("board");for(var i=0,id;i<total;i++){id=nextid();boards[id]=backgammon.new0PGame(id,total,delay,position);};return boards;};})(backgammon);