Sams Teach Yourself JavaScript in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Part I: Getting Started
- Hour 1. Understanding JavaScript
- Hour 2. Creating a Simple Script
- Hour 3. How JavaScript Programs Work
- Part II: Learning JavaScript Basics
- Hour 4. Using Functions and Variables
- Hour 5. Using Strings and Arrays
- Hour 6. Testing and Comparing Values
- Hour 7. Repeating Yourself: Using Loops
- Hour 8. Using Math and Date Functions
- Part III: The Document Object Model (DOM)
- Hour 9. Working with the Document Object Model
- Hour 10. Responding to Events
- Hour 11. Using Windows and Frames
- Hour 12. Getting Data with Forms
- Hour 13. Using Graphics and Animation
- Part IV: Moving on to Advanced JavaScript Features
- Hour 14. Creating Cross-Browser Scripts
- Hour 15. Creating Custom Objects
- Hour 16. Working with Sounds and Plug-Ins
- Hour 17. Debugging JavaScript Applications
- Part V: Working with Dynamic HTML (DHTML)
- Hour 18. Working with Style Sheets
- Hour 19. Using Dynamic HTML (DHTML)
- Hour 20. Using Advanced DOM Features
- Part VI: Putting It All Together
- Hour 21. Improving a Web Page with JavaScript
- Hour 22. Creating a JavaScript Game
- Planning the Program
- Writing the Program
- Workshop: Putting It All Together
- Summary
- Q&A
- Quiz
- Exercises
- Hour 23. Creating DHTML Applications
- Hour 24. JavaScript Tips and Tricks
- Part VII: Appendices
- Appendix A. Other JavaScript Resources
- Appendix B. Tools for JavaScript Developers
- Appendix C. Glossary
- Appendix D. JavaScript Quick Reference
- Appendix E. DOM Quick Reference
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.)
Figure 22.2 The complete game in action.
Summary | Next Section

Account Sign In
View your cart