diff options
author | The Vu | 2015-02-21 20:32:09 +0100 |
---|---|---|
committer | The Vu | 2015-02-21 20:32:09 +0100 |
commit | afecdcc6e27abe2e2ca4dc80631662a4ba6d7392 (patch) | |
tree | d9ddfab7c8fb1cda208f6503c77f27c4f7ef1450 | |
parent | round based game (diff) |
timeoutexception, improved contestants
3 files changed, 34 insertions, 10 deletions
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<Contestant> { 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<Contestant> { 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<Contestant> { /** * * @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(); } |