JavaScript Help

Testing outcomes in a dice game

Page 1 of 1

11 Replies - 1986 Views - Last Post: 29 November 2005 - 09:40 PM

#1 Elvellon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-November 05

JavaScript Help

Posted 28 November 2005 - 05:35 AM

Hello everyone!

I've been coding a dice game and have run into a bit of trouble. My dice game works like this:

1: First the user makes a deposit by clicking an image ($500, $600 etc)

2: The user then makes a bet (of either $100 or $200)

3: The user then makes a prediction on the outcome of the dice roll (1-4, 5-8, 9-12)

4: The user then clicks a button which "rolls" two dice.

5: The total of the two dice detemines whether they win or lose. Eg. If the dice roll total equals 5, and the user predicted an outcome of 5-8, they win.

I am stuck on no. 5 - I need to compare the prediction to the dice roll total to see if the user has won or lost. I am hopelessly stuck.

I was thinking of somehow making the prediction 1-4 equal 'a' and then the dice rolls 1, 2, 3 and 4 equal to 'a' as well so that I can test to see if they match. 5-8 would be 'b' and 9-12 would be 'c', the rolls of 5, 6, 7 and 8 would equal 'b' etc. But how can I do it?

Here's my code for the prediction bit:
<form name="choice">
<P><B>Pick your outcome:</B></P>
<input type="button" name="outcome1" value="1-4" onmousedown="no_choice()" onclick="user_choice = 1, document.chosen.userchoice.value='1-4', pick()">
<input type="button" name="outcome2" value="5-8" onmousedown="no_choice()" onclick="user_choice = 2, document.chosen.userchoice.value='5-8', pick()">
<input type="button" name="outcome3" value="9-12" onmousedown="no_choice()" onclick="user_choice = 3, document.chosen.userchoice.value='9-12', pick()">
</form>

<form name="chosen">
<input type="text" name="userchoice" value="" size="4">
</form>

<script language="Javascript">
var user_choice = 0
function pick(form) {
if (user_choice == 1) {
alert("You have chosen 1-4") }
else {
if (user_choice == 2) {
alert("You have chosen 5-8") }
else {
if (user_choice == 3) {
alert("You have chosen 9-12") }
}}}
</script>


And the script for my dice roll bit is:
<script language=Javascript>
function dice_roll() {
var randice = new Array
randice[0] = "images/1.gif"
randice[1] = "images/2.gif"
randice[2] = "images/3.gif"
randice[3] = "images/4.gif"
randice[4] = "images/5.gif"
randice[5] = "images/6.gif"
var ranimage=Math.round(Math.random()*5)
var ranimage2=Math.round(Math.random()*5)
document.randoimage2.src=randice[ranimage2]
document.randoimage1.src=randice[ranimage]

var dice_total = (Number(ranimage) + Number(ranimage2) + 2);
alert("Your roll = " + dice_total);
}
</script>


Can anyone lend a hand? I'm getting so frustrated! If you want to see my "almost working" prototype for the game click: HERE

Thank you!

This post has been edited by Elvellon: 28 November 2005 - 07:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: JavaScript Help

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: JavaScript Help

Posted 28 November 2005 - 07:51 AM

Send the value generated by the prediction to the dice_roll function:
function dice_roll(prediction) {
var randice = new Array
randice[0] = "images/1.gif"
randice[1] = "images/2.gif"
randice[2] = "images/3.gif"
randice[3] = "images/4.gif"
randice[4] = "images/5.gif"
randice[5] = "images/6.gif"
var ranimage=Math.round(Math.random()*5)
var ranimage2=Math.round(Math.random()*5)
document.randoimage2.src=randice[ranimage2]
document.randoimage1.src=randice[ranimage]

var dice_total = (Number(ranimage) + Number(ranimage2) + 2);
alert("Your roll = " + dice_total);
alert("Your prediction = " + prediction);
if((prediction==1 && dice_total<=4 && dice_total>=1) || (prediction==2 && dice_total<=5 && dice_total>=8) || (prediction==3 && dice_total<=12 && dice_total>=9))
{
   alert("Good Call!\n");
}
else
{
   alert("Prediction Incorrect");
}

}


Was This Post Helpful? 0
  • +
  • -

#3 Elvellon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-November 05

Re: JavaScript Help

Posted 28 November 2005 - 08:48 AM

Ooh, I see what you've done. Very clever!

Only it always tells me that my prediction is incorrect. Do I need to tell it that 1-4 equals 1, 5-8 equals 2 and 9-12 equals 3 somehow? It keeps telling me that "prediction" is undefined.

Sorry for these dumbo questions! And thank you for helping me out!
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: JavaScript Help

Posted 28 November 2005 - 09:00 AM

You'll have to pass the value of prediction to the function...where do you call the dice_roll function?

And no troubles about helping out...that's what we're here for! :)
Was This Post Helpful? 0
  • +
  • -

#5 Elvellon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-November 05

Re: JavaScript Help

Posted 28 November 2005 - 09:13 AM

I'm calling my dice_roll() function in my form "roll". So the user clicks the button and the dice is rolled.

<form name="roll">
<input type="button" value="Roll the Dice!" onclick="dice_roll()">
</form>


I'm also displaying their prediction in a text box (if it helps):

<form name="choice">
<P><B>Pick your outcome:</B></P>
<input type="button" name="outcome1" value="1-4" onclick="user_choice=1, document.chosen.userchoice.value='1-4'">
<input type="button" name="outcome2" value="5-8" onclick="user_choice=2, document.chosen.userchoice.value='5-8'">
<input type="button" name="outcome3" value="9-12" onclick="user_choice=3, document.chosen.userchoice.value='9-12'">
</form>

<form name="chosen">
<input type="text" name="userchoice" value="" size="4">
</form>


:: Lays out some cookies for you ::

I really appreciate the help!
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: JavaScript Help

Posted 28 November 2005 - 10:00 AM

Hmmm...try this:
function dice_roll() {
var prediction;
var randice = new Array
randice[0] = "images/1.gif"
randice[1] = "images/2.gif"
randice[2] = "images/3.gif"
randice[3] = "images/4.gif"
randice[4] = "images/5.gif"
randice[5] = "images/6.gif"
var ranimage=Math.round(Math.random()*5)
var ranimage2=Math.round(Math.random()*5)
document.randoimage2.src=randice[ranimage2]
document.randoimage1.src=randice[ranimage]
if(document.chosen.userchoice.value == "" || document.chosen.userchoice.value == null)
{
   prediction = 0;
}
else
{
   prediction = document.chosen.userchoice.value;
}
var dice_total = (Number(ranimage) + Number(ranimage2) + 2);
alert("Your roll = " + dice_total);
alert("Your prediction = " + prediction);
if((prediction==1 && dice_total<=4 && dice_total>=1) || (prediction==2 && dice_total<=5 && dice_total>=8) || (prediction==3 && dice_total<=12 && dice_total>=9))
{
  alert("Good Call!\n");
}
else
{
  alert("Prediction Incorrect");
}

}


Was This Post Helpful? 0
  • +
  • -

#7 Elvellon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-November 05

Re: JavaScript Help

Posted 28 November 2005 - 10:16 AM

Still telling me that I'm always wrong, but I no longer get the 'undefined' message

I understand your code though, which is unusual for me.

Here's everything I have on my page (might be better for testing purposes):
<form name="choice">
<P><B>Pick your outcome:</B></P>
<input type="button" name="outcome1" value="1-4" onclick="user_choice=1, document.chosen.userchoice.value='1'">
<input type="button" name="outcome2" value="5-8" onclick="user_choice=2, document.chosen.userchoice.value='2'">
<input type="button" name="outcome3" value="9-12" onclick="user_choice=3, document.chosen.userchoice.value='3'">
</form>

<form name="chosen">
<input type="text" name="userchoice" value="" size="4">
</form>

<script language="Javascript">
function dice_roll() {
var prediction;
var randice = new Array
randice[0] = "images/1.gif"
randice[1] = "images/2.gif"
randice[2] = "images/3.gif"
randice[3] = "images/4.gif"
randice[4] = "images/5.gif"
randice[5] = "images/6.gif"
var ranimage=Math.round(Math.random()*5)
var ranimage2=Math.round(Math.random()*5)
document.randoimage2.src=randice[ranimage2]
document.randoimage1.src=randice[ranimage]
if(document.chosen.userchoice.value == "" || document.chosen.userchoice.value == null)
{
  prediction = 0;
}
else
{
  prediction = document.chosen.userchoice.value;
}
var dice_total = (Number(ranimage) + Number(ranimage2) + 2);
alert("Your roll = " + dice_total);
alert("Your prediction = " + prediction);
if((prediction==1 && dice_total<=4 && dice_total>=1) || (prediction==2 && dice_total<=5 && dice_total>=8) || (prediction==3 && dice_total<=12 && dice_total>=9))
{
 alert("Good Call!\n");
}
else
{
 alert("Prediction Incorrect");
}

}
</script>

<form name="roll">
<input type="button" value="Roll the Dice!" onclick="dice_roll()">
</form>

<img src="images/1.gif" width=80 height=80 name="randoimage1">
<img src="images/1.gif" width=80 height=80 name="randoimage2">

Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: JavaScript Help

Posted 28 November 2005 - 10:28 AM

Whoops, my fault...a misplaced equality operator.
use this if statement
if((prediction==1 && dice_total<=4 && dice_total>=1) || (prediction==2 && dice_total>=5 && dice_total<=8) || (prediction==3 && dice_total<=12 && dice_total>=9))
{
alert("Good Call!\n");
}
else
{
alert("Prediction Incorrect");
}


BTW, have you been generating any numbers less than 5?
Was This Post Helpful? 0
  • +
  • -

#9 Elvellon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-November 05

Re: JavaScript Help

Posted 28 November 2005 - 10:33 AM

Oh my goodness! It's working. :D I'm so happy.

Thank you so much! You're amazing!

Yup, I've been getting dice rolls of less than 5. If that's what you mean.

Ugh, I have to get to bed, it's almost 4am. :) Thank you again for your help! I'll be back tomorrow with more questions for you! :D

This post has been edited by Elvellon: 28 November 2005 - 10:52 AM

Was This Post Helpful? 0
  • +
  • -

#10 Elvellon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-November 05

Re: JavaScript Help

Posted 28 November 2005 - 09:19 PM

I'm back again... with another question (like I promised, :P ).

Everything is working just swell (thanks to you, Amadeus) but I am now trying to work out how to reset some values.

Now that my dice game tells the user if they won or lost, I added some code so that their win/loss is added/subtracted from their total. This is working just fine. What I need to do now is reset their bet and their prediction so they can't make the same choices twice in a row.

I need to reset these two values:

<form name="bet">
<p> <B>Place your bet:</B><P>
<input type="button" name="bet100" value="100" onmousedown="no_bet()" onclick="user_bet=1, document.displaybet.currentbet.value=100, betting()">
<input type="button" name="bet200" value="200" onmousedown="no_bet()" onclick="user_bet=2, document.displaybet.currentbet.value=200, betting()">
</form>

<!-- This form is used to display the bet on the page. -->
<form name="displaybet">
<input type="text" name="currentbet" value="" size=3>
</form>


&

<form name="choice">
<P><B>Pick your outcome:</B></P>
<input type="button" name="outcome1" value="1-4" onmousedown="no_choice()" onclick="user_choice = 1, document.chosen.userchoice.value='1', pick()">
<input type="button" name="outcome2" value="5-8" onmousedown="no_choice()" onclick="user_choice = 2, document.chosen.userchoice.value='2', pick()">
<input type="button" name="outcome3" value="9-12" onmousedown="no_choice()" onclick="user_choice = 3, document.chosen.userchoice.value='3', pick()">
</form>


<!--This form is used to display the pick (prediction) on the page -->
<form name="chosen">
<input type="text" name="userchoice" value="" size="4">
</form>


Again, my page as it is now is here: Dice Game
Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: JavaScript Help

Posted 29 November 2005 - 07:24 AM

You can reset the values in the dice roll function, after you have used them. In that function, add this to the end, before the final closing brace:
document.displaybet.currentbet.value = "";
document.chosen.userchoice.value = "";


Was This Post Helpful? 0
  • +
  • -

#12 Elvellon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-November 05

Re: JavaScript Help

Posted 29 November 2005 - 09:40 PM

Oh thank you! :D I was putting it in the wrong place.

:: Stupid me ::

You really are a smart cookie. Thank you for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1