import com.codingame.gameengine.runner.dto.*;
public class TrollTest implements Cloneable {
- int leagueLevel = 1; // @#$%^&* league parameter is *global* despite API!
- Long seed;
- Integer roadLength;
- Integer initialStones;
- TrollTest branch() {
+
+ // ==================== Game Runner encapsulation
+
+ private int league = 1; // @#$%^&* this parameter is *global* despite API!
+ private Long seed;
+ private Integer roadLength;
+ private Integer initialStones;
+
+ private TrollTest branch() {
try { return (TrollTest) clone(); }
catch (CloneNotSupportedException e) { throw new InternalError(e); }
}
- TrollTest setLeague(int league) { leagueLevel = league; return this; }
- TrollTest setSeed(long seed) { this.seed = seed; return this; }
- TrollTest setRoadLength(int l) { roadLength = l; return this; }
- TrollTest setInitialStones(int s) { initialStones = s; return this; }
+ private TrollTest setLeague(int l) { league = l; return this; }
+ private TrollTest setSeed(long s) { seed = s; return this; }
+ private TrollTest setRoadLength(int l) { roadLength = l; return this; }
+ private TrollTest setInitialStones(int s) { initialStones = s; return this; }
- GameResult runGame(String left, String right) {
+ private GameResult runGame(String left, String right) {
MultiplayerGameRunner gameRunner = new MultiplayerGameRunner();
- gameRunner.setLeagueLevel(leagueLevel);
+ gameRunner.setLeagueLevel(league); // mandatory
if (seed != null) gameRunner.setSeed(seed); else gameRunner.setSeed(0l);
+
Properties gameParameters = new Properties();
gameRunner.setGameParameters(gameParameters);
if (roadLength != null)
gameParameters.setProperty("roadLength", roadLength.toString());
if (initialStones != null)
- gameParameters.setProperty("initialStones", initialStones.toString());
+ gameParameters.setProperty("initialStones",
+ initialStones.toString());
gameRunner.addAgent(left);
gameRunner.addAgent(right);
return gameRunner.simulate();
}
+ // ==================== Single-round game result testing
+
@Test
public void drawGame() {
assertIsDraw(runGame(agentOne, agentOne));
assertWinLose(agent(1,2,2,2,8), agent(3,1,1,1,8));
}
+ // ==================== Cheating games (league 1 perk)
+
@Test
public void cheatingGames() {
// win by cheating (works in league 1, which is the default)
branch().setLeague(2).setSeed(1)
.setRoadLength(6).setInitialStones(15)
.assertWinLose(agentTwo, agentCheat);
+
+ // league 3 forbids
+ Random r = new Random();
+ for (int i = 0; i < 16; i++) {
+ branch().setLeague(3).setSeed(r.nextLong())
+ .setRoadLength(6).setInitialStones(15)
+ .assertWinLose(agentTwo, agentCheat);
+ }
}
+ // ==================== Map generation testing
+
static private class GameMap {
int roadLength;
int initialStones;
return result;
}
+ // ==================== Common test agents
+
// great thanks to @dbdr for the intense moral support leading to
- // the following:
- static String agentOne = "yes 1";
- static String agentTwo = "yes 2";
- static String agentAllIn = "yes 15";
- static String agentCrash = "false";
- static String agentGarbage = "yes this_is_assuredly_not_an_int";
- static String agentCheat = agent(-100,25,25,25,25);
-
- static String agent(int... tosses) {
+ // the following acceptance of failure:
+ static private final String agentOne = "yes 1";
+ static private final String agentTwo = "yes 2";
+ static private final String agentAllIn = "yes 15";
+ static private final String agentCrash = "false"; // SLOW, try and avoid
+ static private final String agentGarbage = "yes assuredly_not_an_int";
+ static private final String agentCheat = agent(-100,25,25,25,25);
+
+ static private String agent(int... tosses) {
String cmd = "echo -e ";
for (int i = 0; i < tosses.length; i++) {
if (i > 0) cmd += "\\n";
return cmd;
}
+ // ==================== Debug routines
+
static void dumpGameResult(PrintStream p, GameResult gameResult) {
for (AgentDto agent : gameResult.agents) dumpAgent(p, agent);
dumpMap(p, "errors", gameResult.errors);
// dumpList(p, "views", gameResult.views);
}
- static <V> void dumpList(PrintStream p, String tag, List<V> list) {
+ static private <V> void dumpList(PrintStream p, String tag, List<V> list) {
ListIterator i = list.listIterator();
while (i.hasNext()) {
if (tag != null) p.print(tag + " ");
}
}
- static <K,V> void dumpMap(PrintStream p, String tag, Map<K,V> map) {
+ static private <K,V> void dumpMap(PrintStream p, String tag, Map<K,V> map) {
for (Map.Entry kv : map.entrySet()) {
p.print(tag + " for " + kv.getKey() + ": ");
dumpGeneric(p, kv.getValue());
}
}
- static <E,V> void dumpGeneric(PrintStream p, E v) {
+ static private <E,V> void dumpGeneric(PrintStream p, E v) {
if (v instanceof List) {
dumpList(p, null, (List<?>) v);
}
}
}
- static void dumpString(PrintStream p, String tag, String msg) {
+ static private void dumpString(PrintStream p, String tag, String msg) {
p.println(tag + ": " + msg);
}
p.println("[agent] " + agent.agentId + ": " + agent.avatar + " " + agent.index + " " + agent.name);
}
- void assertWinLose(String winner, String loser) {
+ // ==================== Test assertions
+
+ private void assertWinLose(String winner, String loser) {
assertLeftWin(runGame(winner, loser));
assertRightWin(runGame(loser, winner));
}