# artemkorsakov's solution

## to Yacht in the Java Track

Published at Feb 12 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

# Score a single throw of dice in Yacht

The dice game Yacht is from the same family as Poker Dice, Generala and particularly Yahtzee, of which it is a precursor. In the game, five dice are rolled and the result can be entered in any of twelve categories. The score of a throw of the dice depends on the category chosen.

## Scores in Yacht

``````YachtCategory    Score                   Example
Ones            1 × number of ones      1 1 1 4 5 scores 3
Twos            2 × number of twos      2 2 3 4 5 scores 4
Threes          3 × number of threes    3 3 3 3 3 scores 15
Fours           4 × number of fours     1 2 3 3 5 scores 0
Fives           5 × number of fives     5 1 5 2 5 scores 15
Sixes           6 × number of sixes     2 3 4 5 6 scores 6
Full House      Total of the dice       3 3 3 5 5 scores 19
Four of a Kind  Total of the four dice  4 4 4 4 6 scores 16
Little Straight 30 points               1 2 3 4 5 scores 30
Big Straight    30 points               2 3 4 5 6 scores 30
Choice          Sum of the dice         2 3 3 4 6 scores 18
Yacht           50 points               4 4 4 4 4 scores 50
``````

If the dice do not satisfy the requirements of a category, the score is zero. If, for example, Four Of A Kind is entered in the Yacht category, zero points are scored. A Yacht scores zero if entered in the Full House category.

## Task

Given a list of values for five dice and a category, your solution should return the score of the dice for that category. If the dice do not satisfy the requirements of the category your solution should return 0. You can assume that five values will always be presented, and the value of each will be between one and six inclusively. You should not assume that the dice are ordered.

# Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

in your terminal.

## Source

James Kilfiger, using wikipedia https://en.wikipedia.org/wiki/Yacht_(dice_game)

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### YachtTest.java

``````import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class YachtTest {

@Test
public void yacht() {
Yacht yacht = new Yacht(new int[]{ 5, 5, 5, 5, 5 }, YachtCategory.YACHT);
assertEquals(50, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void notYacht() {
Yacht yacht = new Yacht(new int[]{ 1, 3, 3, 2, 5 }, YachtCategory.YACHT);
assertEquals(0, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void ones() {
Yacht yacht =  new Yacht(new int[]{ 1, 1, 1, 3, 5 }, YachtCategory.ONES);
assertEquals(3, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void onesOutOfOrder() {
Yacht yacht = new Yacht(new int[]{ 3, 1, 1, 5, 1 }, YachtCategory.ONES);
assertEquals(3, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void noOnes() {
Yacht yacht = new Yacht(new int[]{ 4, 3, 6, 5, 5 }, YachtCategory.ONES);
assertEquals(0, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void twos() {
Yacht yacht = new Yacht(new int[]{ 2, 3, 4, 5, 6 }, YachtCategory.TWOS);
assertEquals(2, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void fours() {
Yacht yacht = new Yacht(new int[]{ 1, 4, 1, 4, 1 }, YachtCategory.FOURS);
assertEquals(8, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void yachtCountedAsThrees() {
Yacht yacht = new Yacht(new int[]{ 3, 3, 3, 3, 3 }, YachtCategory.THREES);
assertEquals(15, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void yachtOfThreesCountedAsFives() {
Yacht yacht = new Yacht(new int[]{ 3, 3, 3, 3, 3 }, YachtCategory.FIVES);
assertEquals(0, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void sixes() {
Yacht yacht = new Yacht(new int[]{ 2, 3, 4, 5, 6 }, YachtCategory.SIXES);
assertEquals(6, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void fullHouseTwoSmallThreeBig() {
Yacht yacht = new Yacht(new int[]{ 2, 2, 4, 4, 4 }, YachtCategory.FULL_HOUSE);
assertEquals(16, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void fullHouseThreeSmallTwoBig() {
Yacht yacht = new Yacht(new int[]{ 5, 3, 3, 5, 3 }, YachtCategory.FULL_HOUSE);
assertEquals(19, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void twoPairIsNotAFullHouse() {
Yacht yacht = new Yacht(new int[]{ 2, 2, 4, 4, 5 }, YachtCategory.FULL_HOUSE);
assertEquals(0, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void fourOfAKindIsNotAFullHouse() {
Yacht yacht = new Yacht(new int[]{ 1, 4, 4, 4, 4 }, YachtCategory.FULL_HOUSE);
assertEquals(0, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void yachtIsNotAFullHouse() {
Yacht yacht = new Yacht(new int[]{ 2, 2, 2, 2, 2 }, YachtCategory.FULL_HOUSE);
assertEquals(0, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void fourOfAKind() {
Yacht yacht = new Yacht(new int[]{ 6, 6, 4, 6, 6 }, YachtCategory.FOUR_OF_A_KIND);
assertEquals(24, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void yachtCanBeScoredAsFourOfAKind() {
Yacht yacht = new Yacht(new int[]{ 3, 3, 3, 3, 3 }, YachtCategory.FOUR_OF_A_KIND);
assertEquals(12, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void fullHouseIsNotFourOfAKind() {
Yacht yacht = new Yacht(new int[]{ 3, 3, 3, 5, 5 }, YachtCategory.FOUR_OF_A_KIND);
assertEquals(0, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void littleStraight() {
Yacht yacht = new Yacht(new int[]{ 3, 5, 4, 1, 2 }, YachtCategory.LITTLE_STRAIGHT);
assertEquals(30, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void littleStraightAsBigStraight() {
Yacht yacht = new Yacht(new int[]{ 1, 2, 3, 4, 5 }, YachtCategory.BIG_STRAIGHT);
assertEquals(0, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void fourInOrderButNotALittleStraight() {
Yacht yacht = new Yacht(new int[]{ 1, 1, 2, 3, 4 }, YachtCategory.LITTLE_STRAIGHT);
assertEquals(0, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void noPairsButNotALittleStraight() {
Yacht yacht = new Yacht(new int[]{ 1, 2, 3, 4, 6 }, YachtCategory.LITTLE_STRAIGHT);
assertEquals(0, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void minimumIs1MaximumIs5ButNotALittleStraight() {
Yacht yacht = new Yacht(new int[]{ 1, 1, 3, 4, 5 }, YachtCategory.LITTLE_STRAIGHT);
assertEquals(0, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void bigStraight() {
Yacht yacht = new Yacht(new int[]{ 4, 6, 2, 5, 3 }, YachtCategory.BIG_STRAIGHT);
assertEquals(30, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void bigStraightAsLittleStraight() {
Yacht yacht = new Yacht(new int[]{ 6, 5, 4, 3, 2 }, YachtCategory.LITTLE_STRAIGHT);
assertEquals(0, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void choice() {
Yacht yacht = new Yacht(new int[]{ 3, 3, 5, 6, 6 }, YachtCategory.CHOICE);
assertEquals(23, yacht.score());
}

@Ignore("Remove to run test")
@Test
public void yachtAsChoice() {
Yacht yacht = new Yacht(new int[]{ 2, 2, 2, 2, 2 }, YachtCategory.CHOICE);
assertEquals(10, yacht.score());
}

}``````
``````import java.util.Arrays;

class Yacht {
private int score;

Yacht(int[] dice, YachtCategory yachtCategory) {
switch (yachtCategory) {
case YACHT:
score = getYACHTScore(dice);
break;
case ONES:
score = getNumberScore(dice, 1);
break;
case TWOS:
score = getNumberScore(dice, 2);
break;
case THREES:
score = getNumberScore(dice, 3);
break;
case FOURS:
score = getNumberScore(dice, 4);
break;
case FIVES:
score = getNumberScore(dice, 5);
break;
case SIXES:
score = getNumberScore(dice, 6);
break;
case FULL_HOUSE:
score = getFullHouseScore(dice);
break;
case FOUR_OF_A_KIND:
score = getFourOfAKindScore(dice);
break;
case LITTLE_STRAIGHT:
score = getLittleStraightScore(dice);
break;
case BIG_STRAIGHT:
score = getBigStraightScore(dice);
break;
case CHOICE:
score = getSum(dice);
break;
}
}

int score() {
return score;
}

private int getYACHTScore(int[] dice) {
return Arrays.stream(dice).distinct().count() == 1 ? 50 : 0;
}

private int getNumberScore(int[] dice, int number) {
return Arrays.stream(dice).filter(d -> d == number).sum();
}

private int getFullHouseScore(int[] dice) {
boolean isOnlyTwoUnique = Arrays.stream(dice).distinct().count() == 2;
int firstCount = (int) Arrays.stream(dice).filter(d -> d == dice[0]).count();
boolean isFullHouse = isOnlyTwoUnique && (firstCount == 3 || firstCount == 2);
return isFullHouse ? getSum(dice) : 0;
}

private int getFourOfAKindScore(int[] dice) {
if (Arrays.stream(dice).distinct().count() > 2) {
return 0;
}
int firstCount = (int) Arrays.stream(dice).filter(d -> d == dice[0]).count();
if (firstCount >= 4) {
return 4 * dice[0];
}
return firstCount == 1 ? getSum(dice) - dice[0] : 0;
}

private int getLittleStraightScore(int[] dice) {
boolean isLittleStraightSum = getSum(dice) == 15;
return isStraight(dice) && isLittleStraightSum ? 30 : 0;
}

private int getBigStraightScore(int[] dice) {
boolean isBigStraightSum = getSum(dice) == 20;
return isStraight(dice) && isBigStraightSum ? 30 : 0;
}

private boolean isStraight(int[] dice) {
return Arrays.stream(dice).distinct().count() == 5;
}

private int getSum(int[] dice) {
return Arrays.stream(dice).sum();
}

}``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?