Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package model;
- import java.util.Random;
- public class Yatzy {
- // Face values of the 5 dice.
- // 1 <= values[i] <= 6.
- private int[] values = new int[5];
- // Number of times the 5 dice have been thrown.
- // 0 <= throwCount <= 3.
- private int throwCount = 0;
- // Random number generator.
- private Random random = new Random();
- public Yatzy() {
- }
- /**
- * Returns the 5 face values of the dice.
- */
- public int[] getValues() {
- return this.values;
- }
- /**
- * Sets the 5 face values of the dice. Req: values contains 5 face values in
- * [1..6]. Note: This method is only meant to be used for test, and
- * therefore has package visibility.
- */
- void setValues(int[] values) {
- for(int i = 0; i < values.length; i++){
- this.values[i] = values[i];
- }
- }
- /**
- * Returns the number of times the 5 dice has been thrown.
- */
- public int getThrowCount() {
- return this.throwCount;
- }
- /**
- * Resets the throw count.
- */
- public void resetThrowCount() {
- this.throwCount = 0;
- }
- /**
- * Rolls the 5 dice. Only roll dice that are not hold. Req: holds contain 5
- * boolean values.
- */
- public void throwDice(boolean[] holds) {
- for(int i = 0; i < this.values.length; i++){
- if(!holds[i]){
- this.values[i] = random.nextInt(6) + 1;
- }
- }
- this.throwCount++;
- }
- // -------------------------------------------------------------------------
- /**
- * Returns all results possible with the current face values. The order of
- * the results is the same as on the score board. Note: This is an optional
- * method. Comment this method out, if you don't want use it.
- */
- public int[] getResults() {
- int[] results = new int[15];
- for (int i = 0; i <= 5; i++) {
- results[i] = this.sameValuePoints(i + 1);
- }
- results[6] = this.onePairPoints();
- results[7] = this.twoPairPoints();
- results[8] = this.threeSamePoints();
- results[9] = this.fourSamePoints();
- results[10] = this.fullHousePoints();
- results[11] = this.smallStraightPoints();
- results[12] = this.largeStraightPoints();
- results[13] = this.chancePoints();
- results[14] = this.yatzyPoints();
- return results;
- }
- // -------------------------------------------------------------------------
- // Returns an int[7] containing the frequency of face values.
- // Frequency at index v is the number of dice with the face value v, 1 <= v
- // <= 6.
- // Index 0 is not used.
- private int[] calcCounts() {
- int[] dice = new int[7];
- int throwValue = 0;
- // g� igennem antal terninger
- for(int place = 0; place < this.values.length; place++){
- throwValue = this.values[place];
- dice[throwValue]++;
- }
- return dice;
- }
- /**
- * Returns same-value points for the given face value. Returns 0, if no dice
- * has the given face value. Requires: 1 <= value <= 6;
- */
- public int sameValuePoints(int value) {
- return calcCounts()[value] * value;
- }
- /**
- * Returns points for one pair (for the face value giving highest points).
- * Returns 0, if there aren't 2 dice with the same face value.
- */
- public int onePairPoints() {
- int greatestSum = 0;
- int faceValueSum = 0;
- for(int i = 1; i < this.calcCounts().length; i++){
- if(this.calcCounts()[i] >= 2){
- faceValueSum = i * 2;
- greatestSum = faceValueSum;
- }
- }
- return greatestSum;
- }
- /**
- * Returns points for two pairs (for the 2 face values giving highest
- * points). Returns 0, if there aren't 2 dice with one face value and 2 dice
- * with a different face value.
- */
- public int twoPairPoints() {
- int pair1 = 0;
- int pair2 = 0;
- int sum = 0;
- for(int i = 1; i < this.calcCounts().length; i++){
- if(this.calcCounts()[i] >= 2){
- if(pair1 == 0){
- pair1 = i * 2;
- } else if(pair2 == 0){
- pair2 = i * 2;
- }
- }
- }
- if(pair1 != 0 && pair2 != 0){
- sum = pair1 + pair2;
- }
- return sum;
- }
- /**
- * Returns points for 3 of a kind. Returns 0, if there aren't 3 dice with
- * the same face value.
- */
- public int threeSamePoints() {
- int value1 = 0;
- for(int i = 1; i < this.calcCounts().length; i++){
- if(this.calcCounts()[i] >= 3){
- value1 = i * 3;
- }
- }
- return value1;
- }
- /**
- * Returns points for 4 of a kind. Returns 0, if there aren't 4 dice with
- * the same face value.
- */
- public int fourSamePoints() {
- int value = 0;
- for(int i = 1; i < this.calcCounts().length; i++){
- if(this.calcCounts()[i] >= 4){
- value = i * 4;
- }
- }
- return value;
- }
- /**
- * Returns points for full house. Returns 0, if there aren't 3 dice with one
- * face value and 2 dice a different face value.
- */
- public int fullHousePoints() {
- int value1 = 0;
- int value2 = 0;
- int sum = 0;
- for(int i = 1; i < this.calcCounts().length; i++){
- if(this.calcCounts()[i] == 2){
- value1 = i * 2;
- }
- if(this.calcCounts()[i] == 3){
- value2 = i * 3;
- }
- }
- if(value1 != 0 && value2 != 0){
- sum = value1 + value2;
- }
- return sum;
- }
- /**
- * Returns points for small straight. Returns 0, if the dice are not showing
- * 1,2,3,4,5.
- */
- public int smallStraightPoints() {
- int smallStraight = 15;
- // T�l ikke den sidste med, da det er en 6'er
- for(int i = 1; i < this.calcCounts().length - 1; i++){
- if(this.calcCounts()[i] != 1){
- smallStraight = 0;
- }
- }
- return smallStraight;
- }
- /**
- * Returns points for large straight. Returns 0, if the dice is not showing
- * 2,3,4,5,6.
- */
- public int largeStraightPoints() {
- int largeStraight = 20;
- // T�l ikke det f�rste element med
- for(int i = 2; i < this.calcCounts().length; i++){
- if(this.calcCounts()[i] != 1){
- largeStraight = 0;
- }
- }
- return largeStraight;
- }
- /**
- * Returns points for chance.
- */
- public int chancePoints() {
- int chance = 0;
- for(int i = 1; i < this.calcCounts().length; i++){
- chance += i * this.calcCounts()[i];
- }
- return chance;
- }
- /**
- * Returns points for yatzy. Returns 0, if there aren't 5 dice with the same
- * face value.
- */
- public int yatzyPoints() {
- int yatzy = 0;
- for(int i = 1; i < this.calcCounts().length; i++){
- if(this.calcCounts()[i] == 5){
- yatzy = 50;
- }
- }
- return yatzy;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement