• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Hour 23. Creating a JavaScript Game > Workshop: Putting It All Together

Workshop: Putting It All Together

Listing 23.9 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/.

Listing 23.9 The Complete Draw Poker Game

1:  <html>
2:  <head>

3:  <title>Draw Poker</title>
4:  <script LANGUAGE="JavaScript1.1">
5:  var score = 100;
6:  var dealt = false;
7:  var hand = new Array(6);
8:  var held = new Array(6);
9:  var deck = new Array(53);
10:  function DealDraw() {
11:     if (dealt == true) Draw();
12:     else Deal();
13:  }
14:  function Deal() {
15:  // fill the deck (in order, for now)
16:     for (i=1; i<14; i++) {
17:       deck[i] = new Card(i,"c");
18:       deck[i+13] = new Card(i,"h");
19:       deck[i+26] = new Card(i,"s");
20:       deck[i+39] = new Card(i,"d");
21:     }
22:  // shuffle the deck
23:     var n = Math.floor(400 * Math.random() + 500);
24:     for (i=1; i<n; i++) {
25:        card1 = Math.floor(52*Math.random() + 1);
26:        card2 = Math.floor(52*Math.random() + 1);
27:        temp = deck[card2];
28:        deck[card2] = deck[card1];
29:        deck[card1] = temp;
30:     }
31:  // Deal and Display cards
32:     for (i=1; i<6; i++) {
33:        hand[i] = deck[i];
34:        document.images[i].src = hand[i].fname();
35:        document.images[i+5].src = "hold.gif";
36:        held[i] = false;
37:       }
38:     dealt = true;
39:     score = score - 1; //deduct one for bet amount
40:     document.form1.total.value = score;
41:     document.images[11].src="draw.gif";
42:     Addscore();
43:  }
44:  //Hold or discard a card
45:  function Hold(num) {
46:     if (!dealt) return;
47:     if (!held[num]) {
48:        held[num]=true;
49:        document.images[5+num].src="hold2.gif";
50:     }
51:     else {
52:        held[num]=false;
53:        document.images[5+num].src="hold.gif";
54:     }
55:  }
56:  //Draw new cards
57:  function Draw() {
58:     var curcard = 6;
59:     for (i=1; i<6; i++) {
60:        if (!held[i]) {
61:        hand[i] = deck[curcard++];
62:        document.images[i].src = hand[i].fname();
63:        }
64:     }
65:     dealt = false;
66:     document.images[11].src="deal.gif";
67:     score += Addscore();
68:     document.form1.total.value = score;
69:  }
70:  // Make a filename for an image, given Card object
71:  function fname() {
72:     return this.num + this.suit + ".gif";
73:  }
74:  // Constructor for Card objects
75:  function Card(num,suit) {
76:     this.num = num;
77:     this.suit = suit;
78:     this.fname = fname;
79:  }
80:  // Numeric sort function
81:  function Numsort(a,b) { return a - b; }
82:  // Calculate Score
83:  function Addscore() {
84:     var straight = false;
85:     var flush = false;
86:     var pairs = 0;
87:     var three = false;
88:     var tally = new Array(14);
89:  // sorted array for convenience
90:     var nums = new Array(5);
91:     for (i=0; i<5; i++) {
92:        nums[i] = hand[i+1].num;
93:     }
94:     nums.sort(Numsort);
95:  // flush
96:     if (hand[1].suit == hand[2].suit &&
97:         hand[2].suit == hand[3].suit &&
98:         hand[3].suit == hand[4].suit &&
99:         hand[4].suit == hand[5].suit) flush = true;
100:  // straight (Ace low)
101:     if (nums[0] == nums[1] - 1 &&
102:         nums[1] == nums[2] - 1 &&
103:         nums[2] == nums[3] - 1 &&
104:         nums[3] == nums[4] - 1) straight = true;
105:  // straight (Ace high)
106:     if (nums[0] == 1 && nums[1] == 10 && nums[2] == 11
107:         && nums[3] == 12 && nums[4] == 13)
108:        straight = true;
109:  // royal flush, straight flush, straight, flush
110:     if (straight && flush && nums[4] == 13 && nums[0] == 1) {
111:        document.form1.message.value="Royal Flush";
112:        return 100;
113:     }
114:     if (straight && flush) {
115:       document.form1.message.value="Straight Flush";
116:       return 50;
117:     }
118:     if (straight) {
119:        document.form1.message.value="Straight";
120:        return 4;
121:     }
122:     if (flush) {
123:        document.form1.message.value="Flush";
124:        return 5;
125:     }
126:  // tally array is a count for each card value
127:     for (i=1; i<14; i++) {
128:        tally[i] = 0;
129:     }
130:     for (i=0; i<5; i++) {
131:        tally[nums[i]] += 1;
132:     }
133:
134:     for (i=1; i<14; i++) {
135:        if (tally[i] == 4) {
136:           document.form1.message.value = "Four of a Kind";
137:           return 25;
138:        }
139:        if (tally[i] == 3) three = true;
140:        if (tally[i] == 2) pairs += 1;
141:     }
142:     if (three && pairs == 1) {
143:        document.form1.message.value="Full House";
144:        return 10;
145:     }
146:     if (pairs == 2) {
147:        document.form1.message.value="Two Pair";
148:        return 2;
149:     }
150:     if (three) {
151:        document.form1.message.value="Three of a Kind";
152:        return 3;
153:     }
154:     if (pairs == 1) {
155:        if (tally[1] == 2 || tally[11]==2
156:        || tally[12] == 2 || tally[13]==2) {
157:           document.form1.message.value="Jacks or Better";
158:           return 1;
159:        }
160:     }
161:     document.form1.message.value="No Score";
162:     return 0;
163:  }
164:  </script>
165:  </head>
166:  <body>
167:  <img src="title.gif" width=381 height=81>
168:  <hr>
169:  <form NAME="form1">
170:  <table>
171:  <tr>
172:    <td> <img border=0 src="blank.gif" height=136 width=106>
173:    <td> <img border=0 src="blank.gif" height=136 width=106>
174:    <td> <img border=0 src="blank.gif" height=136 width=106>
175:    <td> <img border=0 src="blank.gif" height=136 width=106>
176:    <td> <img border=0 src="blank.gif" height=136 width=106>
177:    <td> </td>
178:  </tr>
179:  <tr>
180:    <td> <a href="#" onClick="Hold(1);">
181:         <img border=0 src="hold.gif" height=50 width=106></a>
182:    <td> <a href="#" onClick="Hold(2);">
183:         <img border=0 src="hold.gif" height=50 width=106></a>
184:    <td> <a href="#" onClick="Hold(3);">
185:         <img border=0 src="hold.gif" height=50 width=106></a>
186:    <td> <a href="#" onClick="Hold(4);">
187:         <img border=0 src="hold.gif" height=50 width=106></a>
188:    <td> <a href="#" onClick="Hold(5);">
189:         <img border=0 src="hold.gif" height=50 width=106></a>
190:  </tr>
191:  <tr>
192:    <td> <B>Total<BR>Score:</B>
193:         <input TYPE="TEXT" SIZE=6 NAME="total" VALUE="100"></td>
194:    <td colspan=2> <B>Current <BR>Hand:</B>
195:         <input TYPE="TEXT" SIZE=20 NAME="message"
196:          VALUE="Press DEAL to begin.">
197:    <td>
198:    <td> <a href="#" onClick="DealDraw();">
199:         <img border=0 src="deal.gif" height=50 width=106></a>
200:  </tr>
201:  </table>
202:  </form>
203:  </body>
204:  </html>


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint