2 Replies - 769 Views - Last Post: 26 October 2013 - 03:45 PM Rate Topic: -----

#1 NoobToob  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 28
  • Joined: 07-October 13

Can't figure out how to compare matches within array...

Posted 22 October 2013 - 05:22 PM

I have been trying to solve this one for a while now and am stuck... This question is a continuation of a previous question
Original Question

Question:
Previously, you created a Die application that randomly "throws" five dice for the computer and five for the player. Modify the application to decide the winner based on the following hierarchy of Die values. Any higher comnbination beats a lower one; for example, five of a kind beats four of a kind/
five of a kind
four of a kind
three " " "
pair

For this game dice values do not count, for example if both players have a three of a kind, its a tie, no matter what the values of the three dice are. Game does not recognize a full house. Save the application of FiveDice2.java.


I managed to get my program to array just fine while using the original Die class and have figured out how to use the arrays memory address to pull its data but I don't know how to compare to have the computer figure out the matches.

Die class
public class Die
{

private final int DIE_NUMBERS = 6;
private String name;
private int DieValue;



public Die(String name)
{
   this.name = name;

   DieValue = ((int)(Math.random() * 100) % DIE_NUMBERS + 1);
}

public Die()
{
}

public int getDieValue()
{
   return DieValue;
}

public void setDieValue(int Dienum)
{
   DieValue = Dienum;
}

public void display()
{
   System.out.println(name + " rolled a " + DieValue);  

}
}


FiveDice2 class
public class FiveDice2
{
   private static int total;

   public static void main(String[] args)
   {
   Die playerComputer = new Die();
   Die playerHuman = new Die();

   System.out.println("Computer rolls...");
   playerComputer = getData(playerComputer);

   System.out.println("Human rolls...");
   playerHuman = getData(playerHuman);

   System.out.println("Computer total is = " + playerComputer.getDieValue());
   System.out.println("Human total is = " + playerHuman.getDieValue());
   }

   public static Die getData(Die c)
   {
   final int TOTAL_THROWS = 5;
   int total = 0;
   int[] dieValue = new int[TOTAL_THROWS];
   String[] DiceNumber = {"Die 1", "Die 2", "Die 3", "Die 4", "Die 5"};
   Die[] DieThrown = new Die[TOTAL_THROWS];

   for(int x = 0; x < TOTAL_THROWS; ++x)
   {
   DieThrown[x] = new Die(DiceNumber[x]);
   DieThrown[x].display();
   dieValue[x] = DieThrown[x].getDieValue();
   total = total + dieValue[x];
   }

   methodGetsDiceArray(DieThrown);  

   c.setDieValue(total);
   return c;
   }
   
   public static void methodGetsDiceArray(Die[] diceArray)
   {
   Die[] PossibleDieValue = {1, 2, 3, 4, 5, 6};
   int match;
   
   for(int y = 0; y < 6; ++y)
   {
   for(int x = 0; x < diceArray.length; ++x)
    {
   if(PossibleDieValue[y] == diceArray[x])
   match = ++match;
    }
   }
   System.out.println("There are " + match + " matches");
   }
}


I was trying to figure out how to at least compare the array values to numbers between 1 and 6 to scan for matches but Die class numbers won't match with int's and I can't input integers in a Die array...
last time i tried to compile with this I got this

FiveDice2.java:48: error: incompatible types
Die[] PossibleDieValue = {1, 2, 3, 4, 5, 6};
...


Any tips?
All help as always is greatly appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Can't figure out how to compare matches within array...

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,996
  • Joined: 20-September 08

Re: Can't figure out how to compare matches within array...

Posted 23 October 2013 - 03:27 PM

Here, together with a slightly different Die class (correspond in numbering to a real one) is an approach you can take. It shows five throws by one player:

public class Die {
    private final int DIE_NUMBERS = 6;
    private String name;
    private int value;

    public Die(String name) {
        this();
        this.name = name;
    }

    public Die() {
        value = (int) (Math.random() * DIE_NUMBERS) + 1;
    }

    public int getValue() {
        return value;
    }

    public int roll() {
        return (value = (int) (Math.random() * DIE_NUMBERS) + 1);
    }

    public void display() {
        System.out.println(name + " rolled a " + value);
    }

    @Override
    public String toString() {
        return Integer.toString(value);
    }

    public static void main(String[] args) {
        Die d = new Die();
        int[] rolls = new int[7];

        for (int i = 0; i < 5; i++) {
            rolls[d.roll()]++;
        }

        // Print analysis
        for (int i = 1; i < rolls.length; i++) {
            System.out.printf("Number of %ds rolled: %d%n", i, rolls[i]);
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#3 NoobToob  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 28
  • Joined: 07-October 13

Re: Can't figure out how to compare matches within array...

Posted 26 October 2013 - 03:45 PM

View Postg00se, on 23 October 2013 - 03:27 PM, said:

Here, together with a slightly different Die class (correspond in numbering to a real one) is an approach you can take. It shows five throws by one player:

public class Die {
    private final int DIE_NUMBERS = 6;
    private String name;
    private int value;

    public Die(String name) {
        this();
        this.name = name;
    }

    public Die() {
        value = (int) (Math.random() * DIE_NUMBERS) + 1;
    }

    public int getValue() {
        return value;
    }

    public int roll() {
        return (value = (int) (Math.random() * DIE_NUMBERS) + 1);
    }

    public void display() {
        System.out.println(name + " rolled a " + value);
    }

    @Override
    public String toString() {
        return Integer.toString(value);
    }

    public static void main(String[] args) {
        Die d = new Die();
        int[] rolls = new int[7];

        for (int i = 0; i < 5; i++) {
            rolls[d.roll()]++;
        }

        // Print analysis
        for (int i = 1; i < rolls.length; i++) {
            System.out.printf("Number of %ds rolled: %d%n", i, rolls[i]);
        }
    }
}



Alright thanks a lot for the feedback, I unfortunately had to skip the exercise because I was falling behind and had to progress to the next chapter. I will go back to it to finish it up since I now think I know exactly what to do to fix my problem.
I realise that the sorting issue can easily be fixed by simply using a sort method to compare each value in the array with the next to determine matches. This should eliminate the issue of data type compatibilities, why they only showed me how to do it in the next chapter is a mystery to me... maybe there's another way to they expected me to do it, oh well.
I'll update once I have had the time to go back and finish this exercise. May be another week before that happens though... extremely tight on time until then.
Once again thank you so much, the help is greatly appreciated!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1