package net.ciklum.icfpc11.controller import net.ciklum.icfpc11.parser.Command import net.ciklum.icfpc11.domain.Game import net.ciklum.icfpc11.parser.Application import net.ciklum.icfpc11.domain.Slot import net.ciklum.icfpc11.domain.greenspoon10.Function import net.ciklum.icfpc11.domain.GameError import net.ciklum.icfpc11.domain.greenspoon10.Identity import net.ciklum.icfpc11.parser.ArgumentCompiler /** * * @author mym */ @Typed final class Controller { private Game game private final ArgumentCompiler compiler = new ArgumentCompiler() Controller(Game game) { this.game = game } void go(boolean printMoves = false) { // FIXME: Unit-test this!!! while (!game.ended) { Command c = game.currentProponent.nextCommand if (c != null) { applyCommand(c) game.currentOpponent.respondCommand(c) } else { // end of game, or we don't know the situation break } if (printMoves) { System.out.print compiler.compile(c) } game.nextTurn() } } /** test purpose only */ boolean errorHappened = false void applyCommand(Command command) { errorHappened = false // System.err.println "${game} ${game?.currentProponent} ${command}" Slot slot = game.currentProponent.getSlot(command.slotNumber) try { Function result = command.application == Application.LEFT ? command.card.asFunction().apply(slot.value) : slot.value.apply(command.card.asFunction()) slot.value = result } catch (GameError e) { errorHappened = true // log.warn("Error at player ${game.currentProponent.name}'s turn with a Command $command", e) slot.value = Identity.IDENTITY } } }