Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
How to make a dice game for two player in javascript and html using just two dice?

1. Each player throws both dice once per turn. You only score if you throw doubles (that is, both dice have the same number of spots on their top face).

2. Players score five points for double ones, twos, fours or fives. A double six scores twenty five points, but if you throw a double three your score goes back to zero.
3. Add your score as you play. The first player to get fifty points wins the game. (An adult or older child may need to help score, but you can use this game to teach younger children to count by fives.)
Posted
Comments
LanFanNinja 10-Dec-11 17:15pm    
Homework?
I noticed someone gave you a good answer at this link posted just a few minutes ago.

http://www.webdeveloper.com/forum/showthread.php?p=1183910

In case you have not checked it yet. :)

I also noticed you posted this question to http://answers.unity3d.com
Probably will not get any help there for this question.

Did you also post this question to StackOverflow??

I am not trying to be mean with my comment here. Most people simply do not have the time to create projects for other people. Start creating the project and then if you have problems with something post a question here describing the problems you are having. Maybe we can help you then.

P.S. The name of the game you are describing is called Fifty I think if that is any help to you.
[no name] 11-Dec-11 0:22am    
Hey tidu12,
This is too much. I read your comment on LanFanNinja's answer. You want all the things ready-made. Not good!

This looks more like am order for development of the software, not a question for expert advice. We don't do it. Please do yourself a favor: develop this game all by yourself, only in this case it would have some sense.

If you face some particular problem and able to formulate it correctly, we will gladly help you.

—SA
 
Share this answer
 
Comments
LanFanNinja 10-Dec-11 17:26pm    
+5 He/she has been busy posting the question many places on the internet. Most of the posts have been closed.
Sergey Alexandrovich Kryukov 10-Dec-11 17:53pm    
Thank you.
--SA
[no name] 11-Dec-11 0:20am    
My 5!
Sergey Alexandrovich Kryukov 14-Dec-11 13:00pm    
Thank you, Pranit.
--SA
Ok so I have only used javascript a couple of times but I have decided to try and help you. I created this code based on the code you posted in solution 2. Maybe this will give you an idea of something you can do.

Note: I made it into a webpage.

XML
<html>
<body>

<input type="button" value="Roll The Dice" onClick="rollDice()" />
<br />

<script type="text/javascript">
var score = 0;
var maxScore = 50;
var rolls = 0;
var maxRolls = 20;


function rollDice()
{
    var x = Math.floor( Math.random() * 6 ) + 1;
    var y = Math.floor( Math.random() * 6 ) + 1;

    if( x == y )
    {
        score = getScore( x );
        alert("You threw a Double " + x + " Your Score is "+ score);
    }
    else
    {
        alert("You threw a " + x + " and a " + y + " Your Score is " + score);
    }

    rolls++;

    if (rolls == maxRolls && score < maxScore)
    {
        alert("Sorry You Lose!");
        score = 0;
        rolls = 0;
        return;
    }
    else if (score >= maxScore)
    {
        alert("Congratulations You Win!");
        score = 0;
        rolls = 0;
        return;
    }
}

function getScore(x)
{
    switch( x )
    {
        case 1:
            score += 5;
            break;
        case 2:
            score += 5;
            break;
        case 3:
            score = 0;
            break;
        case 4:
            score += 5;
            break;
        case 5:
            score += 5;
            break;
        case 6:
            score += 25;
            break;
    }

    return score;
}
</script>
</body>
</html>
 
Share this answer
 
v5
Comments
tidu12 10-Dec-11 21:35pm    
Can You please write it in while loop that will run until a winner is determined when he gets the score of 50 or after so many throws when he gets a score 50 it stops.
LanFanNinja 10-Dec-11 21:45pm    
I modified my solution to demonstrate a way you could keep track of the score and number of rolls. This really is pretty much the extent of my javascript skills. :)

You can modify this anyway you like including using a while loop to call rollDice() and perhaps changing the alert() calls to document.write() or something?!?

I really wish I could help you more but I don't have the time to learn javascript right now (and don't want to) and that is basically what I would have to do to help you any further.

It seems to me you have quite a lot of learning to do yourself. Programming is about exploring and finding new and interesting ways to solve problems. It is not about getting someone else to solve these problems for you.

Good luck!
Can someone please write this code in while loop that will run until a winner is determined when he gets the score of 50 or after so many throws when he gets a score 50 it stops.

Please????????????!!!!!!!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900