View Javadoc
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              intro();
53              System.out.println("Start (J/N)?");
54              if ("n".equalsIgnoreCase(nextAnswer())) {
55                  engine.abort();
56                  break;
57              }
58              while (!engine.isGameOver()) {
59                  engine.startNewRound();
60                  status();
61                  if (engine.isGameOver()) {
62                      break; // BASIC line 270: final report, but no eleventh action phase.
63                  }
64                  engine.prepareLandTrading();
65                  System.out.println("Ein Hektar Land kostet " + engine.getLandPrice() + " dt Getreide.");
66                  actions();
67                  if (!engine.isGameOver()) {
68                      engine.finishRoundAfterActions();
69                  }
70              }
71              switch (engine.getState()) {
72                  case DISMISSED -> System.out.println(printer.getDismissal());
73                  case ABORTED -> System.out.println("Das kann man nicht mit mir machen! Suchen Sie sich einen anderen zum Mitspielen!");
74                  case COMPLETED -> {
75                      finish();
76                      // Only the mediocre branch reaches the replay prompt in lines 959–966.
77                      if (engine.getRegency() == KaiserEngine.Regency.MEDIOCRE) {
78                          System.out.println("Nochmal (J/N)?");
79                          playAgain = "j".equalsIgnoreCase(nextAnswer());
80                          if (playAgain) {
81                              engine.reset();
82                          }
83                      }
84                  }
85                  default -> throw new IllegalStateException("Game ended without a final state.");
86              }
87          } while (playAgain);
88          byeByeBanner();
89      }
90  
91      /**
92       * Shows information about the game flow and welcomes the player.
93       */
94      public void intro() {
95          banner();
96          System.out.println("Versuchen Sie die antike Stadt " + KaiserEnginePrinter.ANSI_YELLOW + ">SUMERIA<" + KaiserEnginePrinter.ANSI_RESET + " zu regieren!");
97          System.out.println("Ihre Regierungszeit beträgt " + MAX_ROUNDS + " Jahre.");
98          System.out.println("Nach jeweils einem Jahr erhalten Sie einen Bericht über die Entwicklung in der Stadt.");
99          System.out.println("Dann werden wir weitersehen .......");
100         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         System.out.println(KaiserEnginePrinter.ANSI_RED);
110         System.out.println("""
111                 #    #                              \s
112                 #   #    ##   #  ####  ###### ##### \s
113                 #  #    #  #  # #      #      #    #\s
114                 ###    #    # #  ####  #####  #    #\s
115                 #  #   ###### #      # #      ##### \s
116                 #   #  #    # # #    # #      #   # \s
117                 #    # #    # #  ####  ###### #    #\s
118                                                     \s
119                 """);
120         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         System.out.println(KaiserEnginePrinter.ANSI_RED);
130         System.out.println("""
131                 ######                  ######                  #    #                              \s
132                 #     # #   # ######    #     # #   # ######    #   #    ##   #  ####  ###### ##### \s
133                 #     #  # #  #         #     #  # #  #         #  #    #  #  # #      #      #    #\s
134                 ######    #   #####     ######    #   #####     ###    #    # #  ####  #####  #    #\s
135                 #     #   #   #         #     #   #   #         #  #   ###### #      # #      ##### \s
136                 #     #   #   #         #     #   #   #         #   #  #    # # #    # #      #   # \s
137                 ######    #   ######    ######    #   ######    #    # #    # #  ####  ###### #    #\s
138                                                                                                     \n
139                 """);
140         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         System.out.println(printer.getYearResult(engine.getYear()));
148         if (engine.isPlagueOccurred()) {
149             System.out.println(KaiserEnginePrinter.ORANGE
150                     + "Eine fürchterliche Seuche hat die halbe Stadt dahingerafft!"
151                     + KaiserEnginePrinter.ANSI_RESET);
152         }
153         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         System.out.println();
167         System.out.println(KaiserEnginePrinter.ANSI_PURPLE + "#+#+#+#+ Was möchten Sie tun?" + KaiserEnginePrinter.ANSI_RESET);
168 
169         // only buy or sell is allowed
170         if (!KaiserActions.buy(engine, input) && !engine.isGameOver()) {
171             KaiserActions.sell(engine, input);
172         }
173         if (!engine.isGameOver()) {
174             KaiserActions.feed(engine, input);
175         }
176         if (!engine.isGameOver()) {
177             KaiserActions.cultivate(engine, input);
178         }
179 
180         System.out.println();
181     }
182 
183     /**
184      * Shows results after the game is over.
185      */
186     public void finish() {
187         System.out.println(printer.getResults());
188         System.out.println(printer.evaluateRegency());
189     }
190 
191     private String nextAnswer() {
192         return input.hasNext() ? input.next() : "n";
193     }
194 }