// Copyright 2006 Jim Radford <jim@greenfelt.net> and David Caldwell <david@greenfelt.net> -*- c -*-
function poker_hand(cards){this.cards=cards;this.ofakind=this.ofa("rank");this.ofasuit=this.ofa("suit");}
poker_hand.prototype.ofa=function(type){var bytype=[];for(var i in this.cards){var t=this.cards[i][type];bytype[t]=bytype[t]==undefined?1:bytype[t]+1;}
var ofa=[];for(var t in bytype){var n=bytype[t];if(ofa[n]==undefined)
ofa[n]=[];ofa[n].push(t);}
for(var n in ofa)
if(ofa[n])ofa[n].sort(function(A,B){return parseInt(B)-parseInt(A);});return ofa;};poker_hand.straights={"5,4,3,2,1":1,"6,5,4,3,2":2,"7,6,5,4,3":3,"8,7,6,5,4":4,"9,8,7,6,5":5,"10,9,8,7,6":6,"11,10,9,8,7":7,"12,11,10,9,8":8,"13,12,11,10,9":9,"13,12,11,10,1":10};poker_hand.order={high_card:0,pair:1,two_pair:2,three_of_a_kind:3,straight:4,flush:5,full_house:6,four_of_a_kind:7,straight_flush:8,royal_straight_flush:9};poker_hand.prototype.type=function(){switch(this.ofakind[1]?this.ofakind[1].length:0){case 0:return"full_house";case 1:return this.ofakind[2].length==2?"two_pair":"four_of_a_kind";case 2:return"three_of_a_kind";case 3:return"pair";case 5:var st=poker_hand.straights[this.ofakind[1]],fl=this.ofasuit[5];return!st&&!fl?"high_card":st&&!fl?"straight":!st&&fl?"flush":st==10?"royal_straight_flush":"straight_flush";}};poker_hand.prototype.partial_type=function(){if(this.ofakind[1]&&this.ofakind[1].length==5){var st=poker_hand.straights[this.ofakind[1]],fl=this.ofasuit[5];return!st&&!fl?"high_card":st&&!fl?"straight":!st&&fl?"flush":st==10?"royal_straight_flush":"straight_flush";}
if(this.ofakind[3]&&this.ofakind[3].length==1)
return this.ofakind[2]&&this.ofakind[2].length==1?"full_house":"three_of_a_kind";if(this.ofakind[2])return this.ofakind[2].length==2?"two_pair":"pair";if(this.ofakind[4])return"four_of_a_kind";return"high_card";};function grid(div,game){pile.prototype.init.call(this,div,game);this.can_drag=function(){return false;}
this.accept=function(card){return this.pile.length==0;}
this.xoffset=0;this.yoffset=0;var me=this;this.onclick=function(){if(me.pile.length==0){me.game.push_state();if(me.game.play.pile.length)
me.game.play.move_to_pile(me.game.play.top_index(),1,me);}}
this.push=function(card){var ret=grid.prototype.push.call(this,card);this.game.deck.flip();this.game.compute_score();return ret;};}
grid.prototype=new pile();function game(){pile_game.prototype.init.call(this,"dealwithit");this.grid=new Array(5);for(var r=1;r<=5;r++){this.grid[r]=Array(5);for(var c=1;c<=5;c++)
this.grid[r][c]=new grid("grid"+r+c,this);}
this.discard=[];for(var c=1;c<=2;c++)
this.discard[c]=new grid("discard"+c,this);this.deck=new pile("deck",this);this.play=new pile("play",this);this.create_deck(this.deck,this.play,1);if(!this.timer)game.timer=this.timer=new Stat(doc.id("timer"),"timer",this.stats);if(!this.score)game.score=this.score=new Stat(doc.id("score"),"score",this.stats);this.score.set(0);this.deck.yoffset=0;var me=this,flip=this.deck.onclick;delete this.deck.onclick;this.deck.flip=function(){if(this.game.timeout)
this.game.timeout=cancel_timeout(this.game.timeout);if(this.game.is_complete())return;flip(this);this.game.undo.pop();var countdown=function(delay,f){me.timer.set(delay);if(delay==0)f();else me.timeout=timeout(1000,function(){countdown(delay-1,f)});};return countdown(10,function(){me.random_play()});}
this.deal=function(deck){if(this.overlay)this.overlay.unoverlay();this.overlay=overlay_card(deck[deck.length-27+Math.floor(rand()%27)],"star");this.deck.pile=deck;this.deck.flip();};}
game.prototype=new pile_game();game.prototype.won_message="Game over!";game.prototype.random_play=function(){var left=[];if(this.discard[1].pile.left==0)left.push(this.discard[1]);if(this.discard[2].pile.left==0)left.push(this.discard[2]);for(var r in this.grid)
for(var c in this.grid[r])
if(this.grid[r][c].pile.length==0)
left.push(this.grid[r][c]);left[Math.floor(rand()%left.length)].onclick();}
game.prototype.hands=function(){var hands=[];hands["diag"+1]=[];hands["diag"+2]=[];for(var i=1;i<=5;i++)
{hands["row"+i]=[];hands["col"+i]=[];}
for(var i=1;i<=5;i++)
for(var j=1;j<=5;j++)
if(this.grid[i][j].pile.length){hands["row"+i].push(this.grid[i][j].pile[0]);hands["col"+j].push(this.grid[i][j].pile[0]);if(i==j)hands["diag"+1].push(this.grid[i][j].pile[0]);if(i==6-j)hands["diag"+2].push(this.grid[i][j].pile[0]);}
return hands;};game.prototype.is_complete=function(){for(var r in this.grid)for(var c in this.grid[r])if(this.grid[r][c].pile.length==0)return NOT_COMPLETE;return GAME_OVER;};game.score_table={high_card:0,pair:10,two_pair:30,three_of_a_kind:60,straight:120,flush:50,full_house:100,four_of_a_kind:160,straight_flush:300,royal_straight_flush:450};game.prototype.compute_score=function(){var hands=this.hands();var score=0;for(var h in hands){if(hands[h].length>0){var scale=1;for(var i in hands[h])
if(hands[h][i]==this.overlay)
scale=2;var hand=new poker_hand(hands[h]);score+=scale*game.score_table[hand.partial_type()];}}
this.score.set(score);}
function overlay_card(orig,string){orig.draw=function(div,coords,z){var img=card.prototype.draw.call(this,div,coords,z);if(this.face==up)
this.overlay.draw(div,coords,z);else
this.overlay.erase();return img;}
orig.erase=function(){this.overlay.erase();card.prototype.erase.call(this);}
orig.unoverlay=function(){this.overlay.erase();delete this.draw;delete this.erase;delete this.overlay;delete this.unoverlay;}
orig.overlay=new card(string,undefined,undefined,up,null);return orig;};
