1 package de.aikiit.game.kaiser;
2
3 import java.util.Objects;
4 import java.util.Scanner;
5
6
7
8
9
10
11
12
13
14
15 public class KaiserGame {
16
17
18
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
28
29 public KaiserGame() {
30 this(new KaiserEngine(), new Scanner(System.in));
31 }
32
33
34
35
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
45
46
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;
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
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
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
105
106 public void banner() {
107
108
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
125
126 public void byeByeBanner() {
127
128
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
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
158
159
160
161
162
163
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
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
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 }