| 1 | package de.aikiit.game.kaiser; | |
| 2 | ||
| 3 | import java.math.BigDecimal; | |
| 4 | import java.util.Locale; | |
| 5 | import java.util.Scanner; | |
| 6 | import java.util.function.Consumer; | |
| 7 | ||
| 8 | /** | |
| 9 | * Handles the console dialogue for buying and selling land, feeding the population | |
| 10 | * and planting crops. Each action requests a quantity and delegates validation and | |
| 11 | * changes to the game state to {@link KaiserEngine}. | |
| 12 | * <p> | |
| 13 | * Invalid numbers and quantities rejected by the engine produce an explanation and | |
| 14 | * another prompt. Negative quantities end the game according to the BASIC rules; | |
| 15 | * exhausted input also aborts the game rather than prompting indefinitely. | |
| 16 | * All actions use the caller's shared scanner so buffered input is preserved between | |
| 17 | * prompts. The scanner is not closed here because its lifetime belongs to the caller. | |
| 18 | */ | |
| 19 | public final class KaiserActions { | |
| 20 | private KaiserActions() { | |
| 21 | } | |
| 22 | ||
| 23 | /** | |
| 24 | * Asks how many hectares to buy and retries until the engine accepts the quantity | |
| 25 | * or the game is aborted. A positive purchase spends grain at the current land | |
| 26 | * price; entering zero leaves stocks unchanged and allows the caller to offer | |
| 27 | * selling land instead. | |
| 28 | * | |
| 29 | * @param engine game whose land and grain stocks are updated | |
| 30 | * @param input shared console scanner; decimal quantities use a point | |
| 31 | * @return {@code true} after an accepted positive purchase; {@code false} for | |
| 32 | * zero, a negative quantity or exhausted input. The caller must also | |
| 33 | * check {@link KaiserEngine#isGameOver()} before offering another action. | |
| 34 | */ | |
| 35 | public static boolean buy(KaiserEngine engine, Scanner input) { | |
| 36 |
3
1. buy : replaced boolean return with true for de/aikiit/game/kaiser/KaiserActions::buy → KILLED 2. buy : negated conditional → KILLED 3. buy : changed conditional boundary → KILLED |
return perform(engine, input, "Wieviel Land wollen Sie kaufen? 0 = nichts", engine::buyLand).signum() > 0; |
| 37 | } | |
| 38 | ||
| 39 | /** | |
| 40 | * Asks how many hectares to sell in exchange for grain at the current land price. | |
| 41 | * Selling all owned land or more is rejected with a new prompt; zero is accepted | |
| 42 | * without changing ownership or stocks. A negative quantity or exhausted input | |
| 43 | * aborts the game and returns control to the caller. | |
| 44 | * | |
| 45 | * @param engine game whose land and grain stocks are updated | |
| 46 | * @param input shared console scanner; decimal quantities use a point | |
| 47 | */ | |
| 48 | public static void sell(KaiserEngine engine, Scanner input) { | |
| 49 | perform(engine, input, "Wieviel Land wollen Sie verkaufen?", engine::sellLand); | |
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * Asks how much stored grain to distribute to the population. The accepted amount | |
| 54 | * is removed from stocks and retained by the engine for the later starvation | |
| 55 | * calculation; this dialogue does not immediately change the population. | |
| 56 | * Allocating more grain than is available causes another prompt. Zero is valid, | |
| 57 | * while a negative quantity or exhausted input aborts the game. | |
| 58 | * | |
| 59 | * @param engine game whose stocks and food allocation are updated | |
| 60 | * @param input shared console scanner; quantities are in dt of grain | |
| 61 | */ | |
| 62 | public static void feed(KaiserEngine engine, Scanner input) { | |
| 63 | perform(engine, input, "Wieviele dt wollen Sie an Ihr Volk verteilen?", engine::feedToPopulation); | |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Asks how many hectares to plant and delegates seed consumption, harvest and | |
| 68 | * rat losses to the engine. Rejected quantities produce an explanation about | |
| 69 | * available land, seed grain or workers before asking again. | |
| 70 | * Entering zero still processes the season's harvest and possible rat losses; | |
| 71 | * a negative quantity or exhausted input aborts the game instead. | |
| 72 | * | |
| 73 | * @param engine game whose planting and harvest are processed | |
| 74 | * @param input shared console scanner; quantities are in hectares | |
| 75 | */ | |
| 76 | public static void cultivate(KaiserEngine engine, Scanner input) { | |
| 77 | perform(engine, input, "Wieviel Land wollen Sie bebauen?", engine::cultivate); | |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * Runs the common prompt/read/validate loop for one action. Numeric input uses | |
| 82 | * a locale-independent decimal point. Non-numeric tokens are consumed and | |
| 83 | * retried; resource validation errors from the engine are translated into | |
| 84 | * player-facing messages. Negative numbers are passed through to the engine, | |
| 85 | * which owns the BASIC abort rule. | |
| 86 | * | |
| 87 | * @param engine game used for resource explanations and end-of-input handling | |
| 88 | * @param input shared scanner, whose locale is set to {@link Locale#ROOT} | |
| 89 | * @param prompt question displayed before each attempt | |
| 90 | * @param action engine operation that validates and applies the entered quantity | |
| 91 | * @return accepted quantity, including a negative abort request, or zero when | |
| 92 | * input ends; the game state distinguishes an abort from a valid zero | |
| 93 | */ | |
| 94 | private static BigDecimal perform(KaiserEngine engine, Scanner input, String prompt, Consumer<BigDecimal> action) { | |
| 95 | input.useLocale(Locale.ROOT); // BASIC uses a decimal point, independent of the host locale. | |
| 96 | while (true) { | |
| 97 |
1
1. perform : removed call to java/io/PrintStream::println → KILLED |
System.out.println(KaiserEnginePrinter.ANSI_PURPLE + prompt + KaiserEnginePrinter.ANSI_RESET); |
| 98 |
1
1. perform : negated conditional → KILLED |
if (!input.hasNextBigDecimal()) { |
| 99 |
1
1. perform : negated conditional → KILLED |
if (!input.hasNext()) { |
| 100 |
1
1. perform : removed call to de/aikiit/game/kaiser/KaiserEngine::abort → KILLED |
engine.abort(); |
| 101 |
1
1. perform : replaced return value with null for de/aikiit/game/kaiser/KaiserActions::perform → KILLED |
return BigDecimal.ZERO; |
| 102 | } | |
| 103 | input.next(); | |
| 104 |
1
1. perform : removed call to java/io/PrintStream::println → SURVIVED |
System.out.println("Bitte eine Zahl eingeben (Dezimalpunkt statt Komma)."); |
| 105 | continue; | |
| 106 | } | |
| 107 | BigDecimal amount = input.nextBigDecimal(); | |
| 108 | try { | |
| 109 |
1
1. perform : removed call to java/util/function/Consumer::accept → KILLED |
action.accept(amount); |
| 110 |
1
1. perform : replaced return value with null for de/aikiit/game/kaiser/KaiserActions::perform → KILLED |
return amount; |
| 111 | } catch (IllegalArgumentException e) { | |
| 112 | switch (e.getMessage()) { | |
| 113 | case "Not Enough Land", "You cannot cultivate more area than you have." -> | |
| 114 |
1
1. perform : removed call to java/io/PrintStream::println → SURVIVED |
System.out.println("Überlegen Sie doch, Sie haben nur " + engine.getLandArea() + " Hektar Land."); |
| 115 | case "Not enough workers available." -> | |
| 116 |
1
1. perform : removed call to java/io/PrintStream::println → NO_COVERAGE |
System.out.println("Sie haben aber nur " + engine.getPopulation() + " Arbeiter. Noch einmal."); |
| 117 |
1
1. perform : removed call to java/io/PrintStream::println → SURVIVED |
default -> System.out.println("Nun denken Sie doch mal nach, Sie haben nur " |
| 118 | + engine.getGrainStocks() + " Korn zur Verfügung. Nun denn."); | |
| 119 | } | |
| 120 | } | |
| 121 | } | |
| 122 | } | |
| 123 | } | |
Mutations | ||
| 36 |
1.1 2.2 3.3 |
|
| 97 |
1.1 |
|
| 98 |
1.1 |
|
| 99 |
1.1 |
|
| 100 |
1.1 |
|
| 101 |
1.1 |
|
| 104 |
1.1 |
|
| 109 |
1.1 |
|
| 110 |
1.1 |
|
| 114 |
1.1 |
|
| 116 |
1.1 |
|
| 117 |
1.1 |