aboutsummaryrefslogtreecommitdiff
path: root/GameController
diff options
context:
space:
mode:
authorthevu312015-02-21 13:52:18 +0100
committerthevu312015-02-21 13:52:18 +0100
commitc1574bfb567829ea7e47db6292ee4c1a83fc9911 (patch)
treeae44a6856e294be5811977a637d46fa79cc6979d /GameController
parentabstractify (diff)
abstraction
Diffstat (limited to 'GameController')
-rw-r--r--GameController/src/nl/camilstaps/botleagues/Contestant.java36
-rw-r--r--GameController/src/nl/camilstaps/botleagues/GameController.java79
2 files changed, 84 insertions, 31 deletions
diff --git a/GameController/src/nl/camilstaps/botleagues/Contestant.java b/GameController/src/nl/camilstaps/botleagues/Contestant.java
index d639999..d747049 100644
--- a/GameController/src/nl/camilstaps/botleagues/Contestant.java
+++ b/GameController/src/nl/camilstaps/botleagues/Contestant.java
@@ -62,31 +62,41 @@ public class Contestant implements Comparable<Contestant> {
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/GameController/src/nl/camilstaps/botleagues/GameController.java b/GameController/src/nl/camilstaps/botleagues/GameController.java
index 1054215..caf8de3 100644
--- a/GameController/src/nl/camilstaps/botleagues/GameController.java
+++ b/GameController/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 <info@camilstaps.nl>
*/
public abstract class GameController {
-
+
/** The contestants in this game */
List<Contestant> contestants = new ArrayList<Contestant>();
-
+ 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<Contestant> 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();
+
}