1 import java.util.Properties;
3 import java.util.ListIterator;
5 import java.io.PrintStream;
7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.*;
11 import com.codingame.gameengine.runner.MultiplayerGameRunner;
12 import com.codingame.gameengine.runner.dto.*;
14 public class TrollTest {
15 static GameResult runGame(String left, String right) {
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(left);
25 gameRunner.addAgent(right);
27 return gameRunner.simulate();
31 public void drawGame() {
32 assertIsDraw(runGame(agentOne, agentOne));
36 public void defeatGames() {
37 assertIsDefeat(runGame(agentCrash, agentCrash));
38 assertIsDefeat(runGame(agentGarbage, agentGarbage));
42 public void simpleGames() {
43 // wins by direct reach, no fastforward
44 assertWinLose(agentTwo, agentOne);
46 // win by fastforward after loser exhaustion
47 assertWinLose(agentOne, "yes 15");
49 // win despite fastforward after winner exhaustion
50 // (harder to construct :-D )
51 assertWinLose(agent(1,2,2,2,8),
55 // great thanks to @dbdr for the intense moral support leading to
57 static String agentOne = "yes 1";
58 static String agentTwo = "yes 2";
59 static String agentCrash = "false";
60 static String agentGarbage = "yes this_is_assuredly_not_an_int";
62 static String agent(int... tosses) {
63 String cmd = "echo -e ";
64 for (int i = 0; i < tosses.length; i++) {
65 if (i > 0) cmd += "\\n";
71 static void dumpGameResult(PrintStream p, GameResult gameResult) {
72 for (AgentDto agent : gameResult.agents) dumpAgent(p, agent);
73 dumpMap(p, "errors", gameResult.errors);
74 dumpString(p, "failCause", gameResult.failCause);
75 dumpMap(p, "ids", gameResult.ids);
76 dumpString(p, "metadata", gameResult.metadata);
77 dumpMap(p, "outputs", gameResult.outputs);
78 dumpList(p, "summaries", gameResult.summaries);
79 dumpList(p, "tooltips", gameResult.tooltips);
80 // dumpList(p, "views", gameResult.views);
83 static <V> void dumpList(PrintStream p, String tag, List<V> list) {
84 ListIterator i = list.listIterator();
86 if (tag != null) p.print(tag + " ");
87 p.print(i.nextIndex() + ": ");
88 dumpGeneric(p, i.next());
92 static <K,V> void dumpMap(PrintStream p, String tag, Map<K,V> map) {
93 for (Map.Entry kv : map.entrySet()) {
94 p.print(tag + " for " + kv.getKey() + ": ");
95 dumpGeneric(p, kv.getValue());
99 static <E,V> void dumpGeneric(PrintStream p, E v) {
100 if (v instanceof List) {
101 dumpList(p, null, (List<?>) v);
108 static void dumpString(PrintStream p, String tag, String msg) {
109 p.println(tag + ": " + msg);
112 static void dumpAgent(PrintStream p, AgentDto agent) {
113 p.println("[agent] " + agent.agentId + ": " + agent.avatar + " " + agent.index + " " + agent.name);
116 static void assertWinLose(String winner, String loser) {
117 assertLeftWin(runGame(winner, loser));
118 assertRightWin(runGame(loser, winner));
121 static void assertLeftWin(GameResult gameResult) {
122 int[] scores = assertTwoScores(gameResult);
123 if (scores == null) return;
125 int s1 = scores[0], s2 = scores[1];
126 assertTrue("Left player has higher score than right player", s1 > s2);
129 static void assertRightWin(GameResult gameResult) {
130 int[] scores = assertTwoScores(gameResult);
131 if (scores == null) return;
133 int s1 = scores[0], s2 = scores[1];
134 assertTrue("Right player has higher score than right player", s2 > s1);
137 static void assertIsDraw(GameResult gameResult) {
138 int[] scores = assertTwoScores(gameResult);
139 if (scores == null) return;
141 int s1 = scores[0], s2 = scores[1];
142 assertEquals("Player scores are equal", s1, s2);
143 assertTrue("Player scores are non-negative", s1 >= 0);
146 static void assertIsDefeat(GameResult gameResult) {
147 int[] scores = assertTwoScores(gameResult);
148 if (scores == null) return;
150 int s1 = scores[0], s2 = scores[1];
151 assertTrue("First player score is negative", s1 < 0);
152 assertTrue("Second player score is negative", s2 < 0);
155 static int[] assertTwoScores(GameResult gameResult) {
156 Map<Integer,Integer> scores = gameResult.scores;
157 assertEquals("Two scores are reported", scores.size(), 2);
158 assertTrue("Player 0 has a score", scores.containsKey(0));
159 assertTrue("Player 1 has a score", scores.containsKey(1));
160 return new int[] { scores.get(0), scores.get(1) };