Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   Testing outcomes in a dice game - help! (http://www.programmingforums.org/showthread.php?t=7241)

Elvellon Nov 28th, 2005 8:07 AM

Testing outcomes in a dice game - help!
 
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!

DaWei Nov 28th, 2005 8:55 AM

I didn't examine your code or look at your page. Have you considered things like,
:

  if (roll < 5)
      // Group a result
  else if (roll < 9)
      // Group b result
  else
      // Group c result
  // Take me home mama, I be's done

I left out the braces, but you get the drift.

Elvellon Nov 28th, 2005 9:07 AM

Thanks for replying. :)

Yeah, that's what I had in mind. But my problem is getting to that stage in the first place. I need to take the dice roll outcome and then test it against the user's prediction.

I just need a way to compare my variables "user_choice" and "dice_total". But if my users can choose 1-4, 5-8 and 9-12, how do I let my program know that a dice roll of 7 is equal to the user prediction of 5-8? Am I making sense? Lol

:: Pulls at hair ::

DaWei Nov 28th, 2005 10:00 AM

Just expand the comments, that was gonna be YOUR job!
:

  if (roll < 5)
      // Group A result
      if (userChoice == groupA) userWins = true;
      else userWins = false;

  else if (roll < 9)
      // Group B result
  else
      // Group C result
  // Take me home mama, I be's done

It's relatively simple logic that can be implemented in about lebenty-seven ways, including the use of "switch".

tempest Nov 29th, 2005 7:28 PM

:

      if (userChoice == groupA) userWins = true;
      else userWins = false;


.. or ..

:

    userWins = (userChoice == groupA);

Sorry for the rather useless post, just a simple comment on semantics... as Dawei mentioned, there's a million ways...


All times are GMT -5. The time now is 8:46 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC