package net.ciklum.icfpc11.controller import net.ciklum.icfpc11.domain.Player import net.ciklum.icfpc11.parser.Command import net.ciklum.icfpc11.parser.ArgumentParser import net.ciklum.icfpc11.domain.GameError import net.ciklum.icfpc11.parser.ArgumentCompiler /** * blabla * @author vic */ @Typed final class ConsolePlayer extends Player { private final ArgumentParser parser = new ArgumentParser() private final BufferedReader input private final PrintStream output private final ArgumentCompiler compiler = new ArgumentCompiler() ConsolePlayer(String name) { this(name, System.in) } ConsolePlayer(String name, InputStream stream) { super(name) input = new BufferedReader(new InputStreamReader(stream)) output = System.out } Command getNextCommand() { try { String s0 = input.readLine() if (s0 == null) { // Null card - Game ended return null } String s1 = input.readLine() if (s1 == null) { // Null card - Game ended return null } String s2 = input.readLine() if (s2 == null) { // Null card - Game ended return null } Command command = parser.parse(s0, s1, s2) return command } catch (Exception e) { // e.printStackTrace(System.err) // Most probably EOF - game ended :) return null } } void respondCommand(Command response) { output.print compiler.compile(response) output.flush() } }