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 {
15 public void drawGame() {
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());
31 public void crashGame() {
32 MultiplayerGameRunner gameRunner = new MultiplayerGameRunner();
33 Properties gameParameters = new Properties();
34 gameParameters.setProperty("roadLength", "6");
35 gameParameters.setProperty("initialStones", "15");
36 gameRunner.setGameParameters(gameParameters);
37 gameRunner.setSeed(0l);
38 gameRunner.setLeagueLevel(1);
40 gameRunner.addAgent(agentCrash);
41 gameRunner.addAgent(agentCrash);
43 assertIsDefeat(gameRunner.simulate());
46 // great thanks to @dbdr for the intense moral support leading to
48 static String agentOne = "yes 1";
49 static String agentTwo = "yes 2";
50 static String agentCrash = "false";
52 static void dumpGameResult(PrintStream p, GameResult gameResult) {
53 for (AgentDto agent : gameResult.agents) dumpAgent(p, agent);
54 dumpMap(p, "errors", gameResult.errors);
55 dumpString(p, "failCause", gameResult.failCause);
56 dumpMap(p, "ids", gameResult.ids);
57 dumpString(p, "metadata", gameResult.metadata);
58 dumpMap(p, "outputs", gameResult.outputs);
59 dumpList(p, "summaries", gameResult.summaries);
60 dumpList(p, "tooltips", gameResult.tooltips);
61 // dumpList(p, "views", gameResult.views);
64 static <V> void dumpList(PrintStream p, String tag, List<V> list) {
65 V[] a = (V[]) list.toArray();
66 for (int i = 0; i < a.length; i++) {
67 if (tag != null) p.print(tag + " ");
73 static <K,V> void dumpMap(PrintStream p, String tag, Map<K,V> map) {
74 for (Map.Entry kv : map.entrySet()) {
75 p.print(tag + " for " + kv.getKey() + ": ");
76 dumpGeneric(p, kv.getValue());
80 static <V> void dumpGeneric(PrintStream p, V v) {
81 if (v instanceof List) {
82 dumpList(p, null, (List) v);
89 static void dumpString(PrintStream p, String tag, String msg) {
90 p.println(tag + ": " + msg);
93 static void dumpAgent(PrintStream p, AgentDto agent) {
94 p.println("[agent] " + agent.agentId + ": " + agent.avatar + " " + agent.index + " " + agent.name);
97 static void assertIsDraw(GameResult gameResult) {
98 int[] scores = assertTwoScores(gameResult);
99 if (scores == null) return;
101 int s1 = scores[0], s2 = scores[1];
102 assertEquals("Player scores are equal", s1, s2);
103 assertTrue("Player scores are non-negative", s1 >= 0);
106 static void assertIsDefeat(GameResult gameResult) {
107 int[] scores = assertTwoScores(gameResult);
108 if (scores == null) return;
110 int s1 = scores[0], s2 = scores[1];
111 assertTrue("First player score is negative", s1 < 0);
112 assertTrue("Second player score is negative", s2 < 0);
115 static int[] assertTwoScores(GameResult gameResult) {
116 Map<Integer,Integer> scores = gameResult.scores;
117 assertEquals("Two scores are reported", scores.size(), 2);
118 assertTrue("Player 0 has a score", scores.containsKey(0));
119 assertTrue("Player 1 has a score", scores.containsKey(1));
120 return new int[] { scores.get(0), scores.get(1) };