<HTML><HEAD>
<!--
---------
DICE Game
---------
-->
<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers
/*
THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
Copyright (c)1998 by Charles River Media. All Rights Reserved.
This applet can only be re-used or modifed by license holders of the
JavaScript Cookbook CD-ROM. Credit must be given in the source
code and this copyright notice must be maintained. If you do
not hold a license to the JavaScript Cookbook, you may NOT
duplicate or modify this code for your own use.
Use at your own risk. No warranty is given or implied of the suitability
of this applet for any specific application. Neither Erica Sadun nor
Charles River Media will be held responsible for any unwanted effects
due to the use of this applet or any derivative.
*/
//------------------RANDOM NUMBERS----------------------------
var js_mult1=3141
var js_mult2=5821
var js_m1=100000000
var js_m2=10000
var js_iseed=0
var js_iseed1=0
var js_iseed2=0
// Return a Random Integer between 1 and N
// (adapted from cookbook standard 0 to N-1)
function random(n)
{
if (js_iseed == 0)
{
now = new Date()
js_iseed = now.getHours() + now.getMinutes() * 60
+ now.getSeconds() * 3600
}
js_iseed1 = js_iseed / js_m2
js_iseed2 = js_iseed % js_m2
var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) *
js_m2 + (js_iseed2 * js_mult2)) % js_m1
js_iseed = (tmp + 1) % js_m1
return (Math.floor((js_iseed/js_m1) * n) + 1)
}
//----------------------PATH UTILITIES--------------------------
// Get the grandparent directory and append aDir
function getSibling(aDir)
{
var loc = ""+document.location.href
// get this dir, then parent
var base = loc.substring(0, loc.lastIndexOf("/"))
base = base.substring(0, base.lastIndexOf("/")+1)
return base+aDir
}
//------------------ARRAY CREATION----------------------------
// create an array
function createArray(n)
{
for (var i = 0; i < n; i++) {this[i] = 0}
return this
}
// --------------------THE DICE ROLL---------------------
// Roll the dice
function roll(aform)
{
// Game must be started and not finished to roll
if ((!gameInProgress) || (turns > 12))
{
alert("No rolls left. Please start a new game.")
return(false)
}
if (thrown)
{
alert("You must first assign the previous roll.")
return(false)
}
turns++
// Determine Number of Dice and Sides
var num = 5
var sides = 6
// Get the Dice Image Directory
var baseRef = getSibling("GRAFX/DICE/DIE")
// Frame the body
var s1 = "<BODY BGCOLOR=\"ffffff\"><FONT SIZE=6 COLOR=\"770000\"><CENTER>"
var s2 = "</CENTER></FONT></BODY>"
// Frame the table
var t1 = "<TABLE BORDER=1><TR>"
var t2 = "</TABLE></TR>"
// Frame the specific images
var i1 = "<TD><IMG SRC=\""+baseRef
var i2 = ".GIF\" HEIGHT=32 WIDTH=32></TD>"
// Initialize dice window
parent.JCdice.document.open()
parent.JCdice.document.write(s1+t1)
// set dice count to zero
for (var i = 1; i <= sides; i++) dice[i] = 0
// throw the dice
for (var i = 1; i <= num; i++) {
var mythrow = random(sides) // throw a die
dice[mythrow]++ // increment the throw counter
parent.JCdice.document.write(i1+mythrow+i2) // create drawing
}
// Finish the dice window
parent.JCdice.document.write(t2+s2)
parent.JCdice.document.close()
return thrown = true
}
// Set up Double Buttons
function setupForm(t1,act1, t2,t3, act3, t4)
{
document.write("<tr>")
document.write("<td align=right><INPUT TYPE='BUTTON' VALUE='"+t1+"' ");
document.write("onClick='"+act1+"'></td>")
document.write("<td><INPUT TYPE='TEXT' VALUE='' NAME='"+t2+"' SIZE=3></td>")
document.write("<td align=right><INPUT TYPE='BUTTON' VALUE='"+t3+"' ");
document.write("onClick='"+act3+"'></td>")
document.write("<td><INPUT TYPE='TEXT' VALUE='' NAME='"+t4+"' SIZE=3></td>")
document.write("</tr>")
}
// Set up Double Text Entries
function setupForm2(t1,t2,t3,t4)
{
document.write("<tr>")
document.write("<td align=right>"+t1+"</td>")
document.write("<td><INPUT TYPE='TEXT' VALUE='0' NAME='"+t2+"' SIZE=5></td>")
document.write("<td align=right>"+t3+"</td>")
document.write("<td><INPUT TYPE='TEXT' VALUE='0' NAME='"+t4+"' SIZE=5></td>")
document.write("</tr>")
}
// --------------------GAME INITIALIZATION---------------------
// Initialize Global Variables
var dice = createArray(6)
var thrown = false
var gameInProgress = false
var turns = 0
// Start a new game.
function initGame()
{
if (gameInProgress && (turns < 13) && thrown)
{
if (!confirm("Game already in progress. Start over?")) return(false)
}
// Set all fields to zero or empty
for (var i = 1; i <= 6; i++)
eval("document.forms[0].count"+i+".value = ''")
for (var i = 3; i <= 5; i++)
eval("document.forms[0].kind"+i+".value = ''")
document.forms[0].ss.value=''
document.forms[0].ls.value=''
document.forms[0].fh.value=''
document.forms[0].sum.value=''
document.forms[0].SUBTOT1.value='0'
document.forms[0].SUBTOT2.value='0'
document.forms[0].BONUS.value='0'
document.forms[0].TOT1.value='0'
document.forms[0].TOT2.value='0'
// Start game with a throw
thrown = false
gameInProgress = true
turns = 0
return roll()
}
// Create the Data Entry Form
function createForm()
{
document.write("<CENTER><FORM><TABLE BORDER=2>")
document.write("<TR><TD COLSPAN=4 ALIGN=CENTER><B>Dice Roll Game</B></TD></TR>")
setupForm("1s","simple(1)","count1","3 of a kind","mult(3)","kind3")
setupForm("2s","simple(2)","count2","4 of a kind","mult(4)","kind4")
setupForm("3s","simple(3)","count3","Potsie","mult(5)","kind5")
setupForm("4s","simple(4)","count4","Sm. Straight","sstraight()","ss")
setupForm("5s","simple(5)","count5","Lg. Straight","lstraight()","ls")
setupForm("6s","simple(6)","count6","Full House","fullhouse()","fh")
document.write("<tr><td align=right>Subtotal</td>")
document.write("<td><INPUT TYPE='TEXT' NAME='SUBTOT1' SIZE=5 VALUE='0'></td>")
document.write("<td align=right><INPUT TYPE='BUTTON' VALUE='Sum of Dice' ")
document.write("onClick='sum()'</TD>")
document.write("<td align=left><INPUT TYPE='TEXT' NAME='sum'")
document.write("SIZE=3 VALUE=''></td></tr>")
setupForm2("Bonus (over 63)", "BONUS", "Subtotal", "SUBTOT2")
setupForm2("Total","TOT1", "Grand Total", "TOT2")
document.write("<tr><td colspan=2 align=center>")
document.write("<INPUT TYPE='BUTTON' VALUE='Start New Game' ")
document.write("onClick='initGame()'></td>")
document.write("<td colspan=2 align=center>")
document.write("<INPUT TYPE='BUTTON' VALUE='Roll Dice' ")
document.write("onClick='roll()'></td></tr>")
document.write("</TABLE></FORM></CENTER>")
}
// --------------------SCORE UPDATES---------------------
// Updates after a score on the left hand side
function updateTotalsLeft(d)
{
// update totals
var subtotal1 = parseInt(document.forms[0].SUBTOT1.value) + d
var subtotal2 = parseInt(document.forms[0].SUBTOT2.value)
var bonus = (subtotal1 < 63) ? 0 : 35
var total = subtotal1 + bonus
var grandtotal = total + subtotal2
document.forms[0].SUBTOT1.value = subtotal1
document.forms[0].SUBTOT2.value = subtotal2
document.forms[0].TOT1.value = total
document.forms[0].TOT2.value = grandtotal
document.forms[0].BONUS.value = bonus
}
// Updates after a score on the right hand side
function updateTotalsRight(d)
{
// update totals
var subtotal1 = parseInt(document.forms[0].SUBTOT1.value)
var subtotal2 = parseInt(document.forms[0].SUBTOT2.value)+d
var bonus = (subtotal1 < 63) ? 0 : 35
var total = subtotal1 + bonus
var grandtotal = total + subtotal2
document.forms[0].SUBTOT1.value = subtotal1
document.forms[0].SUBTOT2.value = subtotal2
document.forms[0].TOT1.value = total
document.forms[0].TOT2.value = grandtotal
document.forms[0].BONUS.value = bonus
}
// --------------------SCORING TOOLS---------------------
// Sums of ones through sixes
function simple(n)
{
if (!thrown)
{
alert("You must first roll the dice")
return false
}
if (eval("document.forms[0].count"+n+".value != ''"))
{
alert("You have already used this button once in this game.")
return false
}
// Dice Value
var d = dice[parseInt(n)]*parseInt(n)
// update field + totals
eval("document.forms[0].count"+n+".value = "+d)
updateTotalsLeft(d)
return thrown = false
}
// Multiple throws (n of a kind)
function mult(n)
{
if (!thrown)
{
alert("You must first roll the dice")
return false
}
if (eval("document.forms[0].kind"+n+".value != ''"))
{
alert("You have already used this button once in this game.")
return false
}
// Find the greatest multiple
var m = 0
for (var i = 1; i <= 6; i++) if (dice[i] > m) m = dice[i]
// Find the sum of the dice
var sum = 0
for (var i = 1; i <= 6; i++) sum += i*dice[i]
// Check for n of a kind
if (n == 3) {if (m >= 3) d = sum; else d = 0}
else if (n == 4) {if (m >= 4) d = sum; else d = 0}
else if (n == 5){if (m >= 5) d = 50; else d = 0}
// update field & totals
eval("document.forms[0].kind"+n+".value = "+d)
updateTotalsRight(d)
return thrown = false
}
// Sum of Dice
function sum()
{
if (!thrown)
{
alert("You must first roll the dice")
return false
}
if (document.forms[0].sum.value != '')
{
alert("You have already used this button once in this game.")
return false
}
// Find the sum
var sum = 0
for (var i = 1; i <= 6; i++) sum += i*dice[i]
// update field & totals
document.forms[0].sum.value = sum
updateTotalsRight(sum)
return thrown = false
}
// Full House
function fullhouse()
{
if (!thrown)
{
alert("You must first roll the dice")
return false
}
if (document.forms[0].fh.value != '')
{
alert("You have already used this button once in this game.")
return false
}
// Find the two and three
var two = 0
for (var i = 1; i <= 6; i++) if (dice[i] == 2) two = i
var three = 0
for (var i = 1; i <= 6; i++) if (dice[i] == 3) three = i
if ((two != 0) && (three != 0)) d = 25; else d = 0
// update field & totals
document.forms[0].fh.value = d
updateTotalsRight(d)
return thrown = false
}
// Large Straight
function lstraight()
{
if (!thrown)
{
alert("You must first roll the dice")
return false
}
if (document.forms[0].ls.value != '')
{
alert("You have already used this button once in this game.")
return false
}
// Assure all are ones for lower or upper
var lower = true
for (var i = 1; i <= 5; i++) if (dice[i] == 0) lower = false
var upper = true
for (var i = 2; i <= 6; i++) if (dice[i] == 0) upper = false
if (lower || upper) d = 40; else d = 0
// update field & totals
document.forms[0].ls.value = d
updateTotalsRight(d)
return thrown = false
}
// Small Straight
function sstraight()
{
if (!thrown)
{
alert("You must first roll the dice")
return false
}
if (document.forms[0].ss.value != '')
{
alert("You have already used this button once in this game.")
return false
}
// check lower then middle then upper
var lower = true
for (var i = 1; i <= 4; i++) if (dice[i] == 0) lower = false
var middle = true
for (var i = 2; i <= 5; i++) if (dice[i] == 0) middle = false
var upper = true
for (var i = 3; i <= 6; i++) if (dice[i] == 0) upper = false
if (upper || middle || lower) d = 30; else d = 0
// update field & totals
document.forms[0].ss.value = d
updateTotalsRight(d)
return thrown = false
}
<!-- done hiding --></SCRIPT></HEAD>
<BODY bgcolor="ffffff" link="0000ff" vlink="770077">
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/SPICE.JPG" WIDTH=37 HEIGHT=72
ALIGN = LEFT>"Potsie", the Dice Roll Game</H1></FONT>
<BLOCKQUOTE><FONT COLOR="770000">
Enjoy this game of counting dice rolls. Each "slot"
can be filled only once. A little experimentation will
familiarize you with the rules. Five of the same
kind is considered a "Potsie".
</FONT></BLOCKQUOTE>
<BR><BR>
<SCRIPT>
createForm()
</SCRIPT>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
This JavaScript program is simply huge. It demonstrates how
a large sophisticated application can be built in JavaScript.
</FONT>
<h5>Copyright ©1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>