// Copyright 2005-2006 David Caldwell <david@greenfelt.net> All rights reserved. -*- c -*- 
add_option("decks","number",1,"Number of decks to use");function discard(div,game){pile.prototype.init.call(this,div,game);this.accept_stacks=false;this.accept=function(card){return card.rank==king&&2;}
this.xoffset=this.yoffset=0;this.can_drag=function(index){return false;}}
discard.prototype=new pile();function waste(div,game){pile.prototype.init.call(this,div,game);this.accept_stacks=false;this.accept=function(card){return card.came_from_stock;}
this.xoffset=this.yoffset=0;this.can_drag=function(index){return index==this.top_index();}
this.split_move=function(start,n,from){if(this.pile.length>=1&&this.pile[this.pile.length-1].rank+from.pile[start].rank==13)
return[{from:this,to:this.game.discard,fstart:this.pile.length-1,n:1},{from:from,to:this.game.discard,fstart:start,n:1}];else
return[{from:from,to:this,fstart:start,n:1}];};}
waste.prototype=new pile();function tableau(div,game){pile.prototype.init.call(this,div,game,false);this.uncovered=function(){return(!this.covered_by[0]||!this.covered_by[0].pile.length)&&(!this.covered_by[1]||!this.covered_by[1].pile.length);};this.accept=function(card){return this.uncovered()&&this.pile.length&&this.top().rank+card.rank==13?2:0;};this.can_drag=function(index){this.top().came_from_stock=false;return this.uncovered()&&index==this.top_index();};this.xoffset=this.yoffset=0;this.maxstack=1;this.split_move=function(start,n,from){return[{from:this,to:this.game.discard,fstart:0,n:1},{from:from,to:this.game.discard,fstart:start,n:1}];};}
tableau.prototype=new pile();function stock(div,game){pile.prototype.init.call(this,div,game);this.accept=function(card){return false;};this.can_drag=function(index){this.top().came_from_stock=true;return index==this.top_index();};this.splice=this.splice_and_turn_over;this.xoffset=1;this.yoffset=1;this.maxstack=52-35-1;}
stock.prototype=new pile();function game(){pile_game.prototype.init.call(this,this.game);this.decks=options.decks;this.stock=new stock("stock",this);this.waste=new waste("waste",this);this.discard=new discard("discard",this);this.tableau=[];var thash={};for(var r=7-1;r>=0;r--)
for(var i=0;i<=r;i++){var p=new tableau("tableau"+r+i,this);thash[""+r+i]=p;p.covered_by=[thash[sprintf("%d%d",(r+1),i)],thash[sprintf("%d%d",r+1,i+1)]];this.tableau.push(p);}
this.deal=function(deck){this.stock.pile=deck;for(var c in this.stock.pile)
this.stock.pile[c].face=down;for(var c in this.tableau){this.tableau[c].push(this.stock.pop());this.tableau[c].top().face=up;}};this.update=function(force){pile_game.prototype.update.call(this,force);if(force)
resize_tableau(cardset.width,cardset.height);};}
game.prototype=new pile_game();game.prototype.game="pyramid";game.prototype.is_complete=function(){for(var i in this.tableau)
if(this.tableau[i].pile.length){for(var j in this.tableau)
if(this.tableau[j].pile.length&&this.tableau[j].uncovered()&&this.tableau[i].accept(this.tableau[j].top()))
return NOT_COMPLETE;if(this.tableau[i].uncovered()&&(this.waste.pile.length&&this.tableau[i].accept(this.waste.top())||this.stock.pile.length&&this.tableau[i].accept(this.stock.top())||this.tableau[i].top().rank==king))
return NOT_COMPLETE;}
for(var i in this.tableau)
if(this.tableau[i].pile.length)
return this.stock.pile.length==0&&GAME_OVER;return WON;}
