6 Replies - 5314 Views - Last Post: 11 June 2012 - 01:18 PM Rate Topic: -----

#1 commonSense   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-June 12

Comparing three variables

Posted 11 June 2012 - 10:01 AM

Hi, I am having trouble with a dice game attempting to check for 3 of a kind. I tried checking for two of a kind and that worked fine without any trouble.
//the dice have already been rolled and the values displayed, now I want to check for doubles and triples

if (die1 == die2 || die1 == die3 || die1 == die4 || die1 == die5 || die2 == die3 || die2 == die4 || die2 == die5 || die3 == die4 || die3 == die5 || die4 == die5) 
		{
			System.out.println("You rolled doubles!");
//this first if statement worked fine.
		}	
		
//checking for three of a kind did not work..		
else if ((die1 == die2 && == die3) || (die1 == die3 && == die4) || (die1 == die3 && == die5) || (die2 == die3 && == die4) || (die2 == die3 && == die5) || (die3 == die4 && == die5))
		{
			System.out.println("You rolled triples!");
		}	



Later, I will also have to check for four of a kind but I do not know how to approach finding triples or quadruples. Does anyone have any ideas?

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Comparing three variables

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11325
  • View blog
  • Posts: 19,369
  • Joined: 19-March 11

Re: Comparing three variables

Posted 11 June 2012 - 10:27 AM

This is what we'd call a combinatorial explosion. It'll be a lot easier if you put the dice into an iterable structure - an array or a list, probably.

To just check for three of a kind, here's one way to do it:

private boolean threeOfAKind(int[] dice)
{
for (int i =0; i < dice.length -2; i++) 
  {
    int count = 1; // there's one of a kind to start with
    for (int j = i+1; j < dice.length; j ++)
    {
      if (dice[i] == dice[j])
        count ++;
      if (count >=3) return true;
    }
  }
  return false;
}



Here's another way to do it:

private static boolean threeOfAKind(int[] dice)
{

   Arrays.sort(dice);
  for (int i =0; i < dice.length -2; i++) 
  {
    if (dice[i]== dice[i+1] && dice[i]==dice[i+2])
      return true;
  }
  return false;
}


Now, as an exercise for you, see if you can modify one of these to check for the maximum "ofAKindness" of an array of ints. That is, can you return an int to tell you the greatest number of repeated ints?
Was This Post Helpful? 1
  • +
  • -

#3 commonSense   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-June 12

Re: Comparing three variables

Posted 11 June 2012 - 11:11 AM

View Postjon.kiparsky, on 11 June 2012 - 10:27 AM, said:

This is what we'd call a combinatorial explosion. It'll be a lot easier if you put the dice into an iterable structure - an array or a list, probably.

To just check for three of a kind, here's one way to do it:

private boolean threeOfAKind(int[] dice)
{
for (int i =0; i < dice.length -2; i++) 
  {
    int count = 1; // there's one of a kind to start with
    for (int j = i+1; j < dice.length; j ++)
    {
      if (dice[i] == dice[j])
        count ++;
      if (count >=3) return true;
    }
  }
  return false;
}


Thank you so much for replying. I understand how this code segment works and it is a lot neater than I had before. I was just wondering how I would implement something like this into my program. I tried placing it in after the dice are rolled and the value is shown but, I got an error "illegal start of expression". Thanks again for your help and do you have any idea why I'm getting that error?

This is how my program looks with your piece of code in it.
/**
 * @(#)diceroller.java
 *
 * diceroller application
 *
 * @author 
 * @version 1.00 2012/6/12
 */
 
//Imports 
import java.util.Random; 
import java.util.Scanner; 
import static java.lang.System.out; 

public class diceroller 
{ 

    public static void main (String[] args) 
    { 
        //Declarations 
        int die1 = 0; 
        int die2 = 0; 
        int die3 = 0; 
        int die4 = 0;
        int die5 = 0;
        int mynumber = 0; 
        int timesrolled = 0; 
        Random randomizer = new Random (); 
        Scanner s = new Scanner (System.in); 
        //Begin Main Code 
        
     
       //roll the five dice
        
            die1 = randomizer.nextInt (6) + 1; 
            die2 = randomizer.nextInt (6) + 1; 
            die3 = randomizer.nextInt (6) + 1;
            die4 = randomizer.nextInt (6) + 1; 
            die5 = randomizer.nextInt (6) + 1;  
            	
            		
			System.out.println("Player 1, you roll the dice");
			System.out.println("");
			System.out.println("Press enter to see results ...");
			System.out.println("");
			System.out.println("you rolled a "+die1+", a "+die2+", a "+die3+", a "+die4+" and a "+die5+"");
	
	

			private boolean threeOfAKind(int[] dice)
			{
			for (int i =0; i < dice.length -2; i++)
			  {
			    int count = 1; // there's one of a kind to start with
			    for (int j = i+1; j < dice.length; j ++)
			    {
			      if (dice[i] == dice[j])
			        count ++;
			      if (count >=3) return true;
			    }
			  }
			  return false;
			}


Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11325
  • View blog
  • Posts: 19,369
  • Joined: 19-March 11

Re: Comparing three variables

Posted 11 June 2012 - 11:27 AM

You're declaring a new method. You need to close out the old one first - if you haven't written methods beyond main, check the Sun tutorial. Read the whole trail for best effects - there's stuff you won't get right away, but it's worth having a look down the road even if you haven't got there yet.

You'll also have an issue with calling a non-static method from the static main method.

private static boolean threeOfAKind(int[] dice)


That's another issue entirely. For now, just declare this one as static. Not a good practice in the long run, but you can fix it later. One thing at a time.

Then once you get it to compile, you'll have to call the method.

Something like

if (threeOfAKind(dice))
  System.out.println("You rolled three of a kind");



should do the work.
Was This Post Helpful? 1
  • +
  • -

#5 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Comparing three variables

Posted 11 June 2012 - 11:42 AM

This guy had the same problem as you :)
Click me
Was This Post Helpful? 0
  • +
  • -

#6 commonSense   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-June 12

Re: Comparing three variables

Posted 11 June 2012 - 01:13 PM

You've been helping me a lot and the quick replies are great! I tried reading the method turorials and they seem excellent but I didn't understand it so well. Also, I took the ThreeOfAKind code out of the main method and it worked fine. But when I tried to call back the information in an if statement, I got an error saying that java couldn't find the dice symbol. Should I declare "dice"? I'm not sure what to do. I know you've helped a lot but I feel like I need some more haha.
public class diceroller 
{ 
private boolean threeOfAKind(int[] dice)
			{
			for (int i =0; i < dice.length -2; i++)
			  {
			    int count = 1; // there's one of a kind to start with
			    for (int j = i+1; j < dice.length; j ++)
			    {
			      if (dice[i] == dice[j])
			        count ++;
			      if (count >=3) return true;
			    }
			  }
			  return false;
			}

    public static void main (String[] args) 
    { 
        //Declarations 
        int die1 = 0; 
        int die2 = 0; 
        int die3 = 0; 
        int die4 = 0;
        int die5 = 0;
        int mynumber = 0; 
        int timesrolled = 0; 
        Random randomizer = new Random (); 
        Scanner s = new Scanner (System.in); 
        //Begin Main Code 
        
     
       //roll the five dice
        
            die1 = randomizer.nextInt (6) + 1; 
            die2 = randomizer.nextInt (6) + 1; 
            die3 = randomizer.nextInt (6) + 1;
            die4 = randomizer.nextInt (6) + 1; 
            die5 = randomizer.nextInt (6) + 1;  
            	
            		
			System.out.println("Player 1, you roll the dice");
			System.out.println("");
			System.out.println("Press enter to see results ...");
			System.out.println("");
			System.out.println("you rolled a "+die1+", a "+die2+", a "+die3+", a "+die4+" and a "+die5+"");
			
			if (threeOfAKind(dice))
  System.out.println("You rolled three of a kind");
//That is where I had the error. It said it couldn't find the symbol.. 


Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11325
  • View blog
  • Posts: 19,369
  • Joined: 19-March 11

Re: Comparing three variables

Posted 11 June 2012 - 01:18 PM

Well, that's where you'd need to make an array of ints to pass in there. So instead of a bunch of named variables "die2", "die6", "die75", and so forth, you'd have an array of dice, which the machine knows are related. I'll let you figure out how that's done. Sun/Oracle has another tutorial on arrays, which will probably be useful.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1