Simple dice games can be easily created in Java by creating a wrapper around the Random class called Dice. This wrapper will include a simple dice game and can be used as a dice abstraction within a larger program without having to worry about random number seeds and other technical details. Because it is meant to be extensible, this class will include options for setting the number of sides to a die and the number of simultaneous rolls to perform.
Things You'll Need:
- Java development kit
- Java runtime engine
- Java IDE or text editor
- Step 1
Create a "Rollable" interface. This can be done by creating a new class file and writing the following inside it:
/**
* This interface defines objects which are able to be rolled in the fashion of dice.
* @author Amber Rollins
*/
public interface Rollable {
/**
* Rolls a single object and returns the result of the roll.
* @return the result of the roll
*/
int roll();
/**
* Roll a number of objects and return the total rolled.
* @param number the number of objects to roll
* @return the sum total of all rolls
*/
int roll(int number);
} - Step 2
Create a "Dice" class that implements the Rollable interface. The interface defines the minimum functionality that will be required for a dice class, but that interface still needs to be implemented.
public class Dice {
// Fill in the rest using steps 3-5.
} - Step 3
Write two constructors, one that takes no arguments that assumes a six sided die is being created, and another which takes the number of sides on the die as an argument.
/**
* A die is created with 6 sides.
*/
public Dice() {
random = new Random();
sides = 6;
}
/**
* A die is created with a specified number of sides.
* @param sides the number of sides to the die
*/
public Dice(int sides) {
this();
this.sides = sides;
} - Step 4
Write the two roll functions to satisfy the interface requirements declared in Rollable. One function will be used to perform a single roll, and the second will be used to extend that to performing a series of rolls all at once and returning the result.
public int roll() {
// Add 1 because the possible results run from 0 through sides-1 (see references 1), and this is not how ordinary dice behave.
return random.nextInt(sides)+1 ;
}
public int roll(int number) {
int total = 0;
for (int x = 0; x < number; x++) {
total += roll();
}
return total;
} - Step 5
Write a main function that will play a simple high roll dice game. This game will allow two players to reach roll a single die, and the highest roller will win.
public static void main(String[] args) {
try {
Dice d = new Die();
System.out.println("Player one...press any key to roll.");
System.in.read();
int oneRolls = d.roll();
System.out.println("Player one rolls a " + oneRolls);
System.out.println("Player two...press any key to roll.");
System.in.read();
int twoRolls = d.roll();
System.out.println("Player two rolls a " + twoRolls);
if (oneRolls > twoRolls) {
System.out.println("Player one wins.");
} else {
System.out.println("Player two wins.");
}
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
No comments:
Post a Comment