From 8b64ef016d37da8853695c0a6dbd5a10ae6b4f54 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Thu, 19 Feb 2015 18:55:43 +0100 Subject: directory structure --- src/nl/camilstaps/botleagues/Contestant.java | 76 ++++++++++++++++++++++++ src/nl/camilstaps/botleagues/GameController.java | 65 ++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 src/nl/camilstaps/botleagues/Contestant.java create mode 100644 src/nl/camilstaps/botleagues/GameController.java (limited to 'src/nl/camilstaps') diff --git a/src/nl/camilstaps/botleagues/Contestant.java b/src/nl/camilstaps/botleagues/Contestant.java new file mode 100644 index 0000000..77fab8e --- /dev/null +++ b/src/nl/camilstaps/botleagues/Contestant.java @@ -0,0 +1,76 @@ +package nl.camilstaps.botleagues; + +/** + * A contestant is a process, a unique identifier and has a score. + * + * @author Camil Staps + */ +public class Contestant implements Comparable { + + /** 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/src/nl/camilstaps/botleagues/GameController.java b/src/nl/camilstaps/botleagues/GameController.java new file mode 100644 index 0000000..77eea76 --- /dev/null +++ b/src/nl/camilstaps/botleagues/GameController.java @@ -0,0 +1,65 @@ +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 + */ +public abstract class GameController { + + /** The contestants in this game */ + List 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); + } + +} -- cgit v1.2.3 From dced6a8b6e3e158eea774a5a3ef7329ab6509611 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Thu, 19 Feb 2015 18:58:40 +0100 Subject: directory structure --- .gitignore | 1 + build.xml | 38 ++++++++++++++++++++++++ src/nl/camilstaps/botleagues/GameController.java | 2 +- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 build.xml (limited to 'src/nl/camilstaps') diff --git a/.gitignore b/.gitignore index b630aee..5bda502 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ # Directories # /build/ /bin/ +/dist/ target/ # OS Files # diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..31b3d59 --- /dev/null +++ b/build.xml @@ -0,0 +1,38 @@ + + + Controls a Botleagues game + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/nl/camilstaps/botleagues/GameController.java b/src/nl/camilstaps/botleagues/GameController.java index 77eea76..1054215 100644 --- a/src/nl/camilstaps/botleagues/GameController.java +++ b/src/nl/camilstaps/botleagues/GameController.java @@ -19,7 +19,7 @@ import java.util.List; public abstract class GameController { /** The contestants in this game */ - List contestants = new ArrayList<>(); + List contestants = new ArrayList(); /** * Add a new contestant -- cgit v1.2.3 From a248465f92583f1a065074c4a63ddf14a2349fa5 Mon Sep 17 00:00:00 2001 From: thevu31 Date: Fri, 20 Feb 2015 23:13:13 +0100 Subject: abstractify --- src/nl/camilstaps/botleagues/Contestant.java | 58 +++++++++++++++++++++------- 1 file changed, 44 insertions(+), 14 deletions(-) (limited to 'src/nl/camilstaps') diff --git a/src/nl/camilstaps/botleagues/Contestant.java b/src/nl/camilstaps/botleagues/Contestant.java index 77fab8e..d639999 100644 --- a/src/nl/camilstaps/botleagues/Contestant.java +++ b/src/nl/camilstaps/botleagues/Contestant.java @@ -1,5 +1,11 @@ package nl.camilstaps.botleagues; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; + /** * A contestant is a process, a unique identifier and has a score. * @@ -13,50 +19,74 @@ public class Contestant implements Comparable { 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 + * @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 + * @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 + * writes the guess + * + * @throws IOException */ - public Process getProcess() { - return process; + + public void writeGuess() throws IOException { + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( + process.getOutputStream())); + writer.write("Guess\n"); + writer.flush(); + + } + + /** + * + * @return answer + * @throws NumberFormatException + * @throws IOException + */ + public int getAnswer() throws NumberFormatException, IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader( + process.getInputStream())); + int answer = Integer.parseInt(reader.readLine()); + return answer; } @Override @@ -67,10 +97,10 @@ public class Contestant implements Comparable { public int compareTo(Contestant arg0) { return ((Integer) getScore()).compareTo(arg0.getScore()); } - + @Override public String toString() { return "Contestant " + uid + " (" + score + "): " + process.toString(); } - + } -- cgit v1.2.3 From d0ff76cb9152118a94f849de1cb1982936db4a91 Mon Sep 17 00:00:00 2001 From: thevu31 Date: Sat, 21 Feb 2015 13:52:18 +0100 Subject: abstraction --- src/nl/camilstaps/botleagues/Contestant.java | 36 +++++++---- src/nl/camilstaps/botleagues/GameController.java | 79 ++++++++++++++++++------ 2 files changed, 84 insertions(+), 31 deletions(-) (limited to 'src/nl/camilstaps') diff --git a/src/nl/camilstaps/botleagues/Contestant.java b/src/nl/camilstaps/botleagues/Contestant.java index d639999..d747049 100644 --- a/src/nl/camilstaps/botleagues/Contestant.java +++ b/src/nl/camilstaps/botleagues/Contestant.java @@ -62,31 +62,41 @@ public class Contestant implements Comparable { return score; } - /** - * writes the guess - * - * @throws IOException - */ + public int getUID() { + return uid; + } - public void writeGuess() throws IOException { + + public void write(String text) { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( process.getOutputStream())); - writer.write("Guess\n"); - writer.flush(); + + try { + writer.write(text); + writer.flush(); + } catch (IOException e) { + + e.printStackTrace(); + } } /** * - * @return answer - * @throws NumberFormatException + * @return line * @throws IOException */ - public int getAnswer() throws NumberFormatException, IOException { + public String getLine() { BufferedReader reader = new BufferedReader(new InputStreamReader( process.getInputStream())); - int answer = Integer.parseInt(reader.readLine()); - return answer; + try { + return reader.readLine(); + } catch (IOException e) { + + e.printStackTrace(); + return ""; + } + } @Override diff --git a/src/nl/camilstaps/botleagues/GameController.java b/src/nl/camilstaps/botleagues/GameController.java index 1054215..caf8de3 100644 --- a/src/nl/camilstaps/botleagues/GameController.java +++ b/src/nl/camilstaps/botleagues/GameController.java @@ -6,53 +6,94 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import nl.camilstaps.botleagues.roundbasedgame.Turn; + /** - * The idea is to let this be a generic class that can be used for any game. - * It should take care of functions like: + * 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 + * * Starting up bots * Sending data to bots * Receiving data from bots * * @author Camil Staps */ public abstract class GameController { - + /** The contestants in this game */ List contestants = new ArrayList(); - + Contestant currentContestant; + /** * 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 + * 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 + * @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()); + 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())); } - + + public void addBots(String[] bots) { + try { + for (String bot : bots) { + int i = bot.lastIndexOf("/"); + addContestant(new File(bot.substring(0, i + 1)), + bot.substring(i + 1)); + } + } catch (IOException e) { + System.err.println(e.getMessage()); + } + } + /** * Rank the contestants by ascending score */ protected void rank() { Collections.sort(contestants); } - + + /** + * Get the current contestants + * + * @return contestants + */ + public List getContestants() { + return contestants; + } + + /** + * + * @return currentContestant + */ + + public Contestant getCurrentContestant() { + return currentContestant; + } + + protected abstract Contestant getNextContestant(); + + public void setCurrentContestant(Contestant contestant) { + currentContestant = contestant; + } + /** * 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 @@ -62,4 +103,6 @@ public abstract class GameController { return contestants.get(contestants.size() - 1); } + protected abstract Turn getTurn(); + } -- cgit v1.2.3 From 619d95f5e98cf3323e0b580a36cce012433852ab Mon Sep 17 00:00:00 2001 From: The Vu Date: Sat, 21 Feb 2015 15:43:18 +0100 Subject: deleted turn-class --- src/nl/camilstaps/botleagues/GameController.java | 4 ---- 1 file changed, 4 deletions(-) (limited to 'src/nl/camilstaps') diff --git a/src/nl/camilstaps/botleagues/GameController.java b/src/nl/camilstaps/botleagues/GameController.java index caf8de3..5542321 100644 --- a/src/nl/camilstaps/botleagues/GameController.java +++ b/src/nl/camilstaps/botleagues/GameController.java @@ -6,8 +6,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import nl.camilstaps.botleagues.roundbasedgame.Turn; - /** * The idea is to let this be a generic class that can be used for any game. It * should take care of functions like: @@ -102,7 +100,5 @@ public abstract class GameController { rank(); return contestants.get(contestants.size() - 1); } - - protected abstract Turn getTurn(); } -- cgit v1.2.3 From 0d5ea2a152723097885a353ff715ff91a178ddd3 Mon Sep 17 00:00:00 2001 From: The Vu Date: Sat, 21 Feb 2015 15:45:16 +0100 Subject: round based game --- src/nl/camilstaps/botleagues/roundbasedgame/Move.java | 5 +++++ .../roundbasedgame/RoundBasedGameController.java | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/nl/camilstaps/botleagues/roundbasedgame/Move.java create mode 100644 src/nl/camilstaps/botleagues/roundbasedgame/RoundBasedGameController.java (limited to 'src/nl/camilstaps') diff --git a/src/nl/camilstaps/botleagues/roundbasedgame/Move.java b/src/nl/camilstaps/botleagues/roundbasedgame/Move.java new file mode 100644 index 0000000..5e03707 --- /dev/null +++ b/src/nl/camilstaps/botleagues/roundbasedgame/Move.java @@ -0,0 +1,5 @@ +package nl.camilstaps.botleagues.roundbasedgame; + +public class Move { + +} diff --git a/src/nl/camilstaps/botleagues/roundbasedgame/RoundBasedGameController.java b/src/nl/camilstaps/botleagues/roundbasedgame/RoundBasedGameController.java new file mode 100644 index 0000000..a3690bd --- /dev/null +++ b/src/nl/camilstaps/botleagues/roundbasedgame/RoundBasedGameController.java @@ -0,0 +1,17 @@ +package nl.camilstaps.botleagues.roundbasedgame; + +import nl.camilstaps.botleagues.Contestant; +import nl.camilstaps.botleagues.GameController; + +public abstract class RoundBasedGameController extends GameController { + + public void run() { + while (getNextContestant() != null) + nextTurn(getNextContestant()); + System.out.println(getWinner()); + + } + + public abstract Move nextTurn(Contestant contestant) ; + +} -- cgit v1.2.3 From afecdcc6e27abe2e2ca4dc80631662a4ba6d7392 Mon Sep 17 00:00:00 2001 From: The Vu Date: Sat, 21 Feb 2015 20:32:09 +0100 Subject: timeoutexception, improved contestants --- src/nl/camilstaps/botleagues/Contestant.java | 24 +++++++++++++++++++--- src/nl/camilstaps/botleagues/GameController.java | 6 ++++-- .../roundbasedgame/RoundBasedGameController.java | 14 ++++++++----- 3 files changed, 34 insertions(+), 10 deletions(-) (limited to 'src/nl/camilstaps') diff --git a/src/nl/camilstaps/botleagues/Contestant.java b/src/nl/camilstaps/botleagues/Contestant.java index d747049..570f884 100644 --- a/src/nl/camilstaps/botleagues/Contestant.java +++ b/src/nl/camilstaps/botleagues/Contestant.java @@ -6,6 +6,8 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import nl.camilstaps.botleagues.exceptions.TimeOutException; + /** * A contestant is a process, a unique identifier and has a score. * @@ -19,6 +21,8 @@ public class Contestant implements Comparable { private final int uid; /** The score */ private int score = 0; + + private int index; /** * Create a new contestant with default uid -1 @@ -65,8 +69,14 @@ public class Contestant implements Comparable { public int getUID() { return uid; } + + public int getIndex() { + return index; + } - + public void setIndex(int index) { + this.index = index; + } public void write(String text) { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( process.getOutputStream())); @@ -84,13 +94,21 @@ public class Contestant implements Comparable { /** * * @return line + * @throws TimeOutException * @throws IOException */ - public String getLine() { + public String getLine(int timeOut) throws TimeOutException { + float startTime = System.currentTimeMillis(); BufferedReader reader = new BufferedReader(new InputStreamReader( process.getInputStream())); try { - return reader.readLine(); + while (System.currentTimeMillis() - startTime < timeOut + && !reader.ready()) { + } + if (reader.ready()) + return reader.readLine(); + else + throw new TimeOutException(); } catch (IOException e) { e.printStackTrace(); diff --git a/src/nl/camilstaps/botleagues/GameController.java b/src/nl/camilstaps/botleagues/GameController.java index 5542321..85ee88f 100644 --- a/src/nl/camilstaps/botleagues/GameController.java +++ b/src/nl/camilstaps/botleagues/GameController.java @@ -45,11 +45,14 @@ public abstract class GameController { } public void addBots(String[] bots) { + int index = 0; try { for (String bot : bots) { int i = bot.lastIndexOf("/"); addContestant(new File(bot.substring(0, i + 1)), bot.substring(i + 1)); + contestants.get(index).setIndex(index); + index++; } } catch (IOException e) { System.err.println(e.getMessage()); @@ -81,11 +84,10 @@ public abstract class GameController { return currentContestant; } - protected abstract Contestant getNextContestant(); - public void setCurrentContestant(Contestant contestant) { currentContestant = contestant; } + /** * Get the winner of the game (the one with the highest score) diff --git a/src/nl/camilstaps/botleagues/roundbasedgame/RoundBasedGameController.java b/src/nl/camilstaps/botleagues/roundbasedgame/RoundBasedGameController.java index a3690bd..3f61565 100644 --- a/src/nl/camilstaps/botleagues/roundbasedgame/RoundBasedGameController.java +++ b/src/nl/camilstaps/botleagues/roundbasedgame/RoundBasedGameController.java @@ -1,17 +1,21 @@ package nl.camilstaps.botleagues.roundbasedgame; -import nl.camilstaps.botleagues.Contestant; import nl.camilstaps.botleagues.GameController; +import nl.camilstaps.botleagues.exceptions.TimeOutException; public abstract class RoundBasedGameController extends GameController { - public void run() { - while (getNextContestant() != null) - nextTurn(getNextContestant()); + public void run() throws TimeOutException { + while (getCurrentContestant() != null) + + nextTurn(); + setNextContestant(); System.out.println(getWinner()); } + + public abstract Move nextTurn() throws TimeOutException; - public abstract Move nextTurn(Contestant contestant) ; + public abstract void setNextContestant(); } -- cgit v1.2.3