login about faq

To prove you're not a spammer, email newuser.lgqa@gmail.com with the subject "Account Request" to request an account.


    <html>
<h1>Dice Game</h1>

<input type="button" onclick="Rules()" value="Click to Learn the Rules!"/>
<p></p>
<script>
function Rules()
{alert("Click to start the game...Follow the prompts through for five turns and at the end you will recieve a total score...The aim of the game is to achieve the highest possible score... Have Fun! :)")
}
</script>

<input type="button" onclick="roll()" value="Click to start the game" />

<script>

function roll()
{
var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    alert("Ready to roll!");
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)

var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)

var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)

var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)

var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)

}
</script>

<p></p>
<p></p>
<input type="button" onclick="rollAgain()" value="Play again" />

<script>
function rollAgain()
{
var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    {   
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)
    }
var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    {   
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)
    }
var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    {   
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)
    }
var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    {   
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)
    }
var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    {   
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)
    }
}   
</script>

<p></p>
<p></p>

<input type="button" onclick="end()" value="End game" />

<script>
function end()
{
Alert("Thank you for playing")
}
</script>

</html>

asked Sep 13 '12 at 00:51

untaken_screen_name's gravatar image

untaken_screen_name
13181115

closed Sep 13 '12 at 09:40

Mattophobia's gravatar image

Mattophobia ♦♦
7.0k74122206

The question has been closed for the following reason "The question is answered, right answer was accepted" by Mattophobia Sep 13 '12 at 09:40


To add up the total score you need a variable at the beginning of the roll() function and you need to add the score of every dice thow to the total score. Also since the roll() and rolAgain() functions are the same (except for the "ready to roll" dialog) you can just call the roll() methods for both the start game and play again buttons, or have the rollAgain() function call the roll() function and possibly create a new function for the start game button that just displays the "ready to roll" dialog and calls the roll() function.

<script>

function roll()
{
var totalScore = 0;
var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    totalScore = totalScore + score;
    alert("Ready to roll!");
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)

var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    totalScore = totalScore + score;
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)

var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    totalScore = totalScore + score;
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)

var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    totalScore = totalScore + score;
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)

var die1 = Math.floor(Math.random()*6 + 1);
    var die2 = Math.floor(Math.random()*6 + 1);
    score = (die1 + die2)
    totalScore = totalScore + score;
    alert("You rolled a " + die1 + " and a " + die2 + " for a score of " + score)

    alert("Your total score for this game was " + totalScore);
}
</script>

answered Sep 13 '12 at 04:47

nickjuly4's gravatar image

nickjuly4
95191621

edited Sep 13 '12 at 04:52

1

Thankyou very much for your help. Your a life saver :)

(Sep 13 '12 at 04:58) untaken_screen_name untaken_screen_name's gravatar image

You're welcome! :)

(Sep 13 '12 at 05:18) nickjuly4 nickjuly4's gravatar image

For a school project i have been asked to create a simple web based game using Java script. I have the basic part of the game worked out but i need the scores of the 5 dice throws to accumulate to one total score that can be shown in a dialogue box. Any help would be greatly appreciated. Thanks in advance.

I am posting this part of my question in the comments because there was a problem with the formatting of the code when i put both the question and the code in the question description box.

answered Sep 13 '12 at 00:55

untaken_screen_name's gravatar image

untaken_screen_name
13181115

feel free to copy and paste this into notepad or similar software to see the script working. This may help you understand what i mean better.

(Sep 13 '12 at 00:58) untaken_screen_name untaken_screen_name's gravatar image

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported


Tags:

×450
×195
×129
×91
×60
×58
×41
×7
×6

Asked: Sep 13 '12 at 00:51

Seen: 863 times

Last updated: Sep 13 '12 at 09:40