<!--
//=====================================================================
// 4-5-6    : Program Simulates the 4-5-6 Dice Game, (c) 1997
// Author   : Chris Strickland
// Company  : New Horizons Software
//          : Links to the WEB page are welcomed 
//          : Any copying of the code on this page will be under the
//          : standard shareware license agreement.  Donations of $10
//          : are appreciated and will encourage future development of
//          : shareware WEB products by New Horizons Sofware.  
//          : Send checks to:
//          : Chris Strickland
//          : 1738 Ayshire Dr
//          : Titusville FL, 32796
//          : Donations will include 90 days of free email support
//          : and six months of free upgrades.
// Developed: Feb 26, 97 
// Modified : Mar  7, 97 - Removed the this.Player and this.NewGame
//          : to try and make the game compatible with IE.
//
// Bugs     : 
//===================================================================
//

TRUE=1;
FALSE=0;
DEBUG=FALSE;

function createArray(n, init)
{
   this.size = n  
   for (i = 1 ; i <= n ; i++)
         this[i] = init      //Initialize all elements
   return this               //Return the array object 
}

//---------------------------------------------------------
// Create and initialize string arrays
//---------------------------------------------------------
function initStringArray(m)
{
   var i;
   this.size = m;
   for (i = 0 ; i <= m ; i++)
         this[i] = ""   //Initialize all of the left hand elements
   return this;         //Return the  array object to the caller
}

function initPlayers()
{
   this.PlayerName=new initStringArray(2);
   this.PlayerName[0]="Banker's";
   this.PlayerName[1]="Your"; 
}

function FillDice()
{
   dice[0]='./one.gif';
   dice[1]='./two.gif';
   dice[2]='./three.gif';
   dice[3]='./four.gif';
   dice[4]='./five.gif';
   dice[5]='./six.gif';
}

function reset()
{
   if (DEBUG==TRUE) document.write("Enter Reset<BR>");
   document.cookie="play=00";
   document.cookie="pont=00";
   this.Player=0;
   this.NewGame=TRUE;
}

// --------------------------
// Generate the Dice Roll
// --------------------------

function RollDice(n,high)
{
   var i;

   v = new createArray(n,0);
   document.write("<P><CENTER>");
   for (i=0; i<=n; i++) {
      a=Math.random();
      this.v[i]=Math.ceil(a*high);
      document.write("<IMG SRC="+dice[v[i]-1]+" >");
   }
   document.write("<P>");
   document.write("</CENTER>");
}

// --------------------------------------------
// Check for Pairs
// --------------------------------------------

function CheckPair(sucker)
{

   if (DEBUG == TRUE) document.write("Enter CheckPair<BR>");

   var pair=0;
   var one=0;
   var two=0;
   var three=0;
   var four=0;
   var five=0;
   var six=0;
   face=new createArray(6, 0);

// Count up matching dice

   for (i=1; i<3; i++) {
      for (j=0; j<i; j++)
         if (v[i]==v[j]) pair=pair+1;
   }

// Check for three of a kind

   if (pair == 3) {
      document.write (PlayerName[eval(Player)]," Has Three of a Kind, A Winner!!!<BR>");
      reset();
   }

// See if it's a pair, had one dice match

   if (pair == 1) {
      for (i=0;i<3;i++) {
         for (j=1;j<7;j++) {
           if (v[i]==j) face[j]=face[j]+1;
         }
      }

// Check to see if a 1, a sure loser

      if (face[1] == 1) {
         document.write("A One With A Pair, ",PlayerName[eval(Player)]," a loser!!!<BR>");
         reset();
      }

// Check to see if a 6, a sure winner

      if (face[6] == 1) {
         document.write("A Six With A Pair, ",PlayerName[eval(Player)]," a winner!!!<BR>");
         reset();
      }

      if (NewGame == FALSE) CheckPoint();

   } // End Pair Check

// Check to see if we need a re-roll

   if (NewGame == FALSE) MeaningLess(pair, sucker);

}

function CheckPoint()
{

   var i=0;
   var xstr="";
   var prvPoint=0;

   if (DEBUG==TRUE) document.write("Enter CheckPoint<BR>");

   for (i=2;i<6;i++) {
      if (face[i] == 1) {
         document.write(PlayerName[eval(Player)], " Point is: ",i,"<BR>");
         if (eval(Player)==1) {
            prvPoint=fromCookie(2);
            if (eval(prvPoint) >= i){
               document.write("Banker's ",eval(prvPoint));
               document.write(" Point Wins<BR>");
            }
            else {
               document.write("Your point beats Banker's ");
               document.write(eval(prvPoint), " Point<BR>");
            }
            reset();
         }
         else {
            document.cookie="pont=0"+i;

            if (DEBUG==TRUE)
               document.write("Before Player=",Player,"<BR>");

            this.Player=eval(1-Player);

            if (DEBUG==TRUE)
               document.write("After Player=",Player,"<BR>");

            document.cookie="play=0"+Player;

            if (DEBUG==TRUE) xstr=fromCookie(1);
         }
      }
   }
}

function MeaningLess(pair, sucker)
{

   var xstr="";

   if (DEBUG==TRUE) document.write("Enter MeaningLess<BR>");

   if (pair == 0  && sucker==1)
      document.write("Meaningless Roll, Re-Roll Dice<BR>");

   if (DEBUG==TRUE) xstr=fromCookie(1);

}

// Check to see if the dice rolled a 4-5-6

function Check456()
{

   if (DEBUG == TRUE) document.write("Enter Check456<BR>");

   var four=0;
   var five=0;
   var six=0;
   var score=0;
   var i=0;

// Check dice for 4, 5 and 6

   for (i=0; i<3; i++) {
      if (v[i]==4) four=1;
      if (v[i]==5) five=1;
      if (v[i]==6) six=1
   }           

// Total 4, 5, 6 counter

   score=four+five+six;

// If it's 3 then we had a 4-5-6

   if (score == 3) {
      document.write("It's a 4-5-6, ",PlayerName[eval(Player)]," Won!!!<BR>");
      sucker=0;
      reset();
   }
   return sucker;
}

// --------------------------------------------------------
// Function determines if a 1-2-3 was rolled.
// --------------------------------------------------------

function Check123()
{

   if (DEBUG == TRUE) document.write("Enter Check123<BR>");

   var one=0;
   var two=0;
   var three=0;
   var score=0;

   for (i=0; i<3; i++) {
      if (v[i]==1) one=1;
      if (v[i]==2) two=1;
      if (v[i]==3) three=1
   }           
   score=one+two+three;
   if (score == 3) {
      document.write("A 1-2-3 ", PlayerName[eval(Player)]);
      document.write(" a Loser, Sucker!!!<BR>");
      sucker=0;
      reset();
   }
   return sucker;
}

//--------------------------------------------------------------------
     
function fromCookie(choice)
{
   var cookstr=document.cookie;
   var xstr="";
   var tmpstr="";
   var pos=0;
   var xlen=0;
   var end=0;
   var equal=0;

   if (DEBUG == 1) 
      document.write("fromCookie: cookie={",
         cookstr,"}<BR>");

   if (choice==1) {
      pos=cookstr.indexOf("play=");
   }

   if (choice==2) {
      pos=cookstr.indexOf("pont=");
   }

   xstr=cookstr.substring(pos+5,pos+7);

   if (DEBUG == 1)
      document.write("fromCookie: pos=",pos,
         " xstr=", xstr," Choice=",choice,"<BR>");

   return xstr;
}    

//---------------------------------------------------------

