aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThe Vu2015-02-21 20:32:09 +0100
committerThe Vu2015-02-21 20:32:09 +0100
commite81b6e3fa3e9028fbfe66eda8661960d445b8a7c (patch)
treef8d423ec704800255826cf3b2d8ce2a6c6c2bb39
parentround based game (diff)
timeoutexception, improved contestants
-rw-r--r--GameController/src/nl/camilstaps/botleagues/Contestant.java24
-rw-r--r--GameController/src/nl/camilstaps/botleagues/GameController.java6
-rw-r--r--GameController/src/nl/camilstaps/botleagues/roundbasedgame/RoundBasedGameController.java14
3 files changed, 34 insertions, 10 deletions
diff --git a/GameController/src/nl/camilstaps/botleagues/Contestant.java b/GameController/src/nl/camilstaps/botleagues/Contestant.java
index d747049..570f884 100644
--- a/GameController/src/nl/camilstaps/botleagues/Contestant.java
+++ b/GameController/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/GameController/src/nl/camilstaps/botleagues/GameController.java b/GameController/src/nl/camilstaps/botleagues/GameController.java
index 5542321..85ee88f 100644
--- a/GameController/src/nl/camilstaps/botleagues/GameController.java
+++ b/GameController/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/GameController/src/nl/camilstaps/botleagues/roundbasedgame/RoundBasedGameController.java b/GameController/src/nl/camilstaps/botleagues/roundbasedgame/RoundBasedGameController.java
index a3690bd..3f61565 100644
--- a/GameController/src/nl/camilstaps/botleagues/roundbasedgame/RoundBasedGameController.java
+++ b/GameController/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();
}