473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

JavaScript Dice Game

1
The game requires it not to accept negative numbers. At the moment it isnt, and it is urgent I find the solution asap. So if anyone can help I would much appreciate it.

Thanks Bree

This is the code so far.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Dice Game</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">

var imageArray = new Array(4); // Array of image objects
// Initialise the array elements to Image objects
for (var i = 0; i <= 3; i++)
{
imageArray[i] = new Image();
}
// Preload the images
imageArray[0].src = "dice1.gif";
imageArray[1].src = "dice2.gif";
imageArray[2].src = "dice3.gif";
imageArray[3].src = "dice4.gif";
imageArray[2].src = "dice5.gif";
imageArray[3].src = "dice6.gif";
// Play the game
function play()
{
var image1; // The first dice
var image2; // The second dice
var result; // Result of dice
var amount; // The user's amount

// Ask the user enter amount
amount = prompt("Enter the amount you want to bet", "");
// convert string to number
amount = parseFloat(amount);
amount = Math.round(amount);
// Randomly generate image1 and update the image on the screen
image1 = Math.round(Math.random() * 5) + 1;
document.images[0].src = "dice" + image1 + ".gif";

// Randomly generate image2 and update the image on the screen
image2 = Math.round(Math.random() * 5) + 1;
document.images[1].src = "dice" + image2 + ".gif";

//display results
result= image1 + image2;
// tell user if dice total = 2, 3, 4, 9, 10, 11 or 12 you win
if ((result == 3) || (result == 4) || (result == 9) || (result == 10)|| (result == 11))
{
alert("You Win $"+ amount);
}
else if ((result == 5) || (result == 6) || (result == 7) || (result == 8) )
{
alert("You Lose $"+ amount);
}
if ((result == 2))
{
alert ("You Win $"+(2*amount));
}
if ((result == 12))
{
alert ("You Win $"+(3*amount))
}

}
</script>
</head>

<body>
<h1>Dice Game</h1>
<p>Click on the Play button to place a bet and roll the dice. If 2, 3, 4, 9, 10, 11 or 12 comes up, you win. All numbers pay even money except 2 which pays double and 12 which pays triple. If 5, 6, 7 or 8 comes up, you lose. </p>
<p>
<img src="dice6.gif" width="62" height="62" hspace="10">
<img src="dice6.gif" width="62" height="62" hspace="10">
</p>
<p>&nbsp; </p>
<form>
<input type="button" value="Play" onClick="play()">
</form>
</body>
</html>
Oct 12 '06 #1
0 2304

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: googlegroups | last post by:
Hi, I'm making a javascript program for rolling dice for a roleplaying game that's played in a forum. The die roll gets generated, gets stored as text in a hidden form field, and then gets written...
101
by: Elijah Cardon | last post by:
Let's say I have m dice having n sides, such that n^m is not going to bust int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}. What is a good way to represent this in c so that...
1
by: lenest | last post by:
I need help writing a program.... You are to write a python program to accomplish the following: a.. Play a dice game of Craps using a random number generator to simulate the roll of the...
1
by: Van Dugall | last post by:
Hello I have been writing a game in javascript which works fine but I wanted to step it up and request the data of the game from the server. I haven't really used php...all I know is from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.