HTML for salting the seed
[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     Random random = new Random();
20
21     @Override
22     public int getExpectedOutputLines() {
23         return 1;
24     }
25
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);
29         String reserved = "";
30         while (nReserved --> 0) reserved += " 0";
31         sendInputLine(String.format("%d %d %d %d%s",
32                                     roadLength, initialStones, seed,
33                                     model.getMultiplier(), reserved));
34     }
35
36     void sendGameTurn() {
37         type = null;             //
38         stoneThrow = null;       // correctness over stability!
39         messageString = null;    //
40
41         sendInputLine(String.format("%d %d %d",
42                                     model.getTrollDistance(),
43                                     model.getStones(),
44                                     model.getOppStones()));
45         execute();
46     }
47
48     static enum Action { Throw, Timeout, Invalid }
49     Action type;
50     Integer stoneThrow;
51     String messageString;
52
53     private void reportMsg(String tag) {
54         System.err.println("Message @" + tag + ": " + messageString);
55     }
56
57     void receiveGameTurn() {
58         messageString = "";
59         try { messageString = getOutputs().get(0); }
60         catch (TimeoutException e) { type = Action.Timeout; return; }
61
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; }
66
67         s.useDelimiter(eol);
68         if (s.hasNext(rest)) messageString = s.next(rest);
69         else messageString = "";
70         type = Action.Throw;
71     }
72
73     private static final Pattern rest = Pattern.compile(".*");
74     private static final Pattern eol = Pattern.compile("\n");
75 }