Programming a game of Chance by Dice

Two classes that differ in win condition and number of dice

Page 1 of 1

12 Replies - 9016 Views - Last Post: 16 August 2011 - 04:04 AM Rate Topic: -----

#1 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Programming a game of Chance by Dice

Posted 17 October 2010 - 06:26 PM

Hello again, newbie has another question.

I have asked a question before back in Spring, and after looking back at those answers, I'd like to see if there are different answers. The topic pertains to two not-so-different classes, one called TwoDice.java and another called FiveDice.java. The goal of the TwoDice.java is simply for the program to "throw" two dice and return their values. Right now my code is this:

/* Alphonsus Delgra
	October 16, 2010
	Lab 4-2
	TwoDice.java - Randomly "throw" two dice
*/
 public class TwoDice
 {
 	private int oneDie;
	private int twoDie;
	private static final int LOWEST_DIE_VALUE = 1;
	private static final int HIGHEST_DIE_VALUE = 6;
	
	public int getDieValue()
	{
	oneDie = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
	return oneDie;
	}
	public int getDieValue2()
	{
	twoDie = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
	return twoDie;
	}
}



Right now, the code compiled just fine, but is missing the main method. I think that I should put the main method right above the first declaration for it to look like this:

/* Alphonsus Delgra
	October 16, 2010
	Lab 4-2
	TwoDice.java - Randomly "throw" two dice
*/
 public class TwoDice
 {
        public static void main(String[] args)  // <--right here?
 	private int oneDie;
	private int twoDie;
	private static final int LOWEST_DIE_VALUE = 1;
	private static final int HIGHEST_DIE_VALUE = 6;
	
	public int getDieValue()
	{
	oneDie = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
	return oneDie;
	}
	public int getDieValue2()
	{
	twoDie = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
	return twoDie;
	}
}



The other class is called FiveDice.java, where it's a little more complicated. It;s more of a game where the application that randomly "throws" five dice for the computer, and then five dice for the player. (the computer will not decide the winner, the player will determine the winner based on observation) is one of the hints they give. So to determine the winner, the computer or player must have the better combination of thrown dice. The tiers of winning depend on something along the lines of pairs such as:

Five of a kind
Four of a kind
Three of a kind
A pair

One of the things that it tells me is that I don't have to program yet the part where the application decides who's the winner. All it tells me is to show the thrown dice of the computer and the player, and the winner will be determined from observation just like the hint. Thanks for your time again.

Is This A Good Question/Topic? 0
  • +

Replies To: Programming a game of Chance by Dice

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Programming a game of Chance by Dice

Posted 17 October 2010 - 06:57 PM

Using Math.random() is always tricky with integer values
make it that way

// create a Random object shared by everybody in your class
Random ran = new Random();

....
int value = getRadomValue(HIGHEST_DIE_VALUE);

// returns for dice a value >= 1 and <= the parameter received
int getRandomValue(int max) {
   return ran.nextInt(max) + 1;
}


Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Programming a game of Chance by Dice

Posted 17 October 2010 - 06:58 PM

You put the main() method in the wrong spot. Also, why do you structure your methods like that? What happens if you make 50 dice roles? Make 50 methods? No, the best way to do it is to make a single method and use an array to store the results. Like this:


/* Alphonsus Delgra
	October 16, 2010
	Lab 4-2
	TwoDice.java - Randomly "throw" two dice
*/
 public class TwoDice
 {
 	private int oneDie;
	private int twoDie;
	private static final int LOWEST_DIE_VALUE = 1;
	private static final int HIGHEST_DIE_VALUE = 6;
	
	public int getDie()
	{
	    oneDie = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
	    return oneDie;
	}
  
        // This can go any PHYSICAL location in the class, but 
        // you cannot have a method inside of a method
        public static void main(String[] args) {
            // Make an object which which we work
            TwoDice roller = new TwoDice();

            final int NUMROLES = 2;
            int[] diceRoles = new int[NUMROLES];

            for (int i = 0; i < NUMROLES; i++) 
                diceRoles[i] = roller.getDie()
        }
}



If you change NUMROLES, you will change the number of roles you made. All you must do now is print the values.
Was This Post Helpful? 0
  • +
  • -

#4 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: Programming a game of Chance by Dice

Posted 17 October 2010 - 07:16 PM

Alright I can understand utilizing arrays, but I can't really go that far yet. My textbook (Java Programming 5th Edition by Joyce Farrell) covers Chapter 4 with just more object concepts. I'll get to arrays in the near future because I'm not feeling too comfortable with them right now. Just for now, I I'll stick with the long way. Last time I tried something outside of the specified chapter (I'm in an online class), my professor wasn't too happy with it.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Programming a game of Chance by Dice

Posted 17 October 2010 - 07:20 PM

Alright, well, then just use int variables instead if you can't use arrays. However, if this were an actual application, you would want to use arrays:



/* Alphonsus Delgra
	October 16, 2010
	Lab 4-2
	TwoDice.java - Randomly "throw" two dice
*/
 public class TwoDice
 {
 	private int oneDie;
	private int twoDie;
	private static final int LOWEST_DIE_VALUE = 1;
	private static final int HIGHEST_DIE_VALUE = 6;
	
	public int getDie()
	{
	    oneDie = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
	    return oneDie;
	}
  
        // This can go any PHYSICAL location in the class, but 
        // you cannot have a method inside of a method
        public static void main(String[] args) {
            // Make an object which which we work
            TwoDice roller = new TwoDice();

            int roll1 = roller.getDie();
            int roll2 = roller.getDie();
}


Was This Post Helpful? 0
  • +
  • -

#6 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: Programming a game of Chance by Dice

Posted 17 October 2010 - 07:32 PM

I got the int variables down, just need clarification on the names. The application turned out to be:

/* Alphonsus Delgra
	October 16, 2010
	Lab 4-2
	TwoDice.java - Randomly "throw" two dice
*/
 public class TwoDice
{
 	private int oneDie;
	private int twoDie;
	private static final int LOWEST_DIE_VALUE = 1;
	private static final int HIGHEST_DIE_VALUE = 6;
	
	public int getDieValue()
	{
	oneDie = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
	return oneDie;
	}
	public static void main(String[] args)
	{
	TwoDice roller = new TwoDice();
	
		int roll1 = roller.getDie();
		int roll2 = roller.getDie();
		System.out.println("Die 1 rolled a " + oneDie);
		System.out.println("Die 2 rolled a " + twoDie);
	}
}



Now the thing that's stumping me is the name change. Should the name dieOne or roll1 be the general name? I think that within a different method I can use the same name, but the errors are:
 ----jGRASP exec: javac -g H:\Java 2nd\TwoDice.java

TwoDice.java:22: cannot find symbol
symbol  : method getDie()
location: class TwoDice
		int roll1 = roller.getDie();
		                  ^
TwoDice.java:23: cannot find symbol
symbol  : method getDie()
location: class TwoDice
		int roll2 = roller.getDie();
		                  ^
TwoDice.java:24: non-static variable oneDie cannot be referenced from a static context
		System.out.println("Die 1 rolled a " + oneDie);
		                                       ^
TwoDice.java:25: non-static variable twoDie cannot be referenced from a static context
		System.out.println("Die 2 rolled a " + twoDie);
		                                       ^
4 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.



So I know that the non-static variables can't be referenced, but that's when I just go off on a tangent fixing the static part.
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Programming a game of Chance by Dice

Posted 17 October 2010 - 07:38 PM

I gave you the wrong method name. My bad. You have to do all your interaction through the TwoDice object that we called roller. Otherwise you will have to mess with static methods, which is a pain to new folks.

You need to name them correctly:

/* Alphonsus Delgra
	October 16, 2010
	Lab 4-2
	TwoDice.java - Randomly "throw" two dice
*/
 public class TwoDice
{
 	private int oneDie;
	private int twoDie;
	private static final int LOWEST_DIE_VALUE = 1;
	private static final int HIGHEST_DIE_VALUE = 6;
	
	public int getDieValue()
	{
	oneDie = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
	return oneDie;
	}
	public static void main(String[] args)
	{
	TwoDice roller = new TwoDice();
	          
                // Call the getDieValue() method of the roller object
		int roll1 = roller.getDieValue();
		int roll2 = roller.getDieValue();

                // No, use your variables that you JUST made.
		System.out.println("Die 1 rolled a " + roll1);
		System.out.println("Die 2 rolled a " + roll2);
	}
}



I would even go as far as to make it it's own method with no other variables like so:

/* Alphonsus Delgra
	October 16, 2010
	Lab 4-2
	TwoDice.java - Randomly "throw" two dice
*/
 public class TwoDice
{
	private static final int LOWEST_DIE_VALUE = 1;
	private static final int HIGHEST_DIE_VALUE = 6;
	
	public int getDieValue()
	{
	    int roll = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
	    return roll;
	}

	public static void main(String[] args)
	{
	TwoDice roller = new TwoDice();
	          
                // Call the getDieValue() method of the roller object
		int roll1 = roller.getDieValue();
		int roll2 = roller.getDieValue();

                // No, use your variables that you JUST made.
		System.out.println("Die 1 rolled a " + roll1);
		System.out.println("Die 2 rolled a " + roll2);
	}
}



That isolates the variable in the method so you don't get confused!
Was This Post Helpful? 1
  • +
  • -

#8 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: Programming a game of Chance by Dice

Posted 17 October 2010 - 07:57 PM

Alright that's done with. The only thing left is the FiveDice.java. Basically I have to make it a game between computer and player each rolling five dice. After that I just have to display the values. One way to approach it would be using the TwoDice.java, just add three more dices and then five for the 2nd particpant (player/computer).

This is the best I can do, even if it's completely novice work.

// Alphonsus Delgra
// October 16, 2010
// Lab 4-2
// Five Dice game

public class FiveDice
{
	private static final int LOWEST_DIE_VALUE = 1;
	private static final int HIGHEST_DIE_VALUE = 6;
	
	public int getDieValue()
	{
	int roll = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
	return roll;
	}
	public static void main(String[] args)
	{
	FiveDice roller = new FiveDice();
	
		int compRoll1 = roller.getDieValue();
		int compRoll2 = roller.getDieValue();
		int compRoll3 = roller.getDieValue();
		int compRoll4 = roller.getDieValue();
		int compRoll5 = roller.getDieValue();
		int compRoll6 = roller.getDieValue();
		int playRoll1 = roller.getDieValue();
		int playRoll2 = roller.getDieValue();
		int playRoll3 = roller.getDieValue();
		int playRoll4 = roller.getDieValue();
		int playRoll5 = roller.getDieValue();
		
		System.out.println("Results are:\n Computer");
		System.out.println("Die 1 rolled a " + compRoll1);
		System.out.println("Die 2 rolled a " + compRoll2);
		System.out.println("Die 3 rolled a " + compRoll3);
		System.out.println("Die 4 rolled a " + compRoll4);
		System.out.println("Die 5 rolled a " + compRoll5);
		System.out.println("Player:\nDie 1 rolled a " + playRoll1 + "\nDie 2 rolled a " + playRoll2 + "\nDie 3 rolled a " + playRoll3 + "\nDie 4 rolled a " + playRoll4 + "\nDie 5 rolled a " + playRoll5);

	}
}



I didn't try entering after every playRoll to keep everything in one column without spreading out because the errors always say that the player prints aren't a statement or whatever.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Programming a game of Chance by Dice

Posted 17 October 2010 - 08:00 PM

What horrible code. Never heard of arrays ?
Was This Post Helpful? 0
  • +
  • -

#10 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: Programming a game of Chance by Dice

Posted 17 October 2010 - 08:15 PM

I have heard of arrays yes, but I posted earlier that my class isn't on that chapter yet (I'm finishing up chapter 4 and arrays are in chapter 8). I got to stick with everything up to the current chapter. My professor doesn't like it when we go beyond whatever we're on right now.
Was This Post Helpful? 1
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Programming a game of Chance by Dice

Posted 17 October 2010 - 08:21 PM

Yep, that's about as good as it will do without arrays. Glad that my structure helped

@pbl :D

View PostGamingDoc, on 17 October 2010 - 09:16 PM, said:

Alright I can understand utilizing arrays, but I can't really go that far yet. My textbook (Java Programming 5th Edition by Joyce Farrell) covers Chapter 4 with just more object concepts. I'll get to arrays in the near future because I'm not feeling too comfortable with them right now. Just for now, I I'll stick with the long way. Last time I tried something outside of the specified chapter (I'm in an online class), my professor wasn't too happy with it.

Was This Post Helpful? 0
  • +
  • -

#12 cait  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-August 11

Re: Programming a game of Chance by Dice

Posted 15 August 2011 - 11:43 PM

View Postpbl, on 17 October 2010 - 08:00 PM, said:

What horrible code. Never heard of arrays ?


Interesting. I, too, am a newbie in Java. If I were to do this same problem with arrays, how would you go about doing so?
Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Programming a game of Chance by Dice

Posted 16 August 2011 - 04:04 AM

Instead of this:
int compRoll1 = roller.getDieValue();
		int compRoll2 = roller.getDieValue();
		int compRoll3 = roller.getDieValue();
		int compRoll4 = roller.getDieValue();
		int compRoll5 = roller.getDieValue();
		int compRoll6 = roller.getDieValue();



You would have this:
int[] compRolls = new int[6];
for (int i = 0; i < compRolls.length; i++) {
    compRolls[i] = roller.getDieValue();
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1