Yatzy in Java

Yahtzee153 is a simple and reimagined Yahtzee/Yatzy game where the player goes through three different moments of winning the game. In the first part, they are tasked with scoring a perfect game. This takes the longest as players collect Shop Points from previous games. These Shop Points go to an ability to modify a die in the rolls cast. The second part then asks the player to score 0 before finally asking the player to get a specific (and random) score in between these two values.

01 The title and menu screen. Even if it is quite basic, it allows for future development where one can resume a saved state of the game.

This project was undertaken to showcase that I do (still) know how to program in Java. Previous older projects were usually in a group (and contributions may seem quite muddy) so this was purposefully done independently.

Originally I was going to add more than just the ability to modify a die roll. Some of these ideas included an undo ability, saving rolls for the future, unlocking some kind of narrative (for an alternative ending), or rescoring on straights/flushes. In the end I opted to keep it to a single extra mechanic to keep a short and less messy experience.

If I were to improve on it, I would implement a saved state so players could freely leave and come back to games. I would also like to implement confetti animation (or something) once the player wins the game. Animations on the rolls are also welcome. This perhaps should all be done without JFrames to begin with and allow some kind of Fullscreen mode with optional music and sounds. The Shop and/or Exit buttons are out of alignment (kinda). They were purposefully placed this way, but only after having nudged them in various positions until I undecidedly left them in the places they are in now. Maybe the ability to save an image of the accomplished score so players can brag about it to others without having to manually screenshot it themselves. Essentially these are all Quality of Life updates.

02 Just a sample of a game in progress.

The overall code amounts to about 750 lines and took about 10 days to make from start to finish (including playtesting). The game itself takes about 15min to play completely. It may otherwise take about 8-10 rounds on average.

Below is a snippet of the code that shows how to determine the scores for the rolls. Bubble sorting is used when figuring out the scoring for the straight and royal flush rolls.

switch (value) {
    case 7:
        // Logic for a straight
        for (int i = 0; i < compareArray.length; i++) {
            compareArray[i] = 0;
        }

        System.arraycopy(rollArray, 0, compareArray, 0, rollArray.length);

        bubbleSort(compareArray);

        checkVal = 0;
        for (int i = 1; i < compareArray.length; i++) {
            if (compareArray[i-1]+1 == compareArray[i]) {
                checkVal++;
            }
        }

        if (checkVal == 4) {
            for (int i = 0; i < rollArray.length; i++) {
                scoreArray[value-1] = scoreArray[value-1] + rollArray[i];
            }
        }

        if (scoreArray[value-1] == 20) {  // 2+3+4+5+6
            perfectScore = "*** ";
        }

        break;

    case 8:
        // Logic for a royal flush
        for (int i = 0; i < compareArray.length; i++) {
            compareArray[i] = 0;
        }

        System.arraycopy(rollArray, 0, compareArray, 0, rollArray.length);

        bubbleSort(compareArray);

        checkVal = 0;
        if (compareArray[0] == compareArray[1]) { checkVal++; }
        if (compareArray[3] == compareArray[4]) { checkVal++; }
        if (compareArray[2] == compareArray[0]) { checkVal++; }
        if (compareArray[2] == compareArray[3]) { checkVal++; }

        if (checkVal == 3) {
            for (int i = 0; i < rollArray.length; i++) {
                scoreArray[value-1] = scoreArray[value-1] + rollArray[i];
            }
        }

        if (scoreArray[value-1] == 28) {  // 6+6+6+5+5
            perfectScore = "*** ";
        }
        break;
    default:
        // Score round as appropriate
        for (int i = 0; i < rollArray.length; i++) {
            if (rollArray[i] == value) {
                scoreArray[value-1] = scoreArray[value-1] + rollArray[i];
            }
            if (scoreArray[value-1] == value * 5) {
                perfectScore = "*** ";
            }
        }
        break;
}

2022-08-23