Hi there, i just joined this forum in hope improving my Programming skills.

Currently i have a college assignmnt. It is to create a dice game

"There is a little known dice game which is played with a pair of dice and has the following rules: the player keeps rolling the dice until the total on the dice is 2, 3, 7, 11 or 12. If the total is 7, then the player loses, if it's 2, 3, 11 or 12 then the player wins. If the total is any other number, then the game continues.
Example:
Would you like to play (y/n): y
The dice are:
1 6
The total is 7
Unfortunately, you lost.

Marking scheme says 40% goes for the use of methods, so i was hoping if any of you kind gents/ladies would help me out with this one?

Dani AI

Generated

As noted, this is a common exercise and 's direction — keep a loop for play/quit and split work into methods — is the right approach. To capture the 40% for methods, make each method single-purpose, small, and easy to test: input, rolling, classification, printing, and the overall game loop.

Suggested class skeleton (method names only — keep logic inside these small units):

public enum Outcome { WIN, LOSE, CONTINUE }

public class DiceGame {
    // RNG can be swapped for a seeded Random during tests
    private final java.util.concurrent.ThreadLocalRandom rng = java.util.concurrent.ThreadLocalRandom.current();

    public int rollDie() { ... }                // 1..6
    public int[] rollTwoDice() { ... }          // returns both dice values
    public Outcome classify(int total) { ... }  // maps totals to Outcome
    public void printRoll(int die1, int die2) { ... }
    public void play(java.util.Scanner in) { ... } // user loop, calls others
}

Implementation notes and grading tips:

  • Use ThreadLocalRandom.current().nextInt(1, 7) for 1..6 (see ThreadLocalRandom javadoc). For repeatable unit tests, inject a Random with a fixed seed instead.
  • Keep I/O out of logic: classify should only map a total to Outcome (use a small Set or switch), so it can be unit tested.
  • Robust input parsing: use Scanner.nextLine().trim().equalsIgnoreCase("y") (guard against empty lines) and always close the Scanner.
  • Demonstrate method use in Javadoc and include a few JUnit tests: test rollDie() bounds, classify() for known totals, and play() behavior in deterministic mode.
  • Small extras that impress markers: roll-history printing, win/loss counters, and a seeded test-mode. A quick probability note: immediate win and loss combinations are equal, so eventual win probability is 50% — a short comment like this shows extra understanding.

Recommended Answers

All 3 Replies

I knew that assignment looked familiar, take a look here :)

Hello - welcome to the forum. You will be told if you keep posting questions like this that we don't do your homework for you, you need to post code to demonstrate your own effort.

But I'll give you a point in the right direction - assuming that you don't need to use any animations or any methods in the javax.swing library, first focus on getting input from the user and then use this in combination with a while loop, to ensure that it continues to run until the user inputs 'n.'

You will then need to use the math library to get two random number's between, and including, 1 and 6 - see to see how.
Next you need to add these two numbers and check to see if it meets the criteria - i.e. does it add up to 7?

Something like:

int sum = randomOne + randomTwo;

if( sum == 7 )
{
   System.out.println( "You lose!" );
} else if( sum == 2 || sum == 3 || sum == 11 || sum ==12  )
{
  System.out.println( "You win :D" );
} else {
  // Keep going
}

All of the above can be split into methods to keep it all simple and use another class to unite them.
Remember if you don't show any effort no one will want to help you.

, thanks for the link, much appreciated...infact i think the author of the thread you linked me to is in my course!

@katana, thanks for your help too :) PS i wasnt actually looking for someone to do the assignmnent for me, i just wanted some tips as to how i should approach the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.