# Programmieren Tutorium ## Liam Wachter ### 10. Februar 2022 --- # Blatt 5 --- ## Gut so? ``` . └── edu └── kit └── informatik ├── FightEnum.java ├── Godfavor.java ├── Orlog.java ├── Phases.java ├── Player.java └── Utils.java ``` ---- ## Vorteile? ```plantuml @startuml package "ui" #00DDDD { package "commands" { } package "resources" { } } package "model" #FFDDDD{ package "evaluator" #FFD0DD{ package "godfavor" {} } package "excpetion" {} } @enduml ``` --- ## Gut so? ```java do { switch (command) { case COMMAND_PRINT: try { isValid(input); print(); } catch (IllegalArgumentException e) { System.err.println(UNKNOWN_UNSUPPORTED_COMMAND); } break; case COMMAND_ROLL: try { isValid(input); roll(parameters); } catch (IllegalArgumentException e) { System.err.println(START_GAME); } break; // ... default: System.err.println(COMMAND_NOT_FOUND); break; } } while (!quit); ``` ---- ## Besser ### Mehr Struktur ![](https://i.imgur.com/TEHgaTB.png) ---- ### Weniger duplizierter Code ```java public abstract class InGameCommand extends Command { @Override protected void execute(List<String> arguments) { try { this.executeInGameCommand(arguments); } catch (GameStateException e) { System.err.println(ErrorMessage.ILLEGAL_GAME_STATE.toString()); } } // ... protected abstract void executeInGameCommand(List<String> arguments) throws GameStateException; } ``` ---- ### Übersichtlicher, Wartbarer ```java public class Evaluate extends InGameCommand { private static final String REGEX = "evaluate"; private static final int NUMBER_OF_PARAMETERS = 0; // ... ``` ---- ### Wartbarer ```java Session session = new Session(); session.addCommand(new Print(game.get())); session.addCommand(new Roll(game.get())); session.addCommand(new Turn(game.get())); session.addCommand(new SelectGodFavor(game.get())); session.addCommand(new Evaluate(game.get())); session.addCommand(new Quit(session)); session.start(); ``` --- ## Gut so? ```java switch(this.GameState) { case ROLL: // ... case SELECT_AND_EVALUATE: // ... case GAME_OVER: // ... } ``` ---- ![](https://i.imgur.com/rjxKcdH.png =900x) ---- ![](https://i.imgur.com/eBGtzVh.png) --- ## Gut so? ```java public final class Session { private static final String MESSAGE_START_GAME = "Error, Please start a new game."; private static final String MESSAGE_INVALID_INPUT = "Error, You entered invalid information"; private static final String MESSAGE_UNSUPPORTED_COMMAND = "Error, this command can not be executed"; private static final String MESSAGE_COMMAND_NOT_FOUND = "Error, Command not found"; public void method1(); public void method2(); // ... } ``` ---- ## Besser. Warum? ```java /** * All error messages that are shown to the user. */ public enum ErrorMessage { /** * The error message printed if a player enters an blank string as command. */ EMPTY_COMMAND("please enter a command."), /** * The error message printed if the command was not found. * * Expects one format argument: * The command name entered by the user (string). */ COMMAND_NOT_FOUND("there is no command called '%s'."), /** * The error message printed if a player entered a command that could not be parsed. */ ILLEGAL_FORMAT("the input could not be parsed."), /** * The error message printed if the command did not have the expected number of arguments. * * Expects two format arguments in the following order: * The number of expected arguments (integer) and * the number of provided arguments (integer). */ ILLEGAL_ARGUMENT_COUNT("expected %d argument(s) but got %d."), /** * The error message printed if an illegal player name was entered. * * Expects one format argument: * The illegal player name (string). */ ILLEGAL_NAME("'%s' is not a legal name. Must be not empty and contain no whitespace."), /** * The error message printed if an illegal health count was entered. * * Expects one format argument: * The illegal health count (integer). */ ILLEGAL_HEALTH("%d is not a legal initial health count."), /** * The error message printed if an illegal token count was entered. * * Expects one format argument: * The illegal token count (integer). */ ILLEGAL_TOKEN_COUNT("%d is not a legal initial token count."), /** * The error message printed if a string could not be parsed to an integer. * * Expects one format argument: * The illegal string. */ ILLEGAL_INTEGER("cannot parse %s into an integer."), /** * The error message printed if a string could not be parsed to a die. * * Expects one format argument: * The illegal string. */ ILLEGAL_DIE("cannot parse %s into a die."), /** * The error message printed if rolled dice yield to much tokens. * * Expects two format arguments int the following order: * The maximum number of tokens (integer) and * the illegal number of tokens (integer). */ ILLEGAL_ROLL_TOKEN_COUNT("a maximum of %d dice are allowed to yield tokens, but got %d."), /** * The error message printed if a string could not be parsed to a god favor. * * Expects one format argument: * The illegal string. */ ILLEGAL_GOD_FAVOR("cannot parse %s into a god favor."), /** * The error message printed if a integer is not a legal god favor level. * * Expects three format arguments int the following order: * The illegal god favor level (integer), * the lowest possible level (integer) and * the highest possible level (integer). */ ILLEGAL_GOD_FAVOR_LEVEL("cannot parse %d into a god favor level. Expected an integer between %d and %d."), /** * The error message printed if the executed command cannot be executed in the current game state. */ ILLEGAL_GAME_STATE("cannot execute this command in the current game state."); private static final String PREFIX = "Error, "; private final String message; private ErrorMessage(String message) { this.message = message; } /** * Formats this message with the specified arguments. * * Calls {@link String#format(String, Object...)} internally. * * @param args arguments referenced by the format specifiers in the format string * @return the formatted string */ public String format(Object... args) { return PREFIX + String.format(this.message, args); } @Override public String toString() { return PREFIX + this.message; } } ``` <!--- ## Gut so? ```java public boolean evaluate() throws IllegalArgumentException { if (gameOver == false && evaluateLegal == true) { evaluateDice(playerTwo, playerOne); evaluateDice(playerOne, playerTwo); checkGameOver(); if (!gameOver) { if (playerOne != null || playerOne.getPickedGodFavor().getName().equals("TT")) { evaluateGodFavors(playerOne, playerTwo); evaluateGodFavors(playerTwo, playerOne); } else { evaluateGodFavors(playerTwo, playerOne); evaluateGodFavors(playerOne, playerTwo); } checkGameOver(); } if (gameOver == true) { return true; } else { System.out.print(printPlayers()); turnLegal = true; evaluateLegal = false; phase = resolutionPhase; ArrayList<DieFace> emptyList = new ArrayList<>(); playerOne.setDice(emptyList); playerTwo.setDice(emptyList); playerOne.setTimesToRoll(0); playerTwo.setTimesToRoll(0); return false; } } else { throw new IllegalArgumentException(); } } ``` --> --- ## Gut so? ```java public class SelectAndEvaluate { private ArrayList<Player> players = new ArrayList<>(); // ... public ArrayList<Player> getPlayers() {/*...*/} } ``` ```java public class Game { public void updatePlayers() { for (Player player : state.getPlayers()) { // ... } } } ``` ---- ## Etwas Besser ```java public class SelectAndEvaluate { List<Player> players = new ArrayList<>(); // ... public List<Player> getPlayers() { return players; } } ``` ```java public class Game { public void updatePlayers() { for (Player player : state.getPlayers()) { // ... } } } ``` ---- ## Noch Besser ```java public SelectAndEvaluate { List<Player> players = new ArrayList()<>; // ... public List<Player> getPlayers() { return new ArrayList<>(players); } } ``` ```java public Game { public void updatePlayers() { for (Player player : state.getPlayers()) { // ... } } } ``` ---- ## Deutlich besser ```java public class SelectAndEvaluate { List<Player> players = new ArrayList<>(); // ... public void updatePlayers() {/*...*/} } ``` --- ## Gut so? ```java public boolean checkValid(String s) throws IllegalArgumentException { if (/*some check */) { return true; } throw new IllegalArgumentExcpetion(); } ``` ---- ## Besser ```java public boolean checkValid(String s) { return /*some check*/; } ``` ```java if (!checkValid(s)) throw new InputExeption(); ``` --- # Fragen? ---- * Sieht die Bewertung bei den Abschlussaufgaben aus, exakt gleich wie bei den Übungsaufgaben? * Wie schwer ist es zu bestehen wenn man die Übungsaufgaben hinbekommen hat? * Wird auch nur die Methodik bewertet und die Funktionalität von Artemis? * Wie viel Prozent der Punkte braucht man zum bestehen? --- ## Viel Erfolg bei den Abschlussaufgaben!
{"metaMigratedAt":"2023-06-16T19:08:24.815Z","metaMigratedFrom":"YAML","title":"Programmieren Tutorium","breaks":true,"slideOptions":"{\"theme\":\"white\",\"controls\":false,\"transition\":\"slide\",\"disableLayout\":false,\"width\":\"70%\"}","contributors":"[{\"id\":\"b5538627-faab-4c8f-81e8-b5bc6f7890d3\",\"add\":11005,\"del\":299}]"}
    278 views