Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 28th, 2005, 7:07 AM   #1
Elvellon
Newbie
 
Elvellon's Avatar
 
Join Date: Nov 2005
Location: Australia
Posts: 5
Rep Power: 0 Elvellon is on a distinguished road
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!

Last edited by Elvellon; Nov 28th, 2005 at 7:11 AM. Reason: Forgot to add something
Elvellon is offline   Reply With Quote
Old Nov 28th, 2005, 7:55 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 28th, 2005, 8:07 AM   #3
Elvellon
Newbie
 
Elvellon's Avatar
 
Join Date: Nov 2005
Location: Australia
Posts: 5
Rep Power: 0 Elvellon is on a distinguished road
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 ::
Elvellon is offline   Reply With Quote
Old Nov 28th, 2005, 9:00 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 29th, 2005, 6:28 PM   #5
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
      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...
__________________

tempest is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:48 AM.

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