# Dice Game Program toString() method help

• January 16th, 2013, 11:51 PM
JonSnow
Dice Game Program toString() method help
I was working on this program based off of some college CS courses assignments just to learn and I have it all working except one thing I can't figure out. The assignment says to create a toString() method in the Die class that will return the numbers that came up when the dice was rolled twice. I know quite a bit of java and this is just bugging me that I don't know how to properly do this. Please help me out.

For example, the output should look like: "You rolled a six and a four".

Die class:

Code :

```public class Die {   private int die; Random dice;   public Die() {   this.die = 0;   dice = new Random();   }   public int roll() {   die = dice.nextInt(6) + 1;   return die;   }   public boolean equals(int die, int die2) {   if (die == die2) { return true; } else { return false; }   }     }```

Main Method:

Code :

```public class DoublesGame {   public static void main(String[] args) {   double value = 10.00; double bet = 0; Currency usd = Currency.getInstance("USD"); NumberFormat format = NumberFormat.getCurrencyInstance(java.util.Locale.US); format.setCurrency(usd);   Die obj = new Die(); Scanner ui = new Scanner(System.in);   while (value > 0) {   System.out.println("You have " + format.format(value) + " " + usd.getCurrencyCode()); System.out.println("How much would you like to bet? "); bet = ui.nextDouble();     if (obj.equals(obj.roll(), obj.roll()) == true) { System.out.println("You win " + bet * 2); value += bet*2; } else { System.out.println("You lose " + bet); value -= bet; }   }   System.out.println("Better Luck Next Time, Jack!");       } }```
• January 17th, 2013, 12:25 AM
Zaphod_b
Re: Dice Game Program toString() method help
Quote:

Originally Posted by JonSnow
... toString() method in the Die class that will return the numbers that came up when the dice was rolled twice....

I'm not sure what kind of dice game you have created that is based on tossing a single die twice and paying off 2-to-1 if you get a "doubles" (first toss equal to second toss). I would like to be the House for this game (since the odds are 6-to-1 against the player and the payoff is only 2-to-1).

Anyhow...

Your Die class consists of a single die, not two. If you want a toString() method for the class that reports results for two tosses, there must be a way (in the class) to keep track of two tosses.

Philosophically, I dislike having a class named "Die" that has two "die" members (since the plural of die is dice), but for purposes of building on what you already have, maybe you could have two members, die1 and die2.

(Or maybe you could create a new class named Dice that has two Die members that you have already defined.)

Either way, maybe you could have a "roll2" method that gets two random numbers: One sets the value for die1 and the other for die2. This method could return the sum of the two dice values so that the game can be played.

Then the toString() method could create an output String based on the values of die1 and die2.

Then read up on how craps is played and flesh out your game to take into account snake-eyes, boxcars, craps, etc. Or you could come by my place and I'll show you how.. We can use my dice.

Bring cash. No checks or credit cards, please.

Cheers!

Z
• January 17th, 2013, 12:31 AM
JonSnow
Re: Dice Game Program toString() method help
@Zaphod-

I could not agree more this "game" is terrible as far as an actual game goes. I was following an assignment for a CS class CS 110: Program 7 Assignment for Programming Fundamentals 1 and there is the link if you don't believe me..lol And I thought of creating two roll methods, but I wanted to follow the assignment word for word. It just intrigued me that the professor thought it was possible to print " You have rolled a six and four" with the current design the way it is. Thanks for the good advice on craps, I have no doubt you are right.
• January 17th, 2013, 02:52 AM
Starstreak
Re: Dice Game Program toString() method help
Well the simplest way would be to have a switch/case statement for each number (1-6). If the die is a 6, then it assigns the string "six" to an output variable.

To avoid confusion, I wouldn't call the method toString(), since this is already a Java method in the API.
• January 17th, 2013, 02:59 PM
Zaphod_b
Re: Dice Game Program toString() method help
Quote:

Originally Posted by JonSnow
... It just intrigued me that the professor thought it was possible to print " You have rolled a six and four"

But the toString() method in the assignment just returns the String "one", "two", "three", "four", "five", or "six" depending on the integer value of the die 1, 2, 3 4, 5, 6. It does not keep track of two tosses of the die and report both values.

Why not just make an array of Strings containing {"one", "two",...,"six"} and have the toString() method return one of those elements depending on the value of the die?

It seems to me (because of the requirement of the equals() method) that your game will have two different instances of Die objects. In fact, since you have now posted a link to the assignment, I see the following:
Quote:

"The DoublesGame main method uses two Die objects and uses the roll, equals, and toString methods correctly."
Toss them both using each object's roll() method. Craft a message using the toString() method of the two Die objects, and then compare the objects for equality to determine whether this play is a winner.

Bottom line: Read the entire assignment before starting to write code. Make sure you understand the assignment. Use any helpful hints that were given.

Post-bottom-line comment (if that's allowed):
The reason that I jumped on craps as the game is that whenever anyone says "dice game" to me, I automatically think "Craps! Let's play!" The Doubles game of your assignment would be a fair game if the payoff were 6-to-1 rather than 2-to-1. You could probably make a lot of money on the Street by offering 4-to-1 or 5-to 1-odds. (Assuming, of course, that the other players were idiots. That's what most street games and all casino games assume: The players are idiots.)

I am always disappointed when educators create assignments like this that could actually be educational (by making it a fair game and maybe even explaining why the odds are what they are) but come up with an exercise in programming that has no real pedagogical payoff beyond the programming language aspects.

Cheers!

Z