0

I want to create a poker dice game in java, and when the player rolls the dice, I want the program to tell the results and the current score. However, something is wrong. It isn't giving me properly the score. For example, I changed the the Math Random algorithm to always give me always (1,1,1,1,1), so the result would be 50. Unfortunately,it is giving me 0. Can I have some help please? Thanks.

This is my code:

public class DiceGame {

    public static int [] rollDice() {
        int [] diceSide = new int[5];
        Random diceRoller = new Random();
         for (int i = 0; i<diceSide.length; i++) {
            int roll = diceRoller.nextInt(1) + 1;
            diceSide[i] = roll;
    }
         System.out.print(diceSide[0] + "" + diceSide[1] + "" + diceSide[2] + "" + diceSide[3] + "" + diceSide[4]);
         return diceSide; 
    }

    public static int getResult(int[] dice) {

        int resValue = 0;

        for (int i = 0; i < dice.length; i++) {
            if (dice[i] == 5) {
                resValue = 50;

            } else if (dice[i] == 4) {
                resValue = 40;          

            } else if (dice[i] == 3) {
                resValue = 30;     

            }
        }
        System.out.print(resValue); 
        return resValue;
    }


    public static void main(String[] args) {

        int player1=0;
        int player2;
        int player3;
        int player4;
        int player5;

        player1 += getResult(rollDice());
    }
}
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
Ana Grou
  • 19
  • 1
  • 9
  • 2
    in don´t really get the problem you have, or rather what you want to achieve. it can only return 0, since every instance in your dice array equals 1. This leads to the point that every dice instance is not equal to 5, 4 and 3. Due to this resValue wont have any other value then 0 before returning – SomeJavaGuy Feb 25 '15 at 14:19
  • @erhun i guess she only means the System.out.print(resValue) part, but i am not 100% sure. – SomeJavaGuy Feb 25 '15 at 14:24
  • I'm really curious: what is a poker dice ? – Radu Murzea Feb 25 '15 at 19:24

3 Answers3

0

Math.Random returns a value thats between 0 and 1. You have to multiply this value with the Max-Value you want to recieve - 1 and add +1 to the whole thing. Don't forget to cast this whole thing to (int). It should look something like this:

(int) (Math.Random(5) + 1)

This way, you're getting values between 1 and 6.

Officer Bacon
  • 724
  • 1
  • 7
  • 22
0

In rollDice you take a value from java.util.Random. However, the upper limit is excluded, so you always end up with one by doing diceRoller.nextInt(1) + 1.

An array of integers is good for representing multiple dice, so if that's what you want, you could use diceRoller.nextInt(6) + 1 to set the value of each dice. If you want a single dice, you only need a single integer variable.

tsnorri
  • 1,966
  • 5
  • 21
  • 29
0

You are doing something different. You are creating random numbers from 0 to the max value of an integer.

diceRoller.nextInt(1)

This create a random number from 0 to 1. So it's rare you get 5 times 1. If you wanted to mock it, you should have put one as an assignment

Then, because this piece of code:

    for (int i = 0; i < dice.length; i++) {
        if (dice[i] == 5) {
            resValue = 50;

        } else if (dice[i] == 4) {
            resValue = 40;          

        } else if (dice[i] == 3) {
            resValue = 30;     

        }
    }

Means that if the last of the dice[i] values are 3, 4 or 5, it gets a new value. What I think you were trying to do is this (adding ten times the value of the dice for the whole array):

    int resValue = 0;

    for (int i = 0; i < dice.length; i++) {
        resValue += dice[i] * 10;
    }

In the last case, if all the 5 direRoll results are 1, you would get 50.

Michael
  • 3,308
  • 5
  • 24
  • 36