View Javadoc
1   package de.aikiit.game.kaiser;
2   
3   import java.util.InputMismatchException;
4   import java.util.Scanner;
5   
6   /**
7    * This class contains all available actions that have to be taken by the player of the game.
8    * At the moment this is done via the operating system's console.
9    */
10  public class KaiserActions {
11  
12      /**
13       * Try to buy land in the current game.
14       *
15       * @param engine the current game engine
16       * @return {@code true} if the buy action succeeded, {@code false} otherwise.
17       */
18      public static boolean buy(KaiserEngine engine) {
19          while (true) {
20              try {
21                  System.out.println(KaiserEnginePrinter.ANSI_PURPLE + "Wieviel Land wollen Sie kaufen? 0 = nichts" + KaiserEnginePrinter.ANSI_RESET);
22                  Long buy = new Scanner(System.in).nextLong();
23                  return engine.buyLand(buy) > 0;
24              } catch (InputMismatchException e) {
25                  System.out.print(KaiserEnginePrinter.ANSI_PURPLE);
26                  System.out.println("Gib einen richtigen Wert ein, Du Knalltüte!");
27                  System.out.print(KaiserEnginePrinter.ANSI_RESET);
28              } catch (IllegalArgumentException e) {
29                  System.out.print(KaiserEnginePrinter.ANSI_PURPLE);
30                  System.out.println("Überleg doch mal, Du hast nur " + engine.getSupplies() + " Korn zur Verfügung. Nun denn, versuch es nochmals.");
31                  System.out.print(KaiserEnginePrinter.ANSI_RESET);
32              }
33          }
34      }
35  
36      /**
37       * Perform a sell operation in the current game.
38       *
39       * @param engine the current game engine
40       */
41      public static void sell(KaiserEngine engine) {
42          while (true) {
43              try {
44                  System.out.println(KaiserEnginePrinter.ANSI_PURPLE + "Wieviel Land wollen Sie verkaufen?" + KaiserEnginePrinter.ANSI_RESET);
45                  Long sell = new Scanner(System.in).nextLong();
46                  engine.sellLand(sell);
47                  return;
48              } catch (InputMismatchException e) {
49                  System.out.print(KaiserEnginePrinter.ANSI_PURPLE);
50                  System.out.println("Gib einen richtigen Wert ein, Du Knalltüte!");
51                  System.out.print(KaiserEnginePrinter.ANSI_RESET);
52              } catch (IllegalArgumentException e) {
53                  System.out.print(KaiserEnginePrinter.ANSI_PURPLE);
54                  System.out.println("Überleg doch mal, Du hast nur " + engine.getArea() + " Hektar Land. Probier es erneut.");
55                  System.out.print(KaiserEnginePrinter.ANSI_RESET);
56              }
57          }
58      }
59  
60      /**
61       * Perform a feeding operation in the current game, that means give food to the population.
62       *
63       * @param engine the current game engine
64       */
65      public static void feed(KaiserEngine engine) {
66          while (true) {
67              try {
68                  System.out.println(KaiserEnginePrinter.ANSI_PURPLE + "Wieviel dzt wollen Sie an Ihr Volk verteilen?" + KaiserEnginePrinter.ANSI_RESET);
69                  Long feed = new Scanner(System.in).nextLong();
70                  engine.feedToPopulation(feed);
71                  return;
72              } catch (InputMismatchException e) {
73                  System.out.print(KaiserEnginePrinter.ANSI_PURPLE);
74                  System.out.println("Gib einen richtigen Wert ein, Du Knalltüte!");
75                  System.out.print(KaiserEnginePrinter.ANSI_RESET);
76              } catch (IllegalArgumentException e) {
77                  System.out.print(KaiserEnginePrinter.ANSI_PURPLE);
78                  System.out.println("Überleg doch mal, Du hast nur " + engine.getSupplies() + " Korn zur Verfügung. Nun denn, probier es erneut.");
79                  System.out.print(KaiserEnginePrinter.ANSI_RESET);
80              }
81          }
82      }
83  
84      /**
85       * In order to feed your population you need to plant crops and cultivate your available area/land.
86       *
87       * @param engine the current game engine
88       */
89      public static void cultivate(KaiserEngine engine) {
90          while (true) {
91              try {
92                  System.out.println(KaiserEnginePrinter.ANSI_PURPLE + "Wieviel Land wollen Sie bebauen?" + KaiserEnginePrinter.ANSI_RESET);
93                  Long cultivate = new Scanner(System.in).nextLong();
94                  engine.cultivate(cultivate);
95                  return;
96              } catch (InputMismatchException e) {
97                  System.out.print(KaiserEnginePrinter.ANSI_PURPLE);
98                  System.out.println("Gib einen richtigen Wert ein, Du Knalltüte!");
99                  System.out.print(KaiserEnginePrinter.ANSI_RESET);
100             } catch (IllegalArgumentException e) {
101                 System.out.print(KaiserEnginePrinter.ANSI_PURPLE);
102                 switch (e.getMessage()) {
103                     case "You cannot cultivate more area than you have." ->
104                             System.out.println("Überleg doch mal, Du hast nur " + engine.getArea() + " Hektar Land. Probier es nochmals.");
105                     case "You cannot cultivate more than you have." ->
106                             System.out.println("Nun denk doch mal nach, Du hast nur " + engine.getSupplies() + " Korn zur Verfügung. Nun denn, versuche es erneut.");
107                     case "Not enough workers available." ->
108                             System.out.println("Sie haben aber nur " + engine.getPopulation() + " Arbeiter. Noch einmal.");
109                 }
110                 System.out.print(KaiserEnginePrinter.ANSI_RESET);
111             }
112         }
113     }
114 }