package net.ciklum.icfpc11.plan import junit.framework.TestCase import static net.ciklum.icfpc11.plan.FunctionTreeBuilder.* import net.ciklum.icfpc11.controller.Controller import net.ciklum.icfpc11.domain.Game import net.ciklum.icfpc11.domain.Player import net.ciklum.icfpc11.parser.Command import net.ciklum.icfpc11.domain.greenspoon10.CardBuilder import net.ciklum.icfpc11.domain.GameError /** * * @author mym */ class MovePlannerTest extends TestCase { private final int SLOT = 16 private MovePlanner planner private Controller controller @Override protected void setUp() { super.setUp() planner = new MovePlanner(SLOT) controller = new Controller(Game.push(new Player('p0'), new Player('p1'))) } void tearDown() { Game.pop() } void test_normalizeTheTree_simple() { assert attack(zero, zero) == planner.normalizeTheTree(attack(zero, zero)) // no normalization assert S(K(attack(zero)),get(zero)) == planner.normalizeTheTree(attack(zero, get(zero))) // gets normalized assert S(K(S(zero)),K(zero)) == planner.normalizeTheTree(S(zero, K(zero))) // S is also normalized } void test_plan_simple_attack_1() { def tree = attack(zero, zero) List commands = planner.plan(tree) commands.each {Command c -> controller.applyCommand(c) } assert tree.toString() == controller.game.currentProponent.getSlot(SLOT).value.toString() } void test_plan_simple_attack_2() { def tree = attack(zero, get(zero)) List commands = planner.plan(tree) println commands controller.game.currentProponent.getSlot(0).value = CardBuilder.n(777) commands.each {Command c -> println "apply ${c} on ${controller.game.currentProponent.getSlot(SLOT).value.toString()}" controller.applyCommand(c) println " got: ${controller.game.currentProponent.getSlot(SLOT).value.toString()}" } assert 'attack(zero,const(777))' == controller.game.currentProponent.getSlot(SLOT).value.toString() } void test_simple_right_oneop() { def tree = S(get, help(attack)) def expectedTree = S(K(S(get)), help(attack)) assert expectedTree == planner.normalizeTheTree(tree.clone()) List commands = planner.plan(tree) commands.each {Command c -> println "apply ${c} on ${controller.game.currentProponent.getSlot(SLOT).value.toString()}" controller.applyCommand(c) println " got: ${controller.game.currentProponent.getSlot(SLOT).value.toString()}" } assert tree.toString() == controller.game.currentProponent.getSlot(SLOT).value.toString() } void test_long_right_oneop() { def tree = S(get, help(attack(zombie))) def expectedTree = S(K(S(K(S(get)), help)), attack(zombie)) assert expectedTree == planner.normalizeTheTree(tree.clone()) List commands = planner.plan(tree) commands.each {Command c -> println "apply ${c} on ${controller.game.currentProponent.getSlot(SLOT).value.toString()}" controller.applyCommand(c) println " got: ${controller.game.currentProponent.getSlot(SLOT).value.toString()}" } assert tree.toString() == controller.game.currentProponent.getSlot(SLOT).value.toString() } void test_simplest_right_twoop() { def tree = S(get, help(attack, zombie)) //assertNull planner.normalizeTheTree(tree) List commands = planner.plan(tree) commands.each {Command c -> println "apply ${c} on ${controller.game.currentProponent.getSlot(SLOT).value.toString()}" controller.applyCommand(c) println " got: ${controller.game.currentProponent.getSlot(SLOT).value.toString()}" } assert tree.toString() == controller.game.currentProponent.getSlot(SLOT).value.toString() } void test_normalizeTheTree() { def tree = S(K(attack(zero, get(succ(zero)))), get) def expectedtree = S(K(S(K(S(K(attack(zero)), get)), succ(zero))), get) assert expectedtree == planner.normalizeTheTree(tree) } void test_plan_zombie() { def tree = zombie(zero, help(zero,zero)) List commands = planner.plan(tree) controller.game.currentOpponent.getSlot(255).vitality = 0 commands.each {Command c -> println "apply ${c} on ${controller.game.currentProponent.getSlot(SLOT).value.toString()}" controller.applyCommand(c) println " got: ${controller.game.currentProponent.getSlot(SLOT).value.toString()}" } assert 'I' == controller.game.currentProponent.getSlot(SLOT).value.toString() // zombie is executed assert -1 == controller.game.currentOpponent.getSlot(255).vitality assert 'help(zero,zero)' == controller.game.currentOpponent.getSlot(255).value.toString() } void test_normalizeTheTree_SSS() { try { def r = planner.normalizeTheTree(S(I, S(K(attack), K(zero)))) fail("should have failed with GameError, but got a result $r") } catch(e) { if (!(e instanceof GameError)) { fail("should have failed with GameError, but failed with $e") } } } void test_planTheTree() { assert ['2 16 zero', '1 attack 16', '2 16 zero'] == planner.plan(attack(zero, zero))*.toString() } void test_normalize_SKK() { def tree = S(K(help(zero, zero)),K(n(4))) List commands = planner.plan(tree) commands.each {Command c -> println "apply ${c} on ${controller.game.currentProponent.getSlot(SLOT).value.toString()}" controller.applyCommand(c) println " got: ${controller.game.currentProponent.getSlot(SLOT).value.toString()}" } assert tree.toString().replace('dbl(succ(succ(zero)))', 'const(4)') == controller.game.currentProponent.getSlot(SLOT).value.toString() } void test_plan_SKK() { def tree = S(K(help(zero, zero)),K(n(4))) List commands = planner.plan(tree) commands.each {Command c -> controller.applyCommand(c) } assert tree.toString().replace('dbl(succ(succ(zero)))', 'const(4)') == controller.game.currentProponent.getSlot(SLOT).value.toString() } }