[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Mrigank11/programming/master/yahtzee/java/Dice.java [Back]  [Original]

/**
 * Dice class used in the yahtzee game. It can display the number rolled
 * and have n number of sides.
 * @author Felipe Scrochio Custdio
 */
public class Dice {
	
	private int sides;
	private int result;

	/**
	 * Standard 6 sides dice constructor
	 */
	public Dice() {
		sides = 6;
	}

	/**
	 * n number of sides dice constructor
	 * @param  n Number of sides in the dice
	 */
	public Dice(int n) {
		sides = n;
	}

	/**
	 * Gets the last rolled result of this dice
	 * @return Last dice result
	 */
	public int getResult() {
		return result;
	}

	/**
	 * Rolls the dice
	 * @return Number from 1 to n sides.
	 */
	public int roll() {
		Random r = new Random();
		result = Math.abs((r.getIntRand(sides)));
		result++; /* change min from 0 to 1 */
		return result;
	}

	/**
	 * Prints the last result of the dice
	 */
	public void display() {

		String d;
		if (sides == 6) {

			switch(result) {

				case 1:
					d = "+-----+\n|     |\n|  *  |\n|     |\n+-----+";
					System.out.print(d);
					
				case 2:
					d = "+-----+\n|    *|\n|     |\n|*    |\n+-----+";
					System.out.print(d);
					
				case 3:
					d = "+-----+\n|    *|\n|  *  |\n|*    |\n+-----+";
					System.out.print(d);
					
				case 4:
					d = "+-----+\n|*   *|\n|     |\n|*   *|\n+-----+";
					System.out.print(d);
					
				case 5:
					d = "+-----+\n|*   *|\n|  *  |\n|*   *|\n+-----+";
					System.out.print(d);
					
				case 6:
					d = "+-----+\n|*   *|\n|*   *|\n|*   *|\n+-----+";
					System.out.print(d);
			}

		} else {
			System.out.println("This type of dice can't be printed.");
		}
	}
}

Web Proxy Viewer  |  New URL  |  Original Page