-1

I have to create a yahtzee program using java.

In this program I must compare face values every time the dice are rolled. I have an ArrayList of 5 dices.

Is there any way to compare these die without extremely long if statements?

The if statements would have to compare 1 dice value to the other 4 then do that for the other 4 die as well. It will be very long and I'm sure it can be simplified but I don't konw how. Any help is appreciated with this issue. This is the dice Class

public class Die
{
   private final int MAX = 6;  // maximum face value

   private int faceValue;  // current value showing on the die

   //-----------------------------------------------------------------
   //  Constructor: Sets the initial face value.
   //-----------------------------------------------------------------
   public Die()
   {
      faceValue = 1;
   }

   //-----------------------------------------------------------------
   //  Rolls the die and returns the result.
   //-----------------------------------------------------------------
   public int roll()
   {
      faceValue = (int)(Math.random() * MAX) + 1;

      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Face value mutator.
   //-----------------------------------------------------------------
   public void setFaceValue(int value)
   {
      faceValue = value;
   }

   //-----------------------------------------------------------------
   //  Face value accessor.
   //-----------------------------------------------------------------
   public int getVal()
   {
      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Returns a string representation of this die.
   //-----------------------------------------------------------------
   public String toString()
   {
      String result = Integer.toString(faceValue);

      return result;
   }
}

I fixed my java code to include collections.sort

import java.util.*;

public class Yahtzee 
{
    public static void manin(String args[])
    {

        Die die1 = new Die();
        Die die2 = new Die();
        Die die3 = new Die();
        Die die4 = new Die();
        Die die5 = new Die();

        Die placeholder = new Die();

        int timeRolled = 0;


        String myString;

        Scanner scan = new Scanner(System.in);

        System.out.println("Please press 'y' to play and 'n' to quit");

        myString = scan.next();

        if(myString == "y")
        {   
            ArrayList<Integer> yahtzee = new ArrayList<>(5);

            die1.roll();
            die2.roll();
            die3.roll();
            die4.roll();
            die5.roll();

            yahtzee.add(die1.getVal());
            yahtzee.add(die2.getVal());
            yahtzee.add(die3.getVal());
            yahtzee.add(die4.getVal());
            yahtzee.add(die5.getVal());

            Collections.sort(yahtzee);





        }






    }
}

How would I know compare these values to one another?

Todd
  • 129
  • 1
  • 1
  • 9
  • Describe the very long if statement you have in mind. There will be some that are rather complicated but maybe yours can be improved. – zapl Nov 28 '15 at 03:03
  • 4
    Well, this `if(myString == "y")` is not very long, but very wrong :P ... [How do I compare strings in Java?](http://stackoverflow.com/q/513832) – Tom Nov 28 '15 at 03:04
  • That is statement is for controlling the game. – Todd Nov 28 '15 at 03:05

1 Answers1

1

If you are dealing with numbers, why not use int[] instead of whole ArrayList?

 int[] yahtzee = new int[5];
 yahtzee[0] = (die1.roll());
 yahtzee[1] = (die2.roll());
 yahtzee[2] = (die3.roll());
 yahtzee[3] = (die4.roll());
 yahtzee[4] = (die5.roll());
 Arrays.sort(yahtzee);

This will arrange your array in increasing order. First element = smallest, last = largest.

You wont have to manually use any if statement at all and code is more optimized and cleaner as well.

EDIT:

And as pointed in comments, instead of if(myString == "y") you should be using if(myString.equals("y")

Jaskaranbir Singh
  • 2,034
  • 3
  • 17
  • 33
  • 1
    I can't .add to an array. Only an array list. – Todd Nov 28 '15 at 03:05
  • I'm using the face values of the 5 die. So could I set the 5 face values to objects and then use those with arrays.sort? – Todd Nov 28 '15 at 03:07
  • 1
    Or just use `Collections.sort()` on the `List`. Also, while sorting is not a bad idea, even once the values are sorted, how do you expect to check for the various hands without conditional statements? – azurefrog Nov 28 '15 at 03:10
  • 2
    @Todd You can create an array of 5 random values in 1 line even http://ideone.com/w0x9Yu – zapl Nov 28 '15 at 03:11
  • @azurefrog I figured I'd still have to conditionals to compare the values but as I'm using an arraylist can I used for loops of some sort or must I use if statements? – Todd Nov 28 '15 at 03:16