diff options
10 files changed, 0 insertions, 622 deletions
diff --git a/backyard/java-bot/trunk/.gitignore b/backyard/java-bot/trunk/.gitignore deleted file mode 100644 index b630aee..0000000 --- a/backyard/java-bot/trunk/.gitignore +++ /dev/null @@ -1,73 +0,0 @@ -# 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/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/bot/Bot.java b/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/bot/Bot.java deleted file mode 100644 index 217ffb5..0000000 --- a/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/bot/Bot.java +++ /dev/null @@ -1,15 +0,0 @@ -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/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/bot/BotParser.java b/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/bot/BotParser.java deleted file mode 100644 index 0413837..0000000 --- a/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/bot/BotParser.java +++ /dev/null @@ -1,79 +0,0 @@ -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/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/bot/BotStarter.java b/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/bot/BotStarter.java deleted file mode 100644 index dd65f7e..0000000 --- a/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/bot/BotStarter.java +++ /dev/null @@ -1,31 +0,0 @@ -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/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/bot/BotState.java b/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/bot/BotState.java deleted file mode 100644 index 440cef4..0000000 --- a/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/bot/BotState.java +++ /dev/null @@ -1,67 +0,0 @@ -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/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/game/Move.java b/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/game/Move.java deleted file mode 100644 index b2e1db5..0000000 --- a/backyard/java-bot/trunk/src/nl/camilstaps/botleagues/game/Move.java +++ /dev/null @@ -1,38 +0,0 @@ -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); - } - -} diff --git a/backyard/java/trunk/GameController/.gitignore b/backyard/java/trunk/GameController/.gitignore deleted file mode 100644 index b630aee..0000000 --- a/backyard/java/trunk/GameController/.gitignore +++ /dev/null @@ -1,73 +0,0 @@ -# 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/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/Contestant.java b/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/Contestant.java deleted file mode 100644 index 77fab8e..0000000 --- a/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/Contestant.java +++ /dev/null @@ -1,76 +0,0 @@ -package nl.camilstaps.botleagues; - -/** - * A contestant is a process, a unique identifier and has a score. - * - * @author Camil Staps <info@camilstaps.nl> - */ -public class Contestant implements Comparable<Contestant> { - - /** The process we're running this on */ - private final Process process; - /** A unique identifier (can be set in the constructor */ - private final int uid; - /** The score */ - private int score = 0; - - /** - * Create a new contestant with default uid -1 - * - * @param p the process to link this contestant to - */ - public Contestant(Process p) { - process = p; - this.uid = -1; - } - - /** - * Create a new contestant with a custom uid - * - * @param p the process to link this contestant to - * @param uid the uid - */ - public Contestant(Process p, int uid) { - process = p; - this.uid = uid; - } - - /** - * Set a new score - * @param new_score - */ - public void setScore(int new_score) { - score = new_score; - } - - /** - * Get the current score - * @return - */ - public int getScore() { - return score; - } - - /** - * Get the process - * @return - */ - public Process getProcess() { - return process; - } - - @Override - /** - * We compare contestants based on their score. - * Like this, we can make a ranking. - */ - public int compareTo(Contestant arg0) { - return ((Integer) getScore()).compareTo(arg0.getScore()); - } - - @Override - public String toString() { - return "Contestant " + uid + " (" + score + "): " + process.toString(); - } - -} diff --git a/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/GameController.java b/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/GameController.java deleted file mode 100644 index 77eea76..0000000 --- a/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/GameController.java +++ /dev/null @@ -1,65 +0,0 @@ -package nl.camilstaps.botleagues; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * The idea is to let this be a generic class that can be used for any game. - * It should take care of functions like: - * - * * Starting up bots - * * Sending data to bots - * * Receiving data from bots - * - * @author Camil Staps <info@camilstaps.nl> - */ -public abstract class GameController { - - /** The contestants in this game */ - List<Contestant> contestants = new ArrayList<>(); - - /** - * Add a new contestant - * - * Internally, this will create a system call from directory `directory`: - * java `contestant` - * - * So, an example call would be: - * `directory` = /home/user/my-bot - * `contestant` = com.example.mybot.MyBot - * - * @param directory The directory to work from - * @param contestant The Java path to start up - * @throws IOException - */ - protected void addContestant(File directory, String contestant) throws IOException { - System.err.println("Adding " + contestant + " from " + directory.getAbsolutePath()); - ProcessBuilder pb = new ProcessBuilder("java", contestant); - pb.directory(directory); - contestants.add(new Contestant(pb.start(), contestants.size())); - } - - /** - * Rank the contestants by ascending score - */ - protected void rank() { - Collections.sort(contestants); - } - - /** - * Get the winner of the game (the one with the highest score) - * - * NB: the contestants will be sorted after this. - * @see rank - * - * @return the winner of the game - */ - public Contestant getWinner() { - rank(); - return contestants.get(contestants.size() - 1); - } - -} diff --git a/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/MyGame.java b/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/MyGame.java deleted file mode 100644 index e7c222d..0000000 --- a/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/MyGame.java +++ /dev/null @@ -1,105 +0,0 @@ -package nl.camilstaps.botleagues; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.util.Random; - -/** - * The idea is to let this be an example of a game controller. - * Initially, we can do all the work here. Ideally, later, we'll split that up in an abstract part (in GameController) and an extension. - * - * @author Camil Staps <info@camilstaps.nl> - */ -public class MyGame extends GameController { - - /** - * Start the game. In the command line arguments should be descriptions of the bots, as described in @see MyGame() - * - * @param args command line arguments - */ - public static void main(String[] args) { - @SuppressWarnings("unused") - MyGame game = new MyGame(args); - } - - /** - * Start a new game, and do some rounds. Print the winner. - * - * @param bots descriptions of the bots (array of strings). For example: - * - * /home/user/my-bot/com.example.mybot.MyBot - * - * Internally, this will cd to /home/user/my-bot and then execute - * - * java com.example.mybot.MyBot - * - * Arbitrarily many bots can be added - */ - public MyGame(String[] bots) { - try { - for (String bot : bots) { - int i = bot.lastIndexOf("/"); - addContestant(new File(bot.substring(0, i + 1)), bot.substring(i + 1)); - } - - for (int i = 0; i < 10; i++) { - doRound(); - } - System.out.println(getWinner()); - } catch (IOException e) { - System.err.println(e.getMessage()); - } - } - - /** - * Do a round. - * - * Take a number n in mind and let all the contestants guess. The contestants that are the closest to n get a point. - */ - public void doRound() { - Random rand = new Random(); - int n = rand.nextInt(100) + 1; - - System.err.println("Contestants have to guess " + n); - - try { - // Let each contestant guess - // That means: send "Guess" over stdin to the contestant, and read a number from stdout - int[] guesses = new int[contestants.size()]; - int i = 0; - for (Contestant contestant : contestants) { - Process process = contestant.getProcess(); - System.err.print("Asking contestant " + i + "..."); - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); - writer.write("Guess\n"); - writer.flush(); - - int answer = Integer.parseInt(reader.readLine()); - guesses[i++] = Math.abs(n - answer); - System.err.println("he says " + answer + ", that's " + guesses[i - 1] + " difference."); - } - - // Give every contestant that was the closest a point - int lowest = guesses[0]; - for (i = 0; i < guesses.length; i++) { - if (guesses[i] < lowest) { - lowest = guesses[i]; - } - } - for (i = 0; i < guesses.length; i++) { - if (guesses[i] == lowest) { - System.err.println("Point for contestant " + i); - contestants.get(i).setScore(contestants.get(i).getScore() + 1); - } - } - } catch (IOException e) { - // This shouldn't happen. - } - } - -} |