View Javadoc
1   package de.aikiit.game.kaiser;
2   
3   import org.assertj.core.util.VisibleForTesting;
4   
5   /**
6    * This class encapsulates the actual game flow.
7    * A player
8    * <ul>
9    *     <li>is shown current statistics and</li>
10   *     <li>can start to act</li>
11   *     <li>until the maximum number of rounds is reached and the game ends.</li>
12   * </ul>
13   */
14  public class KaiserGame {
15  
16      /**
17       * The player can play at most this number of rounds.
18       */
19      public static final int MAX_ROUNDS = 10;
20      private final KaiserEngine engine;
21      private final KaiserEnginePrinter printer;
22      /**
23       * The current round of the game.
24       */
25      private int round = 0;
26  
27      public KaiserGame() {
28          this(new KaiserEngine());
29      }
30  
31      @VisibleForTesting
32      KaiserGame(KaiserEngine engine) {
33          this.engine = engine;
34          this.printer = new KaiserEnginePrinter(this.engine);
35      }
36  
37      private void incrementRoundCounter() {
38          round++;
39      }
40  
41      /**
42       * Start the game and allow player interactions until the maximum number of rounds is reached.
43       */
44      public void run() {
45          intro();
46          status();
47          while (round < MAX_ROUNDS) {
48              incrementRoundCounter();
49              engine.startNewRound();
50              status();
51              actions();
52              engine.finishRoundAfterActions();
53          }
54  
55          finish();
56          byeByeBanner();
57      }
58  
59      /**
60       * Shows information about the game flow and welcomes the player.
61       */
62      public void intro() {
63          banner();
64          System.out.println("Versuchen Sie die antike Stadt " + KaiserEnginePrinter.ANSI_YELLOW + ">SUMERIA<" + KaiserEnginePrinter.ANSI_RESET + " zu regieren!");
65          System.out.println("Ihre Regierungszeit beträgt " + MAX_ROUNDS + " Jahre.");
66          System.out.println("Nach jeweils einem Jahr erhalten Sie einen Bericht über die Entwicklung in der Stadt.");
67          System.out.println("Dann werden wir weitersehen .......");
68          System.out.println();
69      }
70  
71      /**
72       * Shows the game's banner.
73       */
74      public void banner() {
75          // generated with the help of https://manytools.org/hacker-tools/ascii-banner/
76          // font: banner
77          System.out.println(KaiserEnginePrinter.ANSI_RED);
78          System.out.println("""
79                  #    #                              \s
80                  #   #    ##   #  ####  ###### ##### \s
81                  #  #    #  #  # #      #      #    #\s
82                  ###    #    # #  ####  #####  #    #\s
83                  #  #   ###### #      # #      ##### \s
84                  #   #  #    # # #    # #      #   # \s
85                  #    # #    # #  ####  ###### #    #\s
86                                                      \s
87                                                                      """);
88          System.out.println(KaiserEnginePrinter.ANSI_RESET);
89      }
90  
91      /**
92       * Shows console banner after the game is over.
93       */
94      public void byeByeBanner() {
95          // generated with the help of https://manytools.org/hacker-tools/ascii-banner/
96          // font: banner
97          System.out.println(KaiserEnginePrinter.ANSI_RED);
98          System.out.println("""
99                  ######                  ######                  #    #                              \s
100                 #     # #   # ######    #     # #   # ######    #   #    ##   #  ####  ###### ##### \s
101                 #     #  # #  #         #     #  # #  #         #  #    #  #  # #      #      #    #\s
102                 ######    #   #####     ######    #   #####     ###    #    # #  ####  #####  #    #\s
103                 #     #   #   #         #     #   #   #         #  #   ###### #      # #      ##### \s
104                 #     #   #   #         #     #   #   #         #   #  #    # # #    # #      #   # \s
105                 ######    #   ######    ######    #   ######    #    # #    # #  ####  ###### #    #\s
106                                                                                                     \n                                                                           """);
107         System.out.println(KaiserEnginePrinter.ANSI_RESET);
108     }
109 
110     /**
111      * Shows the underlying status of the current round the player is in.
112      */
113     public void status() {
114         System.out.println(printer.getStatus(round));
115         System.out.println(printer.getYearResult(round));
116     }
117 
118     /**
119      * Performs user interactions.
120      * The player can either
121      * <ul>
122      *     <li>buy or sell land</li>
123      *     <li>feed the population</li>
124      *     <li>perform agricultural operations per round.</li>
125      * </ul>
126      */
127     public void actions() {
128         System.out.println();
129         System.out.println(KaiserEnginePrinter.ANSI_PURPLE + "#+#+#+#+ Was möchten Sie tun?" + KaiserEnginePrinter.ANSI_RESET);
130 
131         // only buy or sell is allowed
132         if (!KaiserActions.buy(this.engine)) {
133             KaiserActions.sell(this.engine);
134         }
135 
136         KaiserActions.feed(this.engine);
137         KaiserActions.cultivate(this.engine);
138 
139         System.out.println();
140     }
141 
142     /**
143      * Shows results after the game is over.
144      */
145     public void finish() {
146         System.out.println(printer.getResults());
147         System.out.println(printer.evaluateRegency());
148     }
149 }