Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## javascript logic question

Hi,

I created a dice game using the code below. I would like to build on it, to build a craps game, asking for help on the programming logic either using if else, or switch statements. The dice can roll random numbers, would like to be able to:

1) First roll, if the dice roll 7 or 11 you win
2) First roll, if the dice roll 2,3,12 you lose
3) First roll, if the dice roll 4,5,6,8,9,10 - that becomes the point, you must roll one of these to win before you roll a 7, if you roll 7 you lose

Thanks!!
DM

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>

var diceFace=new Array("images/c1.gif", "images/c2.gif", "images/c3.gif", "images/c4.gif", "images/c5.gif", "images/c6.gif");

function throwdice(){
//create a random integer between 0 and 5
var randomdice=Math.round(Math.random()*5);
var randomdice2=Math.round(Math.random()*5);
document.images["mydice"].src=diceFace[randomdice];
document.images["mydice2"].src=diceFace[randomdice2];
}
</script>

<body>

<img src="images/d1.gif" name="mydice" id="mydice">
<img src="images/d1.gif" name="mydice2" id="mydice2">

<form>
<input type="button" value="Throw dice!" onClick="timer()">
</form>

</body>
</html>

• Before you go any further, be aware that
var randomdice=Math.round(Math.random()*5);
does not give a true random distribution as 1,2,3,4 are selected more frequently than 0 or 5. You can easily test that for yourself.
You should use
var num = 5;
var randomdice = Math.floor(Math.random() * (num +1)); // generates 0-5 truly randomly

<input type="button" value="Throw dice!" onClick="timer()">
There is no function named timer().

For the rest, show us the code you have created so far. We will not do your homework, but we will help you correct/improve the code that you yourself write.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

• Thanks for your feedback! It seems to work well for the basics, one that I've been stumped on is tracking wins and losses. Still researching...

var dicefaces = new Array;
dicefaces[0] = new Image(50,50);
dicefaces[0].src = "images/d1.gif";
dicefaces[1] = new Image(50,50);
dicefaces[1].src = "images/d2.gif";
dicefaces[2] = new Image(50,50);
dicefaces[2].src = "images/d3.gif";
dicefaces[3] = new Image(50,50);
dicefaces[3].src = "images/d4.gif";
dicefaces[4] = new Image(50,50);
dicefaces[4].src = "images/d5.gif";
dicefaces[5] = new Image(50,50);
dicefaces[5].src = "images/d6.gif";

var playerturn;
playerturn = true;
var pointvalue;
document.game.point.value = pointvalue;

/////////////////////////////////

function throwdice() {
//create a random integer between 0 and 5
var genvalue;
var randomdice1;
var randomdice2;
var sum;
var playerwin;
var playerloss;
genvalue=Math.floor(Math.random()*6);
randomdice1 = genvalue+1;
document.dieimage1.src=dicefaces[genvalue].src;
genvalue=Math.floor(Math.random()*6);
randomdice2 = genvalue+1;
document.dieimage2.src=dicefaces[genvalue].src;
sum = randomdice1 + randomdice2;

if (playerturn) {
document.game.point.value = "";
switch (sum) {
case 7:
case 11:
document.game.status.value = "You win! Throw Again!";
break;
case 2:
case 3:
case 12:
document.game.status.value = "You lose. Throw Again!";
break;
default:
playerturn = false;
pointvalue = sum;
document.game.point.value = pointvalue;
document.game.status.value ="Throw again, hit your point & win.";
}
}
else {
switch (sum) {
case 7:
document.game.status.value = "You lose. Throw Again!";
playerturn = true;
break;
case pointvalue:
document.game.status.value = "You win. Throw Again!";
playerturn = true;
}
}
}

//set timeout function
function timer() {
setTimeout(function() { throwdice() }, 1000);
}

function endgame(){

}

</script>

<body>

<img src="images/d1.gif" name="dieimage1" id="mydice">
<img src="images/d1.gif" name="dieimage2" id="mydice">
<br>
<a href="" onClick="timer(); return false;">Click to throw the dice.</a>

<form name="game">
Status <input name="status" value="" size=35>
<br/>
Point value <input name="point" value="" size=4>
<br/>
Win total <input name="win" value="" size=4>
Loss total <input name="loss" value="" size=4>
</form>

<a href="" onClick="endgame(); return false;">Click to end the game.</a>

</body>
</html>var dicefaces = new Array;
dicefaces[0] = new Image(50,50);
dicefaces[0].src = "images/d1.gif";
dicefaces[1] = new Image(50,50);
dicefaces[1].src = "images/d2.gif";
dicefaces[2] = new Image(50,50);
dicefaces[2].src = "images/d3.gif";
dicefaces[3] = new Image(50,50);
dicefaces[3].src = "images/d4.gif";
dicefaces[4] = new Image(50,50);
dicefaces[4].src = "images/d5.gif";
dicefaces[5] = new Image(50,50);
dicefaces[5].src = "images/d6.gif";

var playerturn;
playerturn = true;
var pointvalue;
document.game.point.value = pointvalue;

/////////////////////////////////

function throwdice() {
var genvalue;
var randomdice1;
var randomdice2;
var sum;
var playerwin;
var playerloss;
genvalue=Math.floor(Math.random()*6);
randomdice1 = genvalue+1;
document.dieimage1.src=dicefaces[genvalue].src;
genvalue=Math.floor(Math.random()*6);
randomdice2 = genvalue+1;
document.dieimage2.src=dicefaces[genvalue].src;
sum = randomdice1 + randomdice2;

if (playerturn) {
document.game.point.value = "";
switch (sum) {
case 7:
case 11:
document.game.status.value = "You win! Throw Again!";
break;
case 2:
case 3:
case 12:
document.game.status.value = "You lose. Throw Again!";
break;
default:
playerturn = false;
pointvalue = sum;
document.game.point.value = pointvalue;
document.game.status.value ="Throw again, hit your point & win.";
}
}
else {
switch (sum) {
case 7:
document.game.status.value = "You lose. Throw Again!";
playerturn = true;
break;
case pointvalue:
document.game.status.value = "You win. Throw Again!";
playerturn = true;
}
}
}

//set timeout function
function timer() {
setTimeout(function() { throwdice() }, 1000);
}

function endgame(){

}

</script>

<body>

<img src="images/d1.gif" name="dieimage1" id="mydice">
<img src="images/d1.gif" name="dieimage2" id="mydice">
<br>
<a href="" onClick="timer(); return false;">Click to throw the dice.</a>

<form name="game">
Status <input name="status" value="" size=35>
<br/>
Point value <input name="point" value="" size=4>
<br/>
Win total <input name="win" value="" size=4>
Loss total <input name="loss" value="" size=4>
</form>

<a href="" onClick="endgame(); return false;">Click to end the game.</a>

</body>
</html>

• The piece of code that needs updating is:

Code:
```default:
playerturn = false;
pointvalue = sum;
document.game.point.value = pointvalue;
document.game.status.value ="Throw again, hit your point & win.";```
You need to add a test to the start of that code to see if they made their point. If they did then they win. If they rolled 7 then they lose.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•