1 package com.codingame.game;
5 import java.util.regex.Pattern;
6 import java.util.Scanner;
7 import java.util.InputMismatchException;
8 import java.util.NoSuchElementException;
10 import com.codingame.gameengine.core.AbstractMultiplayerPlayer;
11 import com.codingame.gameengine.module.entities.Group;
12 import com.codingame.gameengine.module.entities.Text;
13 import com.codingame.gameengine.module.entities.Sprite;
15 public class Player extends AbstractMultiplayerPlayer {
20 public int getExpectedOutputLines() {
24 // same-typed positional parameters… a disaster waiting to happen
25 void gameInit(int roadLength, int initialStones) {
26 sendInputLine(String.format("%d %d", roadLength, initialStones));
31 stoneThrow = null; // correctness over stability!
32 messageString = null; //
34 sendInputLine(String.format("%d %d %d",
35 model.getTrollDistance(),
37 model.getOppStones()));
41 static enum Action { Throw, Timeout, Invalid }
46 private void reportMsg(String tag) {
47 System.err.println("Message @" + tag + ": " + messageString);
50 void receiveGameTurn() {
52 try { messageString = getOutputs().get(0); }
53 catch (TimeoutException e) { type = Action.Timeout; return; }
55 Scanner s = new Scanner(messageString);
56 try { stoneThrow = s.nextInt(); }
57 catch (InputMismatchException e) { type = Action.Invalid; return; }
58 catch (NoSuchElementException e) { type = Action.Invalid; return; }
61 if (s.hasNext(rest)) messageString = s.next(rest);
62 else messageString = "";
66 private static final Pattern rest = Pattern.compile(".*");
67 private static final Pattern eol = Pattern.compile("\n");