X-Git-Url: https://troll.desast.re/troll.git/blobdiff_plain/424174766971a11af0e953a6d5a328d466b8f68b..7b8732a6aba5c34e24aa324a25e2fae37f7149a7:/src/main/java/com/codingame/game/Player.java diff --git a/src/main/java/com/codingame/game/Player.java b/src/main/java/com/codingame/game/Player.java index ac96489..cb5bb4a 100644 --- a/src/main/java/com/codingame/game/Player.java +++ b/src/main/java/com/codingame/game/Player.java @@ -1,6 +1,7 @@ package com.codingame.game; import java.util.List; +import java.util.Random; import java.util.regex.Pattern; import java.util.Scanner; @@ -15,28 +16,60 @@ import com.codingame.gameengine.module.entities.Sprite; public class Player extends AbstractMultiplayerPlayer { Model.Player model; View.Player view; - - private String messageString = ""; - public String getMessageString() { return messageString; } + Random random = new Random(); @Override public int getExpectedOutputLines() { return 1; } - static final Pattern rest = Pattern.compile(".*"); - static final Pattern eol = Pattern.compile("\n"); - public int getAction() throws TimeoutException, NumberFormatException { - Scanner s = new Scanner(getOutputs().get(0)); + // same-typed positional parameters… a disaster waiting to happen + void gameInit(int roadLength, int initialStones, long seed) { + int nReserved = random.nextInt(5); + String reserved = ""; + while (nReserved --> 0) reserved += " 0"; + sendInputLine(String.format("%d %d %d %d%s", + roadLength, initialStones, seed, + model.getMultiplier(), reserved)); + } + + void sendGameTurn() { + type = null; // + stoneThrow = null; // correctness over stability! + messageString = null; // + + sendInputLine(String.format("%d %d %d", + model.getTrollDistance(), + model.getStones(), + model.getOppStones())); + execute(); + } + + static enum Action { Throw, Timeout, Invalid } + Action type; + Integer stoneThrow; + String messageString; + + private void reportMsg(String tag) { + System.err.println("Message @" + tag + ": " + messageString); + } + + void receiveGameTurn() { messageString = ""; - try { - int st = s.nextInt(); - s.useDelimiter(eol); - if (s.hasNext(rest)) - messageString = s.next(rest); - return st; - } - catch (InputMismatchException e) { throw new NumberFormatException(); } - catch (NoSuchElementException e) { throw new NumberFormatException(); } + try { messageString = getOutputs().get(0); } + catch (TimeoutException e) { type = Action.Timeout; return; } + + Scanner s = new Scanner(messageString); + try { stoneThrow = s.nextInt(); } + catch (InputMismatchException e) { type = Action.Invalid; return; } + catch (NoSuchElementException e) { type = Action.Invalid; return; } + + s.useDelimiter(eol); + if (s.hasNext(rest)) messageString = s.next(rest); + else messageString = ""; + type = Action.Throw; } + + private static final Pattern rest = Pattern.compile(".*"); + private static final Pattern eol = Pattern.compile("\n"); }