KaiserGame.java

1
package de.aikiit.game.kaiser;
2
3
import java.util.Objects;
4
import java.util.Scanner;
5
6
/**
7
 * This class encapsulates the actual game flow.
8
 * A player
9
 * <ul>
10
 *     <li>is shown current statistics and</li>
11
 *     <li>can start to act</li>
12
 *     <li>until the maximum number of rounds is reached and the game ends.</li>
13
 * </ul>
14
 */
15
public class KaiserGame {
16
17
    /**
18
     * The player can play at most this number of rounds.
19
     */
20
    public static final int MAX_ROUNDS = 10;
21
22
    private final KaiserEngine engine;
23
    private final KaiserEnginePrinter printer;
24
    private final Scanner input;
25
26
    /**
27
     * Initializes a game with default parameters.
28
     */
29
    public KaiserGame() {
30
        this(new KaiserEngine(), new Scanner(System.in));
31
    }
32
33
    /**
34
     * Allows setting an explicit engine for testing purposes.
35
     * @param engine initialize with the given engine.
36
     */
37
    KaiserGame(KaiserEngine engine, Scanner input) {
38
        this.engine = Objects.requireNonNull(engine);
39
        this.input = Objects.requireNonNull(input);
40
        this.printer = new KaiserEnginePrinter(this.engine);
41
    }
42
43
    /**
44
     * Shows the introduction and start confirmation without the original BASIC code
45
     * gate, then runs the reign until completion, dismissal or an abort. A replay
46
     * starts again with the introduction and start confirmation.
47
     */
48
    public void run() {
49
        boolean playAgain;
50
        do {
51
            playAgain = false;
52 1 1. run : removed call to de/aikiit/game/kaiser/KaiserGame::intro → SURVIVED
            intro();
53 1 1. run : removed call to java/io/PrintStream::println → KILLED
            System.out.println("Start (J/N)?");
54 1 1. run : negated conditional → KILLED
            if ("n".equalsIgnoreCase(nextAnswer())) {
55 1 1. run : removed call to de/aikiit/game/kaiser/KaiserEngine::abort → KILLED
                engine.abort();
56
                break;
57
            }
58 1 1. run : negated conditional → KILLED
            while (!engine.isGameOver()) {
59 1 1. run : removed call to de/aikiit/game/kaiser/KaiserEngine::startNewRound → KILLED
                engine.startNewRound();
60 1 1. run : removed call to de/aikiit/game/kaiser/KaiserGame::status → KILLED
                status();
61 1 1. run : negated conditional → KILLED
                if (engine.isGameOver()) {
62
                    break; // BASIC line 270: final report, but no eleventh action phase.
63
                }
64 1 1. run : removed call to de/aikiit/game/kaiser/KaiserEngine::prepareLandTrading → KILLED
                engine.prepareLandTrading();
65 1 1. run : removed call to java/io/PrintStream::println → KILLED
                System.out.println("Ein Hektar Land kostet " + engine.getLandPrice() + " dt Getreide.");
66 1 1. run : removed call to de/aikiit/game/kaiser/KaiserGame::actions → KILLED
                actions();
67 1 1. run : negated conditional → KILLED
                if (!engine.isGameOver()) {
68 1 1. run : removed call to de/aikiit/game/kaiser/KaiserEngine::finishRoundAfterActions → KILLED
                    engine.finishRoundAfterActions();
69
                }
70
            }
71
            switch (engine.getState()) {
72 1 1. run : removed call to java/io/PrintStream::println → KILLED
                case DISMISSED -> System.out.println(printer.getDismissal());
73 1 1. run : removed call to java/io/PrintStream::println → KILLED
                case ABORTED -> System.out.println("Das kann man nicht mit mir machen! Suchen Sie sich einen anderen zum Mitspielen!");
74
                case COMPLETED -> {
75 1 1. run : removed call to de/aikiit/game/kaiser/KaiserGame::finish → KILLED
                    finish();
76
                    // Only the mediocre branch reaches the replay prompt in lines 959–966.
77 1 1. run : negated conditional → KILLED
                    if (engine.getRegency() == KaiserEngine.Regency.MEDIOCRE) {
78 1 1. run : removed call to java/io/PrintStream::println → KILLED
                        System.out.println("Nochmal (J/N)?");
79
                        playAgain = "j".equalsIgnoreCase(nextAnswer());
80 1 1. run : negated conditional → KILLED
                        if (playAgain) {
81 1 1. run : removed call to de/aikiit/game/kaiser/KaiserEngine::reset → KILLED
                            engine.reset();
82
                        }
83
                    }
84
                }
85
                default -> throw new IllegalStateException("Game ended without a final state.");
86
            }
87 1 1. run : negated conditional → KILLED
        } while (playAgain);
88 1 1. run : removed call to de/aikiit/game/kaiser/KaiserGame::byeByeBanner → SURVIVED
        byeByeBanner();
89
    }
90
91
    /**
92
     * Shows information about the game flow and welcomes the player.
93
     */
94
    public void intro() {
95 1 1. intro : removed call to de/aikiit/game/kaiser/KaiserGame::banner → SURVIVED
        banner();
96 1 1. intro : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println("Versuchen Sie die antike Stadt " + KaiserEnginePrinter.ANSI_YELLOW + ">SUMERIA<" + KaiserEnginePrinter.ANSI_RESET + " zu regieren!");
97 1 1. intro : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println("Ihre Regierungszeit beträgt " + MAX_ROUNDS + " Jahre.");
98 1 1. intro : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println("Nach jeweils einem Jahr erhalten Sie einen Bericht über die Entwicklung in der Stadt.");
99 1 1. intro : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println("Dann werden wir weitersehen .......");
100 1 1. intro : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println();
101
    }
102
103
    /**
104
     * Shows the game's banner.
105
     */
106
    public void banner() {
107
        // generated with the help of https://manytools.org/hacker-tools/ascii-banner/
108
        // font: banner
109 1 1. banner : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println(KaiserEnginePrinter.ANSI_RED);
110 1 1. banner : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println("""
111
                #    #                              \s
112
                #   #    ##   #  ####  ###### ##### \s
113
                #  #    #  #  # #      #      #    #\s
114
                ###    #    # #  ####  #####  #    #\s
115
                #  #   ###### #      # #      ##### \s
116
                #   #  #    # # #    # #      #   # \s
117
                #    # #    # #  ####  ###### #    #\s
118
                                                    \s
119
                """);
120 1 1. banner : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println(KaiserEnginePrinter.ANSI_RESET);
121
    }
122
123
    /**
124
     * Shows console banner after the game is over.
125
     */
126
    public void byeByeBanner() {
127
        // generated with the help of https://manytools.org/hacker-tools/ascii-banner/
128
        // font: banner
129 1 1. byeByeBanner : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println(KaiserEnginePrinter.ANSI_RED);
130 1 1. byeByeBanner : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println("""
131
                ######                  ######                  #    #                              \s
132
                #     # #   # ######    #     # #   # ######    #   #    ##   #  ####  ###### ##### \s
133
                #     #  # #  #         #     #  # #  #         #  #    #  #  # #      #      #    #\s
134
                ######    #   #####     ######    #   #####     ###    #    # #  ####  #####  #    #\s
135
                #     #   #   #         #     #   #   #         #  #   ###### #      # #      ##### \s
136
                #     #   #   #         #     #   #   #         #   #  #    # # #    # #      #   # \s
137
                ######    #   ######    ######    #   ######    #    # #    # #  ####  ###### #    #\s
138
                                                                                                    \n
139
                """);
140 1 1. byeByeBanner : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println(KaiserEnginePrinter.ANSI_RESET);
141
    }
142
143
    /**
144
     * Shows the underlying status of the current round the player is in.
145
     */
146
    public void status() {
147 1 1. status : removed call to java/io/PrintStream::println → KILLED
        System.out.println(printer.getYearResult(engine.getYear()));
148 1 1. status : negated conditional → SURVIVED
        if (engine.isPlagueOccurred()) {
149 1 1. status : removed call to java/io/PrintStream::println → KILLED
            System.out.println(KaiserEnginePrinter.ORANGE
150
                    + "Eine fürchterliche Seuche hat die halbe Stadt dahingerafft!"
151
                    + KaiserEnginePrinter.ANSI_RESET);
152
        }
153 1 1. status : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println(printer.getStatus(engine.getYear()));
154
    }
155
156
    /**
157
     * Performs user interactions.
158
     * The player can either
159
     * <ul>
160
     *     <li>buy or sell land</li>
161
     *     <li>feed the population</li>
162
     *     <li>perform agricultural operations per round.</li>
163
     * </ul>
164
     */
165
    public void actions() {
166 1 1. actions : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println();
167 1 1. actions : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println(KaiserEnginePrinter.ANSI_PURPLE + "#+#+#+#+ Was möchten Sie tun?" + KaiserEnginePrinter.ANSI_RESET);
168
169
        // only buy or sell is allowed
170 2 1. actions : negated conditional → KILLED
2. actions : negated conditional → KILLED
        if (!KaiserActions.buy(engine, input) && !engine.isGameOver()) {
171 1 1. actions : removed call to de/aikiit/game/kaiser/KaiserActions::sell → KILLED
            KaiserActions.sell(engine, input);
172
        }
173 1 1. actions : negated conditional → KILLED
        if (!engine.isGameOver()) {
174 1 1. actions : removed call to de/aikiit/game/kaiser/KaiserActions::feed → KILLED
            KaiserActions.feed(engine, input);
175
        }
176 1 1. actions : negated conditional → KILLED
        if (!engine.isGameOver()) {
177 1 1. actions : removed call to de/aikiit/game/kaiser/KaiserActions::cultivate → KILLED
            KaiserActions.cultivate(engine, input);
178
        }
179
180 1 1. actions : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println();
181
    }
182
183
    /**
184
     * Shows results after the game is over.
185
     */
186
    public void finish() {
187 1 1. finish : removed call to java/io/PrintStream::println → SURVIVED
        System.out.println(printer.getResults());
188 1 1. finish : removed call to java/io/PrintStream::println → KILLED
        System.out.println(printer.evaluateRegency());
189
    }
190
191
    private String nextAnswer() {
192 2 1. nextAnswer : negated conditional → KILLED
2. nextAnswer : replaced return value with "" for de/aikiit/game/kaiser/KaiserGame::nextAnswer → KILLED
        return input.hasNext() ? input.next() : "n";
193
    }
194
}

Mutations

52

1.1
Location : run
Killed by : none
removed call to de/aikiit/game/kaiser/KaiserGame::intro → SURVIVED
Covering tests

53

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:startConfirmationCanStopBeforeFirstYear(java.lang.String)]/[test-template-invocation:#3]
removed call to java/io/PrintStream::println → KILLED

54

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:startConfirmationCanStopBeforeFirstYear(java.lang.String)]/[test-template-invocation:#3]
negated conditional → KILLED

55

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:startConfirmationCanStopBeforeFirstYear(java.lang.String)]/[test-template-invocation:#3]
removed call to de/aikiit/game/kaiser/KaiserEngine::abort → KILLED

58

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:negativeInputOrEofStopsRemainingActions(java.lang.String)]/[test-template-invocation:#5]
negated conditional → KILLED

59

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:negativeInputOrEofStopsRemainingActions(java.lang.String)]/[test-template-invocation:#5]
removed call to de/aikiit/game/kaiser/KaiserEngine::startNewRound → KILLED

60

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:finalReportStillAppliesImmigrationAndPlagueBeforeRating()]
removed call to de/aikiit/game/kaiser/KaiserGame::status → KILLED

61

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:negativeInputOrEofStopsRemainingActions(java.lang.String)]/[test-template-invocation:#5]
negated conditional → KILLED

64

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:starvationEndsGameImmediatelyWithoutFinalTenYearEvaluation()]
removed call to de/aikiit/game/kaiser/KaiserEngine::prepareLandTrading → KILLED

65

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:starvationEndsGameImmediatelyWithoutFinalTenYearEvaluation()]
removed call to java/io/PrintStream::println → KILLED

66

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:negativeInputOrEofStopsRemainingActions(java.lang.String)]/[test-template-invocation:#5]
removed call to de/aikiit/game/kaiser/KaiserGame::actions → KILLED

67

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:negativeInputOrEofStopsRemainingActions(java.lang.String)]/[test-template-invocation:#5]
negated conditional → KILLED

68

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:starvationEndsGameImmediatelyWithoutFinalTenYearEvaluation()]
removed call to de/aikiit/game/kaiser/KaiserEngine::finishRoundAfterActions → KILLED

72

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:starvationEndsGameImmediatelyWithoutFinalTenYearEvaluation()]
removed call to java/io/PrintStream::println → KILLED

73

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:negativeInputOrEofStopsRemainingActions(java.lang.String)]/[test-template-invocation:#5]
removed call to java/io/PrintStream::println → KILLED

75

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:finalReportStillAppliesImmigrationAndPlagueBeforeRating()]
removed call to de/aikiit/game/kaiser/KaiserGame::finish → KILLED

77

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:finalReportStillAppliesImmigrationAndPlagueBeforeRating()]
negated conditional → KILLED

78

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:tenActionYearsAreFollowedByEleventhReportAndThenEvaluation()]
removed call to java/io/PrintStream::println → KILLED

80

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:replayResetsGameAndStartsWithoutCodePrompt()]
negated conditional → KILLED

81

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:replayResetsGameAndStartsWithoutCodePrompt()]
removed call to de/aikiit/game/kaiser/KaiserEngine::reset → KILLED

87

1.1
Location : run
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:starvationEndsGameImmediatelyWithoutFinalTenYearEvaluation()]
negated conditional → KILLED

88

1.1
Location : run
Killed by : none
removed call to de/aikiit/game/kaiser/KaiserGame::byeByeBanner → SURVIVED
Covering tests

95

1.1
Location : intro
Killed by : none
removed call to de/aikiit/game/kaiser/KaiserGame::banner → SURVIVED
Covering tests

96

1.1
Location : intro
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

97

1.1
Location : intro
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

98

1.1
Location : intro
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

99

1.1
Location : intro
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

100

1.1
Location : intro
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

109

1.1
Location : banner
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

110

1.1
Location : banner
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

120

1.1
Location : banner
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

129

1.1
Location : byeByeBanner
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

130

1.1
Location : byeByeBanner
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

140

1.1
Location : byeByeBanner
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

147

1.1
Location : status
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:tenActionYearsAreFollowedByEleventhReportAndThenEvaluation()]
removed call to java/io/PrintStream::println → KILLED

148

1.1
Location : status
Killed by : none
negated conditional → SURVIVED
Covering tests

149

1.1
Location : status
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:finalReportStillAppliesImmigrationAndPlagueBeforeRating()]
removed call to java/io/PrintStream::println → KILLED

153

1.1
Location : status
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

166

1.1
Location : actions
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

167

1.1
Location : actions
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

170

1.1
Location : actions
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:finalReportStillAppliesImmigrationAndPlagueBeforeRating()]
negated conditional → KILLED

2.2
Location : actions
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:finalReportStillAppliesImmigrationAndPlagueBeforeRating()]
negated conditional → KILLED

171

1.1
Location : actions
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:finalReportStillAppliesImmigrationAndPlagueBeforeRating()]
removed call to de/aikiit/game/kaiser/KaiserActions::sell → KILLED

173

1.1
Location : actions
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:starvationEndsGameImmediatelyWithoutFinalTenYearEvaluation()]
negated conditional → KILLED

174

1.1
Location : actions
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:starvationEndsGameImmediatelyWithoutFinalTenYearEvaluation()]
removed call to de/aikiit/game/kaiser/KaiserActions::feed → KILLED

176

1.1
Location : actions
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:negativeInputOrEofStopsRemainingActions(java.lang.String)]/[test-template-invocation:#4]
negated conditional → KILLED

177

1.1
Location : actions
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:negativeInputOrEofStopsRemainingActions(java.lang.String)]/[test-template-invocation:#4]
removed call to de/aikiit/game/kaiser/KaiserActions::cultivate → KILLED

180

1.1
Location : actions
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

187

1.1
Location : finish
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

188

1.1
Location : finish
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[method:finalReportStillAppliesImmigrationAndPlagueBeforeRating()]
removed call to java/io/PrintStream::println → KILLED

192

1.1
Location : nextAnswer
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:startConfirmationCanStopBeforeFirstYear(java.lang.String)]/[test-template-invocation:#3]
negated conditional → KILLED

2.2
Location : nextAnswer
Killed by : de.aikiit.game.kaiser.KaiserGameTest.[engine:junit-jupiter]/[class:de.aikiit.game.kaiser.KaiserGameTest]/[test-template:startConfirmationCanStopBeforeFirstYear(java.lang.String)]/[test-template-invocation:#3]
replaced return value with "" for de/aikiit/game/kaiser/KaiserGame::nextAnswer → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.2 support