![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2005
Location: Australia
Posts: 5
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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
__________________
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2005
Location: Australia
Posts: 5
Rep Power: 0
![]() |
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 :: |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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
__________________
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 |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
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...
__________________
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|