aboutsummaryrefslogtreecommitdiff
path: root/GuessANumber/RandomBot
diff options
context:
space:
mode:
Diffstat (limited to 'GuessANumber/RandomBot')
-rw-r--r--GuessANumber/RandomBot/.gitignore73
-rw-r--r--GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/Bot.java15
-rw-r--r--GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotParser.java79
-rw-r--r--GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotStarter.java31
-rw-r--r--GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotState.java67
-rw-r--r--GuessANumber/RandomBot/src/nl/camilstaps/botleagues/game/Move.java38
6 files changed, 303 insertions, 0 deletions
diff --git a/GuessANumber/RandomBot/.gitignore b/GuessANumber/RandomBot/.gitignore
new file mode 100644
index 0000000..b630aee
--- /dev/null
+++ b/GuessANumber/RandomBot/.gitignore
@@ -0,0 +1,73 @@
+# Source: http://www.bmchild.com/2012/06/git-ignore-for-java-eclipse-project.html
+
+# Directories #
+/build/
+/bin/
+target/
+
+# OS Files #
+.DS_Store
+
+*.class
+
+# Package Files #
+*.jar
+*.war
+*.ear
+*.db
+
+######################
+# Windows
+######################
+
+# Windows image file caches
+Thumbs.db
+
+# Folder config file
+Desktop.ini
+
+######################
+# OSX
+######################
+
+.DS_Store
+.svn
+
+# Thumbnails
+._*
+
+# Files that might appear on external disk
+.Spotlight-V100
+.Trashes
+
+
+######################
+# Eclipse
+######################
+
+*.pydevproject
+.project
+.metadata
+bin/**
+tmp/**
+tmp/**/*
+*.tmp
+*.bak
+*.swp
+*~.nib
+local.properties
+.classpath
+.settings/
+.loadpath
+/src/main/resources/rebel.xml
+# External tool builders
+.externalToolBuilders/
+
+# Locally stored "Eclipse launch configurations"
+*.launch
+
+# CDT-specific
+.cproject
+
+# PDT-specific
+.buildpath
diff --git a/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/Bot.java b/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/Bot.java
new file mode 100644
index 0000000..217ffb5
--- /dev/null
+++ b/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/Bot.java
@@ -0,0 +1,15 @@
+package nl.camilstaps.botleagues.bot;
+
+import nl.camilstaps.botleagues.game.*;
+
+public interface Bot {
+
+ /**
+ * Get the next move according to a state
+ *
+ * @param the current state
+ * @return the move to do
+ */
+ public Move getMove(BotState state);
+
+}
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+"''");
+ }
+}
diff --git a/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotStarter.java b/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotStarter.java
new file mode 100644
index 0000000..dd65f7e
--- /dev/null
+++ b/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotStarter.java
@@ -0,0 +1,31 @@
+package nl.camilstaps.botleagues.bot;
+
+import java.util.Random;
+
+import nl.camilstaps.botleagues.game.Move;
+
+/**
+ * Example bot
+ *
+ * @author Camil Staps <info@camilstaps.nl>
+ */
+public class BotStarter implements Bot {
+
+ /**
+ * Start up a parser
+ * Command-line parameters are ignored
+ *
+ * @param args
+ */
+ public static void main(String[] args) {
+ BotParser parser = new BotParser(new BotStarter());
+ parser.run();
+ }
+
+ @Override
+ public Move getMove(BotState state) {
+ Random rand = new Random();
+ return new Move(rand.nextInt(100) + 1);
+ }
+
+}
diff --git a/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotState.java b/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotState.java
new file mode 100644
index 0000000..440cef4
--- /dev/null
+++ b/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/bot/BotState.java
@@ -0,0 +1,67 @@
+package nl.camilstaps.botleagues.bot;
+
+/**
+ * Current state of the game
+ *
+ * @author camilstaps
+ */
+public class BotState {
+
+ /**
+ * Several constants for the state of the game (at the start; we have to guess; finished)
+ */
+ public static final int STATE_START = -1;
+ public static final int STATE_GUESS = 0;
+ public static final int STATE_FINISH = 1;
+
+ /**
+ * Several constants for the result of the game (none; win; tie; loss)
+ */
+ public static final int RESULT_NONE = -1;
+ public static final int RESULT_WIN = 0;
+ public static final int RESULT_TIE = 1;
+ public static final int RESULT_LOSS = 2;
+
+ /**
+ * Private properties to hold the state and the result
+ */
+ private int state = STATE_START;
+ private int result = RESULT_NONE;
+
+ /**
+ * Set the state
+ *
+ * @param new_state
+ */
+ public void setState(int new_state) {
+ state = new_state;
+ }
+
+ /**
+ * Get the state
+ *
+ * @return the state
+ */
+ public int getState() {
+ return state;
+ }
+
+ /**
+ * Set the result
+ *
+ * @param new_result
+ */
+ public void setResult(int new_result) {
+ result = new_result;
+ }
+
+ /**
+ * Get the result
+ *
+ * @return the result
+ */
+ public int getResult() {
+ return result;
+ }
+
+}
diff --git a/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/game/Move.java b/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/game/Move.java
new file mode 100644
index 0000000..b2e1db5
--- /dev/null
+++ b/GuessANumber/RandomBot/src/nl/camilstaps/botleagues/game/Move.java
@@ -0,0 +1,38 @@
+package nl.camilstaps.botleagues.game;
+
+/**
+ * Move in the guess-a-number game
+ *
+ * @author Camil Staps <info@camilstaps.nl>
+ */
+public class Move {
+
+ /**
+ * The number we guess
+ */
+ private int number;
+
+ /**
+ * Constructor
+ *
+ * @param nr
+ */
+ public Move(int nr) {
+ number = nr;
+ }
+
+ /**
+ * Get the number we guessed
+ *
+ * @return
+ */
+ public int getNumber() {
+ return number;
+ }
+
+ @Override
+ public String toString() {
+ return Integer.toString(number);
+ }
+
+}