/*
   copyright (c) 2006, keith drakard
   this program is distributed under the terms of the creative commons
   license at http://creativecommons.org/licenses/by-nc-sa/2.0/
*/

  // hiscore whilst testing: 107+224 :)
  
  // static global variables
  var bonus_limit= 62;
  var key= new Array(17);
    key[0]= "Ones"; key[1]= "Twos"; key[2]= "Threes";
    key[3]= "Fours"; key[4]= "Fives"; key[5]= "Sixes";
    key[6]= "BONUS <SMALL>(if >"+bonus_limit+")</SMALL>";
    key[7]= "<B>TOP TOTAL</B>";
    key[8]= "Three Of A Kind"; key[9]= "Four Of A Kind";
    key[10]="Small Straight"; key[11]="Large Straight";
    key[12]="Full House"; key[13]="Yahtzee"; key[14]="Chance";
    key[15]="<B>BOTTOM TOTAL</B>";
    key[16]="<B>TOTAL</B>";
  var version = (document.all || document.layers) ? 1 : 0
  // get player options or set to default
  var passed= window.location.search.substring(1);
  var maxplayers= getParm(passed,'d')- 0; if (maxplayers==0) maxplayers=1;

  // dynamic globals
  var rolled= new Array(5); // for (i=0; i<5; i++) { rolled[i]= 0; }
  var rolls= 0; var player= 0; var tmp= new Array(7); var goes= 0;
  var score= new Array(maxplayers);
  for (i=0; i<maxplayers; i++) { score[i]= new Array(17);
    for (j=0; j<17; j++) {
      if (j!=7 && j!=15 && j!=16) score[i][j]= -1; else score[i][j]= 0;
    }
  }

  function preload() {
    if (document.images) {
      dice= new makeArray(7);
        dice[0].src= "/wp-content/scripts/images/common/blank.gif";
        dice[1].src= "/wp-content/scripts/images/yahtzee/one.gif";
        dice[2].src= "/wp-content/scripts/images/yahtzee/two.gif";
        dice[3].src= "/wp-content/scripts/images/yahtzee/three.gif";
        dice[4].src= "/wp-content/scripts/images/yahtzee/four.gif";
        dice[5].src= "/wp-content/scripts/images/yahtzee/five.gif";
        dice[6].src= "/wp-content/scripts/images/yahtzee/six.gif";
      number= new makeArray(11);
        number[0].src= "/wp-content/scripts/images/common/0.gif";
        number[1].src= "/wp-content/scripts/images/common/1.gif";
        number[2].src= "/wp-content/scripts/images/common/2.gif";
        number[3].src= "/wp-content/scripts/images/common/3.gif";
        number[4].src= "/wp-content/scripts/images/common/4.gif";
        number[5].src= "/wp-content/scripts/images/common/5.gif";
        number[6].src= "/wp-content/scripts/images/common/6.gif";
        number[7].src= "/wp-content/scripts/images/common/7.gif";
        number[8].src= "/wp-content/scripts/images/common/8.gif";
        number[9].src= "/wp-content/scripts/images/common/9.gif";
        number[10].src= "/wp-content/scripts/images/common/blank.gif";

    } else {
      alert("Sorry, this game needs to run on a browser\nwhich supports the image object.");
    }    
  }

  function newgame() {
    var index= document.user.noofplayers.selectedIndex;
    var tmp_p= document.user.noofplayers.options[index].value;

    if (tmp_p!= maxplayers) {
      // options have changed so we need to redraw the page
      window.location.href= "?d="+tmp_p;

    } else {
      // same options so just reset the board
      goes= 0;
      for (k=0; k<17; k++) {
        if (k==7 || k==15 || k==16) { first="100";second="10"; } else { first="10";second="1"; }
        for (p=0; p<maxplayers; p++) {
          document.images["p"+p+"k"+k+"d"+first].src= number[10].src;
          document.images["p"+p+"k"+k+"d"+second].src= number[10].src;
          if (k==7 || k==15 || k==16) document.images["p"+p+"k"+k+"d1"].src= number[0].src;
          if (k!=7 && k!=15 && k!=16) score[p][k]= -1; else score[p][k]= 0;
        }
      }
      rolls= 0; document.user.r.value= " 3 Rolls Left ";
      for (i=0; i<5; i++) { select(i); rolled[i]= 0; }
      document.images["play"+player].src= "/wp-content/scripts/images/common/blank.gif";
      player= 0; document.images["play"+player].src= "/wp-content/scripts/images/common/notblank.gif";
    }
  }


  // scoring functions

  function numberof() {
    for (x=0; x<7; x++) { tmp[x]=0; }          // reset the count of each number
    for (x=0; x<5; x++) { tmp[rolled[x]]++; }  // and add 'em up again..
  }
  function sum(what) {
    // adds up the numbers matching 'what', or alternatively, all numbers
    temp= 0; for (x=0; x<5; x++) { if (rolled[x]== what || what==-1) temp+= rolled[x]; }
    return(temp);
  }
  function match(what) {
    for (x=1; x<7; x++) {
      if (tmp[x] >= what) { return(sum(-1)); } // do we have enough?
    } return(0);
  }
  function house() {
    temp= 0; for (x=1; x<7; x++) { if (tmp[x]==2 || tmp[x]==3 || tmp[x]==5) temp+=tmp[x]; }
    if (temp!= 5) temp=0;
    return(temp);
  }
  function line(what) {
    temp= 1;
    if (tmp[3]==0 || tmp[4]==0) {
      temp= 0; // can't have a line without them
    } else {
      if (what== 3) {
        if ((tmp[1]>0 && tmp[2]>0) || (tmp[2]>0 && tmp[5]>0) || (tmp[5]>0 && tmp[6]>0)) temp=temp; else temp=0;
      } else {
        if (tmp[2]==0 || tmp[5]==0) temp=0;
        if (tmp[1]==1 || tmp[6]==1) temp=temp; else temp=0;
      }
    }
    return(temp);
  }

  function get_score(y) {
    numberof();

    if (y<  6) return(tmp[y+1]*(y+1)); // or use this: if (y<  6) return(sum(y+1));
    if (y== 8) return(match(3));
    if (y== 9) return(match(4));
    if (y==10 && line(3)) return(30);
    if (y==11 && line(4)) return(40);
    if (y==12 && house()) return(25);
    if (y==13 && match(5)) return(50);
    if (y==14) return(sum(-1));

    return(0);
  }


  // board updating functions

  function pick(row) {
    if (goes< (maxplayers* 13)) {
      if (score[player][row]== -1) {
        val= get_score(row);
        if (val>0 || confirm("Are you sure you want to zero out this box?")) {
          write(player,row,val); update_score(player,row,val);

          // and the next player...
          document.images["play"+player].src= "/wp-content/scripts/images/common/blank.gif";
          player++; if (player== maxplayers) player= 0;
          document.images["play"+player].src= "/wp-content/scripts/images/common/notblank.gif";
          rolls= 0; var flag= " "+(3-rolls)+" Roll"; if (3-rolls!=1) flag+= "s";
          flag+= " Left "; document.user.r.value= flag;
          for (i=0; i<5; i++) { if (document.images["die"+i].src!= dice[0].src) { select(i); } rolled[i]= 0; }
          goes++;
        }
      } else alert("You can't fill in a box twice.");
    }
  }

  function write(x,y,n) {
    var ns= n+""; var len= ns.length;
    document.images["p"+x+"k"+y+"d1"].src= number[ns.charAt(len-1)].src;
    if (len>=2) document.images["p"+x+"k"+y+"d10"].src= number[ns.charAt(len-2)].src;
    if (len==3) document.images["p"+x+"k"+y+"d100"].src= number[ns.charAt(len-3)].src;
  }

  function update_score(x,y,n) {
    score[x][y]= n; y= (y< 6) ? 7 : 15   // top or bottom total
    n+= score[x][y];
    if (y==7 && score[x][6]!=35) {
      if (n>bonus_limit) { n+= 35; score[x][6]= 35; write(x,6,35); }
      else { score[x][6]++; if (score[x][6]==5) write(x,6,0); }
    }
    score[x][y]= n; write(x,y,n);

    // total total...
    y=16; n= score[x][7]+ score[x][15]; write(x,y,n);
  }


  // dice functions

  function roll(rescue) {
    var flag= 0;
    if (goes< (maxplayers* 13)) {
      if ((!rescue && rolls<3) || (rescue && rolls>0)) {
        for (x=0; x<5; x++) {
          if (!rescue && document.images["die"+x].src==dice[0].src) { rolled[x]=rand(6); flag=1; }
          document.images["die"+x].src= dice[rolled[x]].src;
        }
      }
      if (flag) {
        rolls++; flag= " "+(3-rolls)+" Roll"; if (3-rolls!=1) flag+= "s";
        flag+= " Left "; document.user.r.value= flag;
      }
    }
  }

  function select(x) {
    if (rolls<3) {
      if (document.images["die"+x].src== dice[0].src)
        document.images["die"+x].src= dice[rolled[x]].src;
      else
        document.images["die"+x].src= dice[0].src;
    }
  }

  // The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu)
  // See:  http://www.msc.cornell.edu/~houle/javascript/randomizer.html
  // NOTE:- modified by Kif to produce integers between 1-limit
  rnd.today=new Date(); rnd.seed=rnd.today.getTime();
  function rnd() {
    rnd.seed = (rnd.seed*9301+49297) % 233280;
    return rnd.seed/(233280.0);
  }
  function rand(limit) { return Math.ceil(rnd()*limit); }

  // The following functions were written by Martin Webb at http://www.irt.org/
  function makeArray(n) {
    this.length= n; for (i=0; i<n; i++) { this[i] = new Image(); }
    return this;
  }
  function getParm(string,parm) {
    var startPos= string.indexOf(parm+"=");
    if (startPos> -1) {
      startPos= startPos+ 2;
      var endPos= string.indexOf("&",startPos);
      if (endPos== -1) endPos= string.length;
      return unescape(string.substring(startPos,endPos));
    }
    return '';
  }


function initialise() {

  var output= '';

  output+= '<table align="center" cellspacing=8 cellpadding=0 border=0><tr>\n';

  // create the board
  output+= '<td><table cellspacing=0 cellpadding=4 border=0>\n';

  // do the header
  output+= '<tr align="center"><td>&nbsp;</td>';
  for (x=0; x<maxplayers; x++) { output+= '<td bgcolor="';
    if (x%2==1) output+= '#00001b'; else output+= '#001b00';
    output+= '">&nbsp;<b>Player '+(x+1)+'</b>&nbsp;<br>';
    output+= '<img src="/wp-content/scripts/images/common/'; if (x==0) output+= 'not';
    output+= 'blank.gif" width=50 height=3 alt="" name="play'+x+'"></td>';
  } output+= '</tr>\n';

  // for each row
  for (k=0; k<17; k++) {

    // set up the digits (100s,10s,1s) for the board
    if (k==7 || k==15 || k==16) { first="100";second="10"; } else { first="10";second="1"; }

    // do the "key"
    output+= '<tr><td bgcolor="';
    if (k%2==1) output+= '#1b0000">'; else output+= '#220000">';
    if (k!=6 && k!=7 && k!=15 && k!=16) output+= '<a href="javascript:pick('+k+');" onFocus="blur();">'+key[k]+'</a>&nbsp;';
      else output+= key[k];
    output+= '</td>';

    // for each player
    for (p=0; p<maxplayers; p++) {
      output+= '<td bgcolor="';

      // alternate colours for visibility...
      if (k%2==1) { if (p%2==1) output+= '#00001b'; else output+= '#001b00'; }
      else { if (p%2==1) output+= '#000022'; else output+= '#002200'; }

      // do the digits (p1k12d10 etc)
      output+= '" align="right"><img src="/wp-content/scripts/images/common/blank.gif" width=14 height=18 alt="" name="p'+p+'k'+k+'d'+first+'">';
      output+= '<img src="/wp-content/scripts/images/common/blank.gif" width=14 height=18 alt="" name="p'+p+'k'+k+'d'+second+'">';
      if (k==7 || k==15 || k==16) output+= '<img src="/wp-content/scripts/images/common/0.gif" width=14 height=18 alt="" name="p'+p+'k'+k+'d1">';
      output+= '</td>';
    }
    output+= '</tr>\n';

    // and if necessary, have a blank line
    if (k==7 || k==15) output+= '<tr><td colspan=4>&nbsp;</td></tr>\n';
  }
  output+= '</table></td>\n';


  // and create the dice rolling bit, plus whose-go-is-it indicator etc
  output+= '<td valign="top"><form name="user"><table cellspacing=2 cellpadding=4 border=0>\n';
  output+= '<tr><td colspan=5>&nbsp;</td></tr>\n';
  output+= '<tr bgcolor="#008800"><td colspan=5 align=center><font color="#000000">Roll and select the dice you wish to remove</font></td></tr>\n';
  output+= '<tr bgcolor="#330000">'; for (x=0; x<5; x++) {
    output+= '<td><a href="javascript:select('+x+')" onFocus="blur();"><img src="/wp-content/scripts/images/common/blank.gif"';
    output+= ' width=40 height=40 name="die'+x+'" border=0 alt=""></a></td>';
  } output+= '</tr>\n';
  output+= '<tr bgcolor="#008800"><td colspan=5 align="center"><input style="width:40%" name="r" type="button" value=" 3 rolls left " onclick="roll(0)" onfocus="blur();">';
  output+= '&nbsp;&nbsp;&nbsp;<input style="width:5em" type="button" value=" unselect " onclick="roll(1)" onfocus="blur();"></td></tr>\n';
  output+= '<tr><td colspan=5>&nbsp;</td></tr>\n';

  output+= '<tr bgcolor="#008800"><td colspan=5 align="center" valign="middle"><font color="#000000">players: <select name="noofplayers" size=1>';
  for (x= 1; x<5; x++) {
    output+= '<option value="'+x+'"';
    if (x==maxplayers) output+= ' selected';
    output+= '>'+x;
  }
  output+= '</select>&nbsp;&nbsp;&nbsp;<input type="button" value=" new game " onclick="newgame()"></font></td></tr>\n';

  // ideally, I'd write some user-interface type instructions here,
  // and maybe a pointer to the general rules of Yahtzee (somewhere on the net?)

  output+= '</table></form></td>\n';
  output+= '</tr></table>\n';

  document.write(output);
  preload();

}