Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

Workshop: Putting It All Together

Listing 22.2 shows the complete draw poker game. In case you don't want to type all this yourself, you can download the complete script from this book's Web site at http://www.jsworkshop.com/.

Example 22.2. The Complete Draw Poker Game

<html>
<head>
<title>Draw Poker</title>
<script LANGUAGE="JavaScript1.1" type="text/javascript1.1">
var score = 100;
var dealt = false;
var hand = new Array(6);
var held = new Array(6);
var deck = new Array(53);
function DealDraw() {
   if (dealt == true) Draw();
   else Deal();
}
function Deal() {
// fill the deck (in order, for now)
   for (i=1; i<14; i++) {
     deck[i] = new Card(i,"c");
     deck[i+13] = new Card(i,"h");
     deck[i+26] = new Card(i,"s");
     deck[i+39] = new Card(i,"d");
   }
// shuffle the deck
   var n = Math.floor(400 * Math.random() + 500);
   for (i=1; i<n; i++) {
      card1 = Math.floor(52*Math.random() + 1);
      card2 = Math.floor(52*Math.random() + 1);
      temp = deck[card2];
      deck[card2] = deck[card1];
      deck[card1] = temp;
   }
// Deal and Display cards
   for (i=1; i<6; i++) {
      hand[i] = deck[i];
      document.images[i].src = hand[i].fname();
      document.images[i+5].src = "hold.gif";
      held[i] = false;
     }
   dealt = true;
   score = score - 1; //deduct one for bet amount
   document.form1.total.value = score;
   document.images[11].src="draw.gif";
   Addscore();
}
//Hold or discard a card
function Hold(num) {
   if (!dealt) return;
   if (!held[num]) {
      held[num]=true;
      document.images[5+num].src="hold2.gif";
   }
   else {
      held[num]=false;
      document.images[5+num].src="hold.gif";
   }
}
//Draw new cards
function Draw() {
   var curcard = 6;
   for (i=1; i<6; i++) {
      if (!held[i]) {
      hand[i] = deck[curcard++];
      document.images[i].src = hand[i].fname();
      }
   }
   dealt = false;
   document.images[11].src="deal.gif";
   score += Addscore();
   document.form1.total.value = score;
}
// Make a filename for an image, given Card object
function fname() {
   return this.num + this.suit + ".gif";
}
// Constructor for Card objects
function Card(num,suit) {
   this.num = num;
   this.suit = suit;
   this.fname = fname;
}
// Numeric sort function
function Numsort(a,b) { return a - b; }
// Calculate Score
function Addscore() {
   var straight = false;
   var flush = false;
   var pairs = 0;
   var three = false;
   var tally = new Array(14);
// sorted array for convenience
   var nums = new Array(5);
   for (i=0; i<5; i++) {
      nums[i] = hand[i+1].num;
   }
   nums.sort(Numsort);
// flush
   if (hand[1].suit == hand[2].suit &&
       hand[2].suit == hand[3].suit &&
       hand[3].suit == hand[4].suit &&
       hand[4].suit == hand[5].suit) flush = true;
// straight (Ace low)
   if (nums[0] == nums[1] - 1 &&
       nums[1] == nums[2] - 1 &&
       nums[2] == nums[3] - 1 &&
       nums[3] == nums[4] - 1) straight = true;
// straight (Ace high)
   if (nums[0] == 1 && nums[1] == 10 && nums[2] == 11
       && nums[3] == 12 && nums[4] == 13)
      straight = true;
// royal flush, straight flush, straight, flush
   if (straight && flush && nums[4] == 13 && nums[0] == 1) {
      document.form1.message.value="Royal Flush";
      return 100;
   }
   if (straight && flush) {
     document.form1.message.value="Straight Flush";
     return 50;
   }
   if (straight) {
      document.form1.message.value="Straight";
      return 4;
   }
   if (flush) {
      document.form1.message.value="Flush";
      return 5;
   }
// tally array is a count for each card value
   for (i=1; i<14; i++) {
      tally[i] = 0;
   }
   for (i=0; i<5; i++) {
      tally[nums[i]] += 1;
   }
   for (i=1; i<14; i++) {
      if (tally[i] == 4) {
         document.form1.message.value = "Four of a Kind";
         return 25;
      }
      if (tally[i] == 3) three = true;
      if (tally[i] == 2) pairs += 1;
   }
   if (three && pairs == 1) {
      document.form1.message.value="Full House";
      return 10;
   }
   if (pairs == 2) {
      document.form1.message.value="Two Pair";
      return 2;
   }
   if (three) {
      document.form1.message.value="Three of a Kind";
      return 3;
   }
   if (pairs == 1) {
      if (tally[1] == 2 || tally[11]==2
      || tally[12] == 2 || tally[13]==2) {
         document.form1.message.value="Jacks or Better";
         return 1;
      }
   }
   document.form1.message.value="No Score";
   return 0;
}
</script>
</head>
<body>
<img src="title.gif" width=381 height=81>
<hr>
<form NAME="form1">
<table>
<tr>
  <td> <img border=0 src="blank.gif" height=136 width=106>
  <td> <img border=0 src="blank.gif" height=136 width=106>
  <td> <img border=0 src="blank.gif" height=136 width=106>
  <td> <img border=0 src="blank.gif" height=136 width=106>
  <td> <img border=0 src="blank.gif" height=136 width=106>
  <td> </td>
</tr>
<tr>
  <td> <a href="#" onClick="Hold(1)">
       <img border=0 src="hold.gif" height=50 width=106></a>
  <td> <a href="#" onClick="Hold(2)">
       <img border=0 src="hold.gif" height=50 width=106></a>
  <td> <a href="#" onClick="Hold(3)">
       <img border=0 src="hold.gif" height=50 width=106></a>
  <td> <a href="#" onClick="Hold(4)">
       <img border=0 src="hold.gif" height=50 width=106></a>
  <td> <a href="#" onClick="Hold(5)">
       <img border=0 src="hold.gif" height=50 width=106></a>
</tr>
<tr>
  <td> <B>Total<BR>Score:</B>
       <input TYPE="TEXT" SIZE=6 NAME="total" VALUE="100"></td>
  <td colspan=2> <B>Current <BR>Hand:</B>
       <input TYPE="TEXT" SIZE=20 NAME="message"
        VALUE="Press DEAL to begin.">
  <td>
  <td> <a href="#" onClick="DealDraw()">
       <img border=0 src="deal.gif" height=50 width=106></a>
</tr>
</table>
</form>
</body>
</html>

Figure 22.2 shows Internet Explorer's display of the script after a game has been in progress for a while. (You'll notice that I'm losing.)

22fig02.jpg

Figure 22.2 The complete game in action.

Share ThisShare This

Informit Network