# Thread: [RESOLVED] Javascript logic question

1. Registered User
Join Date
Mar 2014
Posts
2

## [RESOLVED] 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>

2. I don't think I understand your third rule but in any case I think if/else statements might be the better way to go here. I only say that because with a case/switch you'll have a case for each dice total (and a default), and with if/else statements you'll only need 3 or 4 logical statements to achieve your game rules. I can't say that one is faster than the other in terms of processing but if we are being honest here there is very little chance it will have any impact on the performance of your game. Frankly I just feel it gives you fewer lines as you can group the conditions.

It looks like you should add a global array that holds the dice totals (since rule 3 seems to apply to more than one roll). Other than that you just seem to need to add up the dice rolls (adding +1 to each since you are using the dice rolls as array indexes) and then running them through a set of if/else statements. For functionality you would probably set it up similar to:
Code:
```if(some_condition_here) {
// Stuff
} else if(the_second_condition) {
// More stuff
} else if(the_third_condition) {
// Even more stuff
} else {
// Catch any weird cases that weren't in your rules?
}```

3. Registered User
Join Date
Mar 2014
Posts
2
Thanks! I have been working on the logic and it's working well so far, I just got stuck on how to count the wins & losses, appreciate any feedback.
Code:
```<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Craps Game</title>
<script>

//Name: Michael Manning

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>```

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

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

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.14689 seconds
• Memory Usage 2,870KB
• Queries Executed 15 (?)
Template Usage (33):
• (2)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (3)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (3)postbit
• (3)postbit_onlinestatus
• (3)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (72):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_postinfo_query
• fetch_postinfo
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete