473,789 Members | 2,957 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JavaScript Dice Game

1 New Member
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="javas cript">

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(amou nt);
amount = Math.round(amou nt);
// 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 2351

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

Similar topics

12
3129
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 to the mySQL database upon form submission. What I want to do is prevent cheaters from being able to create their own die roll, and the best way I've come up with to do this is to encrypt what gets stored in the hidden form field. However,...
101
6424
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 I can check cases by brute force? EC
1
6224
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 dice, the code for the rolling of the dice should take place in a user written module named rolldice. b.. The rules of the game are as follows:
1
2070
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 Internet and I couldn't really find the answer I was looking for. I asked around and they said this is really easy to implement but I still can't get it to work. My problem is it doesn't work for some reason... I think I'm not calling the url of my php...
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.