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