View Javadoc
1   package de.aikiit.game.kaiser;
2   
3   import org.junit.jupiter.api.Test;
4   import org.junit.jupiter.params.ParameterizedTest;
5   import org.junit.jupiter.params.provider.ValueSource;
6   
7   import java.io.ByteArrayOutputStream;
8   import java.io.PrintStream;
9   import java.nio.charset.StandardCharsets;
10  import java.util.ArrayList;
11  import java.util.List;
12  import java.util.Scanner;
13  
14  import static org.assertj.core.api.Assertions.assertThat;
15  
16  class KaiserGameTest {
17      @ParameterizedTest
18      @ValueSource(strings = {"n", "N", ""})
19      void startConfirmationCanStopBeforeFirstYear(String input) {
20          KaiserEngine engine = new KaiserEngine(new BasicRandom());
21          String output = run(engine, input);
22          assertThat(engine.getState()).isEqualTo(KaiserEngine.GameState.ABORTED);
23          assertThat(engine.getYear()).isZero();
24          assertThat(output).contains("Start (J/N)?");
25          assertThat(output).doesNotContain("Im Jahr 1", "Kode-Zahl", "20-53");
26      }
27  
28      @Test
29      void starvationEndsGameImmediatelyWithoutFinalTenYearEvaluation() {
30          BasicRandom random = new BasicRandom(0.0, 0.0, 0.0, 0.0, 0.5);
31          KaiserEngine engine = new KaiserEngine(random);
32          String output = run(engine, "j 0 0 0 0");
33          assertThat(engine.getState()).isEqualTo(KaiserEngine.GameState.DISMISSED);
34          assertThat(engine.getYear()).isEqualTo(1);
35          assertThat(output).contains("100 Menschen", "Versager des Jahres",
36                  "Ein Hektar Land kostet 17 dt Getreide.", "Wieviele dt wollen Sie an Ihr Volk verteilen?");
37          assertThat(output).doesNotContain("DZ", "dzt");
38          assertThat(output).doesNotContain("Im Jahr 2", "Bewertung Ihrer Herrschaft", "Nochmal", "Kode-Zahl", "20-53");
39          assertThat(random.remaining()).isZero();
40      }
41  
42      @ParameterizedTest
43      @ValueSource(strings = {"-1", "0 -1", "0 0 -1", "0 0 2000 -1", ""})
44      void negativeInputOrEofStopsRemainingActions(String actions) {
45          KaiserEngine engine = new KaiserEngine(new BasicRandom(0.0));
46          String output = run(engine, "j " + actions);
47          assertThat(engine.getState()).isEqualTo(KaiserEngine.GameState.ABORTED);
48          assertThat(engine.getYear()).isEqualTo(1);
49          assertThat(output).contains("Das kann man nicht mit mir machen");
50          assertThat(output).doesNotContain("Bewertung Ihrer Herrschaft");
51      }
52  
53      @Test
54      void tenActionYearsAreFollowedByEleventhReportAndThenEvaluation() {
55          BasicRandom random = tenYearRandom();
56          KaiserEngine engine = new KaiserEngine(random);
57          String output = run(engine, tenYearInput() + "n");
58          assertThat(engine.getState()).isEqualTo(KaiserEngine.GameState.COMPLETED);
59          assertThat(engine.getYear()).isEqualTo(11);
60          assertThat(engine.getRegency()).isEqualTo(KaiserEngine.Regency.MEDIOCRE);
61          assertThat(engine.getTotalStarvationDeaths()).isZero();
62          assertThat(engine.getPopulation()).isEqualByComparingTo("137");
63          assertThat(engine.getLandArea()).isEqualByComparingTo("1330");
64          assertThat(engine.getGrainStocks()).isEqualByComparingTo("26050");
65          assertThat(engine.getImmigrants()).isEqualByComparingTo("4");
66          assertThat(output.split("Wieviel Land wollen Sie kaufen", -1)).hasSize(11);
67          assertThat(output).contains("Im Jahr 11", "glücklicher", "Nochmal (J/N)?");
68          assertThat(output.indexOf("Im Jahr 11")).isLessThan(output.indexOf("Bewertung Ihrer Herrschaft"));
69          assertThat(output).doesNotContain("Im Jahr 0", "11-jährigen Amtszeit");
70          assertThat(random.remaining()).isEqualTo(1); // No land-price roll without a replay.
71      }
72  
73      @Test
74      void finalReportStillAppliesImmigrationAndPlagueBeforeRating() {
75          List<Double> values = new ArrayList<>();
76          for (int year = 1; year <= 10; year++) {
77              values.addAll(List.of(0.0, 0.99, 0.0, 0.0, year == 10 ? 0.0 : 0.99));
78          }
79          BasicRandom random = new BasicRandom(values.toArray(Double[]::new));
80          KaiserEngine engine = new KaiserEngine(random);
81          String output = run(engine, tenYearInput());
82          assertThat(engine.getYear()).isEqualTo(11);
83          assertThat(engine.getPopulation()).isEqualByComparingTo("68"); // INT((133+4)/2).
84          assertThat(engine.getTotalStarvationDeaths()).isZero();
85          assertThat(engine.getRegency()).isEqualTo(KaiserEngine.Regency.EXCELLENT);
86          assertThat(output).contains("fürchterliche Seuche", "wunderbare Regierungszeit");
87          assertThat(output).doesNotContain("Nochmal");
88          assertThat(random.remaining()).isZero();
89      }
90  
91      @Test
92      void replayResetsGameAndStartsWithoutCodePrompt() {
93          KaiserEngine engine = new KaiserEngine(tenYearRandom());
94          String output = run(engine, tenYearInput() + "j j -1");
95          assertThat(engine.getState()).isEqualTo(KaiserEngine.GameState.ABORTED);
96          assertThat(engine.getYear()).isEqualTo(1);
97          assertThat(engine.getPopulation()).isEqualByComparingTo("100");
98          assertThat(engine.getLandArea()).isEqualByComparingTo("1000");
99          assertThat(engine.getGrainStocks()).isEqualByComparingTo("2800");
100         assertThat(output.split("Start \\(J/N\\)\\?", -1)).hasSize(3);
101         assertThat(output).doesNotContain("Kode-Zahl", "20-53");
102     }
103 
104     private static BasicRandom tenYearRandom() {
105         List<Double> values = new ArrayList<>();
106         for (int year = 1; year <= 10; year++) {
107             // Price 17, yield 5, no rats, factor 1, no plague.
108             values.addAll(List.of(0.0, 0.99, 0.0, 0.0, 0.99));
109         }
110         values.add(0.5); // Line 961: residents unhappy with the mediocre reign.
111         values.add(0.0); // Land price if the player chooses to play again.
112         return new BasicRandom(values.toArray(Double[]::new));
113     }
114 
115     /** A solvent ten-year strategy; keep roughly ten hectares per person. */
116     private static String tenYearInput() {
117         StringBuilder input = new StringBuilder("j ");
118         long population = 100;
119         long area = 1000;
120         long stocks = 2800;
121         for (int year = 1; year <= 10; year++) {
122             long buy = population * 10 - area;
123             input.append(buy).append(' ');
124             if (buy == 0) {
125                 input.append("0 "); // Selling is offered only after buying zero.
126             }
127             stocks -= buy * 17;
128             area += buy;
129             long food = population * 20;
130             long planted = area - 1; // BASIC's worker limit is strict.
131             input.append(food).append(' ').append(planted).append(' ');
132             stocks = stocks - food - planted / 2 + planted * 5;
133             population += (20 * area + stocks) / population / 100 + 1;
134         }
135         return input.toString();
136     }
137 
138     private static String run(KaiserEngine engine, String input) {
139         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
140         PrintStream original = System.out;
141         try (PrintStream output = new PrintStream(bytes, true, StandardCharsets.UTF_8)) {
142             System.setOut(output);
143             new KaiserGame(engine, new Scanner(input)).run();
144         } finally {
145             System.setOut(original);
146         }
147         return bytes.toString(StandardCharsets.UTF_8);
148     }
149 }