Jump to content

Is there an easier way to program a 3 dice game in Java?

- - - - -

  • Please log in to reply
10 replies to this topic

#1
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Best CodeCallers,


I'm Currently making a small dice game.
It's for homework purposes and I'm not really stuck, just wondering if there is an easier way to do it.

I've created a roll() method to return a Random 1-6 number:
//Dice method to create new Random numbers
public int roll() 
{
   int diceThrow = (int)(6*Math.random() + 1);   // Range 1-6
   return diceThrow;
}

Now when a player throws 1-5 He will receive 10 points and with 3 6's he'll get 100 points.

I've created the 3 6's as following:
if(dice1 == 6 && dice2 == 6 && dice3 == 6)
{
    //Give 100 points to user
}

I can do it the same way for 1-5 but is there an easier way, to equal 3 integers at once?

Edited by SterAllures, 18 October 2011 - 02:37 AM.
Problem Solved

4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
What if the player gets 3 5's? Does he get 100 points, or does this just pertain to 6's.

If so, then will this suffice?

if(dice1 == 6 && dice2 == 6 && dice3 == 6)

{

    //Give 100 points to user

} else { 

    //give 10 points to user

}


You could also write
 if ( (dice1==dice2) && (dice2==dice3) && (dice3==6) ) { ... } 


#3
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
When the player gets 3 6's he gets 100 points when he gets 3 1-5's he'll get 10 points.

I don't want to write:
[FONT=monospace]if(dice1 == 5 && dice2 == 5 && dice3 == 5)[/FONT]

for 1 till 5. Wondering is there is an easier way?
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I'm not understanding your logic.

Is 3 1's equal to 100 points also?
Is 3 2's equal to 100 points also?
...
is 3 5's equal to 100 points also?

From my understanding of what you have, only 3 6's are worth 100 points. Everything else is considered 10 points.
In that case this will work:
if(dice1 == 6 && dice2 == 6 && dice3 == 6)

{

    //Give 100 points to user

} else { // any other case

    //give 10 points to user

}

If it's not the case you could write:

boolean rewarded = false;

for(int i = 1; i <= 6 && !rewarded; ++i) {

    if( dice1 == dice2 && dice2 == dice3 && dice3 == i ){

          //reward 100 points

          rewarded = true;

    }

}

//reward 10 points if rewarded = false.



#5
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
The second one is exactly what I needed
I meant to do the following:
He will always have to throw 3 of the same dices.
When he throws 3 dices 1, 2, 3, 4, 5 he will get 10 points
and when he throws 3 6's he will get the jackpot and win 100 points.


I've now got the following:

             if(dice1 == 6 && dice2 == 6 && dice3 == 6)
             {
                //Award 100 points
                wonMoney += 100;
            }
            else
            {
                wonMoney -= 1;
                moneyLabel.setText(wonMoney+ " Euro");
                
            }
            for(int i = 1; i <= 5 && !rewarded; ++i) 
            {
                //If dices all equal i
                if( dice1 == dice2 && dice2 == dice3 && dice3 == i ){
                      
                      //reward 10 points
                      rewarded = true;
                      
                      wonMoney += 10;         
                }
            }

4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Here's a 1-liner. Can you figure it out? :)
int dice1=3, dice2=3, dice3=3;
if(dice1 == dice2 && dice2 == dice3){
    System.out.println("result: " + (10 + dice1/6*90));
}


#7
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts

wim DC said:

Here's a 1-liner. Can you figure it out? :)
int dice1=3, dice2=3, dice3=3;
if(dice1 == dice2 && dice2 == dice3){
    System.out.println("result: " + (10 + dice1/6*90));
}

I think I understand what you're doing.
You give the player points based on which number they threw.

The thing is. they have to get 10 points when all 3 dices are the same number, except when all the 3 dices are 6, then the player will get 100 points.

So with that logic.
I think the for loop gives the easiest answer, because I can check the numbers 1 till 5 in 1 for loop.

Thanks a lot for your help, always appreciated!
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

The thing is. they have to get 10 points when all 3 dices are the same number
That is the if

if(dice1 == dice2 && dice2 == dice3)

Quote

except when all the 3 dices are 6, then the player will get 100 points.
That's what the one-liner does :D

Put this in a main and run it:

for(int i=1 ; i<=6 ; i++){

            int dice1 = i, dice2 = i, dice3 = i;

            if (dice1 == dice2 && dice2 == dice3)

            {

                System.out.println("dices: " + dice1 + ", " + dice2 + ", " + dice3);

                System.out.println("result: " + (10 + dice1 / 6 * 90));

            }

        }



#9
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I guess im still trying to figure the logic but if all 3 dice match
And if they are less than 6 award 10 points then

if(dice1==dice2&&dice2==dice3&&dice3<6) 

    Award10points


Posted from iPhone.

#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
After the if-statement the 3 dices are equal, right?

Okay, then the scoring must become:
[TABLE]
[TR]
[TD]1[/TD]
[TD]=[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]=[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]=[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]=[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]=[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]=[/TD]
[TD]100[/TD]
[/TR]
[/TABLE]



Ok? Right!
That is the same as this, hm?
[TABLE]
[TR]
[TD]1[/TD]
[TD]=[/TD]
[TD]10 + 0[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]=[/TD]
[TD]10 + 0[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]=[/TD]
[TD]10 + 0[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]=[/TD]
[TD]10 + 0[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]=[/TD]
[TD]10 + 0[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]=[/TD]
[TD]10 + 90[/TD]
[/TR]
[/TABLE]



Still with me? :)
Okay, here goes the next step:
[TABLE]
[TR]
[TD]1[/TD]
[TD]=[/TD]
[TD]10 + (0*90)[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]=[/TD]
[TD]10 + (0*90)[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]=[/TD]
[TD]10 + (0*90)[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]=[/TD]
[TD]10 + (0*90)[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]=[/TD]
[TD]10 + (0*90)[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]=[/TD]
[TD]10 + (1*90)[/TD]
[/TR]
[/TABLE]



All still fine? Onwards!
[TABLE]
[TR]
[TD]1[/TD]
[TD]=[/TD]
[TD]10 + ((1/6)*90)[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]=[/TD]
[TD]10 + ((2/6)*90)[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]=[/TD]
[TD]10 + ((3/6)*90)[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]=[/TD]
[TD]10 + ((4/6)*90)[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]=[/TD]
[TD]10 + ((5/6)*90)[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]=[/TD]
[TD]10 + ((6/6)*90)[/TD]
[/TR]
[/TABLE]


And there we go. That's the formula I used. (you should know that in Java -- when dealing with integers -- decimals don't matter, thus 5/6 == 0)

Edited by wim DC, 17 October 2011 - 05:55 AM.
1 bracket too much


#11
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Wow, it works perfectly!

Don't fully understand the (10 + dice1 / 6 * 90) but I'll try to figure that out when I get back from work ;).
Really nice statement!!

I've now got the following:

if (dice1 == dice2 && dice2 == dice3)
                {
                    wonMoney += (10 + dice1 / 6 * 90);
                    mainText.setText("GEWONNEN! U wint");
                    moneyLabel.setText(wonMoney+ " Euro");
                    
                    System.out.println("DICES: " + dice1 + ", " + dice2 + ", "
                            + dice3);
                    System.out.println("GEWONNEN: " + (10 + dice1 / 6 * 90));
                }
                else if( dice1 == dice2 ||
                         dice1 == dice3 ||
                         dice2 == dice3)
                {
                    wonMoney +=1;
                    mainText.setText("GEWONNEN! U wint 1 Euro");
                    moneyLabel.setText(wonMoney+ " Euro");
                    System.out.println("GEWONNEN 1 Euro");
                }
                else
                {
                    wonMoney -= 1;
                }


---------- Post added at 03:26 PM ---------- Previous post was at 03:20 PM ----------

I just read your comment about the
(10 + dice1 / 6 * 90)

And I have to say that is some realy really smart thinking!

I would really never think of that, and I fully understand that now.

Thanks a lot for the formula! Have to remember that ;)!

(Sorry, Can't give you rep atm. gave you rep last time :) )
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users