JavaScript Dice Game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bree
    New Member
    • Oct 2006
    • 1

    #1

    JavaScript Dice Game

    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>
Working...