1 package com.codingame.game;
4 import java.util.Random;
6 import java.util.regex.Pattern;
7 import java.util.Scanner;
8 import java.util.InputMismatchException;
9 import java.util.NoSuchElementException;
11 import com.codingame.gameengine.core.AbstractMultiplayerPlayer;
12 import com.codingame.gameengine.module.entities.Group;
13 import com.codingame.gameengine.module.entities.Text;
14 import com.codingame.gameengine.module.entities.Sprite;
16 public class Player extends AbstractMultiplayerPlayer {
19 Random random = new Random();
22 public int getExpectedOutputLines() {
26 // same-typed positional parameters… a disaster waiting to happen
27 void gameInit(int roadLength, int initialStones, long seed) {
28 int nReserved = random.nextInt(5);
30 while (nReserved --> 0) reserved += " 0";
31 sendInputLine(String.format("%d %d %d %d%s",
32 roadLength, initialStones, seed,
33 model.getMultiplier(), reserved));
38 stoneThrow = null; // correctness over stability!
39 messageString = null; //
41 sendInputLine(String.format("%d %d %d",
42 model.getTrollDistance(),
44 model.getOppStones()));
48 static enum Action { Throw, Timeout, Invalid }
53 private void reportMsg(String tag) {
54 System.err.println("Message @" + tag + ": " + messageString);
57 void receiveGameTurn() {
59 try { messageString = getOutputs().get(0); }
60 catch (TimeoutException e) { type = Action.Timeout; return; }
62 Scanner s = new Scanner(messageString);
63 try { stoneThrow = s.nextInt(); }
64 catch (InputMismatchException e) { type = Action.Invalid; return; }
65 catch (NoSuchElementException e) { type = Action.Invalid; return; }
68 if (s.hasNext(rest)) messageString = s.next(rest);
69 else messageString = "";
73 private static final Pattern rest = Pattern.compile(".*");
74 private static final Pattern eol = Pattern.compile("\n");