1 import java.util.Properties;
 
   4 import java.io.PrintStream;
 
   6 import static org.junit.Assert.assertEquals;
 
   7 import static org.junit.Assert.*;
 
  10 import com.codingame.gameengine.runner.MultiplayerGameRunner;
 
  11 import com.codingame.gameengine.runner.dto.*;
 
  13 public class TrollTest {
 
  16         MultiplayerGameRunner gameRunner = new MultiplayerGameRunner();
 
  17         Properties gameParameters = new Properties();
 
  18         gameParameters.setProperty("roadLength", "6");
 
  19         gameParameters.setProperty("initialStones", "15");
 
  20         gameRunner.setGameParameters(gameParameters);
 
  21         gameRunner.setSeed(0l);
 
  22         gameRunner.setLeagueLevel(1);
 
  24         gameRunner.addAgent(agentOne);
 
  25         gameRunner.addAgent(agentOne);
 
  27         assertIsDraw(gameRunner.simulate());
 
  30     // great thanks to @dbdr for the intense moral support leading to
 
  32     static String agentOne = "yes 1";
 
  33     static String agentTwo = "yes 2";
 
  35     static void dumpGameResult(PrintStream p, GameResult gameResult) {
 
  36         for (AgentDto agent : gameResult.agents) dumpAgent(p, agent);
 
  37         dumpMap(p, "errors", gameResult.errors);
 
  38         dumpString(p, "failCause", gameResult.failCause);
 
  39         dumpMap(p, "ids", gameResult.ids);
 
  40         dumpString(p, "metadata", gameResult.metadata);
 
  41         dumpMap(p, "outputs", gameResult.outputs);
 
  42         dumpList(p, "summaries", gameResult.summaries);
 
  43         dumpList(p, "tooltips", gameResult.tooltips);
 
  44         // dumpList(p, "views", gameResult.views);
 
  47     static <V> void dumpList(PrintStream p, String tag, List<V> list) {
 
  48         V[] a = (V[]) list.toArray();
 
  49         for (int i = 0; i < a.length; i++) {
 
  50             if (tag != null) p.print(tag + " ");
 
  56     static <K,V> void dumpMap(PrintStream p, String tag, Map<K,V> map) {
 
  57         for (Map.Entry kv : map.entrySet()) {
 
  58             p.print(tag + " for " + kv.getKey() + ": ");
 
  59             dumpGeneric(p, kv.getValue());
 
  63     static <V> void dumpGeneric(PrintStream p, V v) {
 
  64         if (v instanceof List) {
 
  65             dumpList(p, null, (List) v);
 
  72     static void dumpString(PrintStream p, String tag, String msg) {
 
  73         p.println(tag + ": " + msg);
 
  76     static void dumpAgent(PrintStream p, AgentDto agent) {
 
  77         p.println("[agent] " + agent.agentId + ": " + agent.avatar + " " + agent.index + " " + agent.name);
 
  80     static void assertIsDraw(GameResult gameResult) {
 
  81         int[] scores = assertTwoScores(gameResult);
 
  82         if (scores == null) return;
 
  84         int s1 = scores[0], s2 = scores[1];
 
  85         assertEquals("Player scores are equal", s1, s2);
 
  86         dumpGameResult(System.err, gameResult);
 
  87         assertTrue("Player scores are non-negative", s1 >= 0);
 
  90     static int[] assertTwoScores(GameResult gameResult) {
 
  91         Map<Integer,Integer> scores = gameResult.scores;
 
  92         assertEquals("Two scores are reported", scores.size(), 2);
 
  93         assertTrue("Player 0 has a score", scores.containsKey(0));
 
  94         assertTrue("Player 1 has a score", scores.containsKey(1));
 
  95         return new int[] { scores.get(0), scores.get(1) };