aboutsummaryrefslogtreecommitdiff
path: root/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotParser.java
diff options
context:
space:
mode:
Diffstat (limited to 'GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotParser.java')
-rw-r--r--GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotParser.java79
1 files changed, 79 insertions, 0 deletions
diff --git a/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotParser.java b/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotParser.java
new file mode 100644
index 0000000..0413837
--- /dev/null
+++ b/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotParser.java
@@ -0,0 +1,79 @@
+package nl.camilstaps.botleagues.bot;
+
+import java.util.Scanner;
+
+import nl.camilstaps.botleagues.game.Move;
+
+/**
+ * Example bot parser
+ *
+ * @author Camil Staps <info@camilstaps.nl>
+ */
+public class BotParser {
+
+ /**
+ * stdin scanner and bot object
+ */
+ final Scanner scan;
+ final Bot bot;
+
+ /**
+ * Constructor
+ *
+ * @param bot
+ */
+ public BotParser(Bot bot) {
+ this.bot = bot;
+ scan = new Scanner(System.in);
+ }
+
+ /**
+ * Run the bot
+ */
+ public void run() {
+ // Hold the state of the bot
+ BotState state = new BotState();
+
+ // Keep reading stdin
+ while (scan.hasNextLine()) {
+
+ String line = scan.nextLine().trim();
+ if (line.length() == 0) {
+ continue;
+ }
+
+ // Split the line up in parts by whitespace
+ String[] parts = line.split("\\s+");
+
+ // What to do?
+ if (parts[0].equals("Guess")) { // Do we have to make a guess?
+ state.setState(BotState.STATE_GUESS);
+ Move move = bot.getMove(state);
+ System.out.println(move.toString());
+ } else if (parts[0].equals("Finish")) { // Are we finished?
+ state.setState(BotState.STATE_FINISH);
+ if (parts[1].equals("win")) { // What is the result?
+ state.setResult(BotState.RESULT_WIN);
+ } else if (parts[1].equals("tie")) {
+ state.setResult(BotState.RESULT_TIE);
+ } else if (parts[1].equals("loss")) {
+ state.setResult(BotState.RESULT_LOSS);
+ } else {
+ state.setResult(BotState.RESULT_NONE);
+ }
+ return;
+ } else { // Unparseable input
+ unparseable(line);
+ }
+ }
+ }
+
+ /**
+ * Show error message on stderr for unparseable input
+ *
+ * @param line
+ */
+ public void unparseable(String line) {
+ System.err.println("Unable to parse line ``"+line+"''");
+ }
+}