Avatar of terhol

terhol's solution

to Yacht in the Java Track

Published at Oct 21 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 category chosen.

Scores in Yacht

Category Score Description Example
Ones 1 × number of ones Any combination 1 1 1 4 5 scores 3
Twos 2 × number of twos Any combination 2 2 3 4 5 scores 4
Threes 3 × number of threes Any combination 3 3 3 3 3 scores 15
Fours 4 × number of fours Any combination 1 2 3 3 5 scores 0
Fives 5 × number of fives Any combination 5 1 5 2 5 scores 15
Sixes 6 × number of sixes Any combination 2 3 4 5 6 scores 6
Full House Total of the dice Three of one number and two of another 3 3 3 5 5 scores 19
Four of a Kind Total of the four dice At least four dice showing the same face 4 4 4 4 6 scores 16
Little Straight 30 points 1-2-3-4-5 1 2 3 4 5 scores 30
Big Straight 30 points 2-3-4-5-6 2 3 4 5 6 scores 30
Choice Sum of the dice Any combination 2 3 3 4 6 scores 18
Yacht 50 points All five dice showing the same face 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.

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

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;
import java.util.HashMap;
import java.util.Map;

class Yacht {
    private final int STRAIGHT_SCORE = 30;
    private final int YACHT_SCORE = 50;
    private int[] dice;
    private YachtCategory yachtCategory;

    Yacht(int[] dice, YachtCategory yachtCategory) {
        this.dice = dice;
        this.yachtCategory = yachtCategory;
    }


    public int score() {
        Arrays.sort(dice);
        int score = 0;
        switch (yachtCategory) {
            case ONES:
                score = scoreOfBasicGames(1, dice);
                break;
            case TWOS:
                score = scoreOfBasicGames(2, dice);
                break;
            case THREES:
                score = scoreOfBasicGames(3, dice);
                break;
            case FOURS:
                score = scoreOfBasicGames(4, dice);
                break;
            case FIVES:
                score = scoreOfBasicGames(5, dice);
                break;
            case SIXES:
                score = scoreOfBasicGames(6, dice);
                break;
            case LITTLE_STRAIGHT:
                if (areNumbersSequential(dice) && isOneTheFirst(dice)) {
                    score = STRAIGHT_SCORE;
                }
                break;
            case BIG_STRAIGHT:
                if (areNumbersSequential(dice) && !isOneTheFirst(dice)) {
                    score = STRAIGHT_SCORE;
                }
                break;
            case CHOICE:
                score = addAllNumbers(dice);
                break;
            case YACHT:
                if (areAllNumbersSame(dice)) {
                    score = YACHT_SCORE;
                }
                break;
            case FOUR_OF_A_KIND:
                if (isEligibleForFourOfKind(dice)) {
                    score = countForFourOfAKind(dice);
                }
                break;
            case FULL_HOUSE:
                if (isEligibleForFullHouse(dice)) {
                    score = addAllNumbers(dice);
                }

        }
        return score;
    }

    private int scoreOfBasicGames(int chosenNumber, int[] dice) {
        int finalNumber = 0;
        int score;
        for (int numberOnDice : dice) {
            if (numberOnDice == chosenNumber) {
                finalNumber++;
            }
        }
        score = finalNumber * chosenNumber;
        return score;
    }

    private int addAllNumbers(int[] dice) {
        int score = 0;
        for (int numberOnDice : dice) {
            score = score + numberOnDice;
        }
        return score;
    }

    private boolean areNumbersSequential(int[] dice) {
        boolean isSequential = true;
        int firstNumber = dice[0];
        for (int number : dice) {
            if (firstNumber != number) {
                isSequential = false;
            }
            firstNumber++;

        }
        return isSequential;
    }

    private Map<Integer, Integer> countFrequencies(int[] dice) {
        Map<Integer, Integer> numberFrequencies = new HashMap<>();
        for (int number : dice) {
            if (numberFrequencies.containsKey(number)) {
                int oldNumberOfFrequencies = numberFrequencies.get(number);
                numberFrequencies.put(number, oldNumberOfFrequencies + 1);
            } else {
                numberFrequencies.put(number, 1);
            }

        }
        return numberFrequencies;
    }

    private boolean areAllNumbersSame(int[] dice) {
        boolean areNumbersSame = false;
        Map<Integer, Integer> numberFrequencies = countFrequencies(dice);
        if (numberFrequencies.size() == 1) {
            areNumbersSame = true;
        }
        return areNumbersSame;
    }

    private boolean isEligibleForFourOfKind(int[] dice) {
        Map<Integer, Integer> numberFrequencies = countFrequencies(dice);
        boolean isEligible = false;
        if (numberFrequencies.containsValue(4) || numberFrequencies.containsValue(5)) {
            isEligible = true;
        }
        return isEligible;

    }

    private int countForFourOfAKind(int[] dice) {
        final int FOUR_OF_KIND = 4;
        Map<Integer, Integer> numberFrequencies = countFrequencies(dice);
        int score;
        int numberPresence = numberFrequencies.containsValue(4) ? 4 : 5;
        int numberCounted = numberFrequencies.entrySet().stream().filter(entry -> numberPresence == entry.getValue())
                .map(Map.Entry::getKey).findFirst().get();
        score = FOUR_OF_KIND * numberCounted;
        return score;
    }

    private boolean isEligibleForFullHouse(int[] dice) {
        Map<Integer, Integer> numberFrequencies = countFrequencies(dice);
        boolean isEligible = false;
        if (numberFrequencies.containsValue(2) && numberFrequencies.containsValue(3)) {
            isEligible = true;
        }
        return isEligible;

    }

    private boolean isOneTheFirst(int[] dice) {
        Map<Integer, Integer> numberFrequencies = countFrequencies(dice);
        return numberFrequencies.containsKey(1);


    }
}

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?