package com.codingame.game;
+import java.util.List;
+
import java.util.regex.Pattern;
import java.util.Scanner;
import java.util.InputMismatchException;
import com.codingame.gameengine.module.entities.Sprite;
public class Player extends AbstractMultiplayerPlayer {
- Group avatar;
- Text stoneCounter;
- Text message;
- Sprite castle;
- Text stone;
-
- private String messageString;
- public String getMessageString() { return messageString; }
+ Model.Player model;
+ View.Player view;
- private int castlePosition;
- public int getCastlePosition() { return castlePosition; }
- public void setCastlePosition(int pos) { castlePosition = pos; }
-
- private int stones;
- public int getStones() { return stones; }
- public void consumeStones(int n) throws InvalidAction {
- if (n > stones) {
- throw new InvalidAction("attempted to throw more stones than they had.");
- }
- setStones(stones - n);
+ @Override
+ public int getExpectedOutputLines() {
+ return 1;
}
- public void setStones(int n) {
- stones = n;
- if (stones <= 0) {
- stoneCounter.setText("No stones!");
- stoneCounter.setFillColor(0xff7777);
- }
- else if (stones == 1) {
- stoneCounter.setText("1 stone");
- stoneCounter.setFillColor(0xffbb77);
- }
- else {
- stoneCounter.setText(stones + " stones");
- }
+
+ // same-typed positional parameters… a disaster waiting to happen
+ void gameInit(int roadLength, int initialStones) {
+ sendInputLine(String.format("%d %d", roadLength, initialStones));
}
- private int multiplier;
- public int getMultiplier() { return multiplier; }
- public void setMultiplier(int m){ multiplier = m; }
+ void sendGameTurn() {
+ type = null; //
+ stoneThrow = null; // correctness over stability!
+ messageString = null; //
- public void adjustScore(int trollPosition) {
- setScore(Math.abs(castlePosition - trollPosition));
+ sendInputLine(String.format("%d %d %d",
+ model.getTrollDistance(),
+ model.getStones(),
+ model.getOppStones()));
+ execute();
}
- @Override
- public int getExpectedOutputLines() {
- return 1;
+ static enum Action { Throw, Timeout, Invalid }
+ Action type;
+ Integer stoneThrow;
+ String messageString;
+
+ private void reportMsg(String tag) {
+ System.err.println("Message @" + tag + ": " + messageString);
}
- 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));
+ 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");
}