Bug report from @Astrobytes and @UnnamedCodinGamer
[troll.git] / src / main / java / com / codingame / game / Player.java
1 package com.codingame.game;
2
3 import java.util.List;
4
5 import java.util.regex.Pattern;
6 import java.util.Scanner;
7 import java.util.InputMismatchException;
8 import java.util.NoSuchElementException;
9
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;
14
15 public class Player extends AbstractMultiplayerPlayer {
16     Model.Player model;
17     View.Player view;
18
19     @Override
20     public int getExpectedOutputLines() {
21         return 1;
22     }
23
24     // same-typed positional parameters… a disaster waiting to happen
25     void gameInit(int roadLength, int initialStones, long seed) {
26         sendInputLine(String.format("%d %d %d",
27                                     roadLength, initialStones, seed));
28     }
29
30     void sendGameTurn() {
31         type = null;             //
32         stoneThrow = null;       // correctness over stability!
33         messageString = null;    //
34
35         sendInputLine(String.format("%d %d %d",
36                                     model.getTrollDistance(),
37                                     model.getStones(),
38                                     model.getOppStones()));
39         execute();
40     }
41
42     static enum Action { Throw, Timeout, Invalid }
43     Action type;
44     Integer stoneThrow;
45     String messageString;
46
47     private void reportMsg(String tag) {
48         System.err.println("Message @" + tag + ": " + messageString);
49     }
50
51     void receiveGameTurn() {
52         messageString = "";
53         try { messageString = getOutputs().get(0); }
54         catch (TimeoutException e) { type = Action.Timeout; return; }
55
56         Scanner s = new Scanner(messageString);
57         try { stoneThrow = s.nextInt(); }
58         catch (InputMismatchException e) { type = Action.Invalid; return; }
59         catch (NoSuchElementException e) { type = Action.Invalid; return; }
60
61         s.useDelimiter(eol);
62         if (s.hasNext(rest)) messageString = s.next(rest);
63         else messageString = "";
64         type = Action.Throw;
65     }
66
67     private static final Pattern rest = Pattern.compile(".*");
68     private static final Pattern eol = Pattern.compile("\n");
69 }