aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGame.java69
1 files changed, 34 insertions, 35 deletions
diff --git a/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGame.java b/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGame.java
index 79a9868..da710d7 100644
--- a/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGame.java
+++ b/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGame.java
@@ -1,25 +1,24 @@
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.
+ * 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()
+ * Start the game. In the command line arguments should be descriptions of
+ * the bots, as described in @see MyGame()
*
- * @param args command line arguments
+ * @param args
+ * command line arguments
*/
public static void main(String[] args) {
@SuppressWarnings("unused")
@@ -29,21 +28,23 @@ public class MyGame extends GameController {
/**
* Start a new game, and do some rounds. Print the winner.
*
- * @param bots descriptions of the bots (array of strings). For example:
+ * @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
+ * /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));
+ addContestant(new File(bot.substring(0, i + 1)),
+ bot.substring(i + 1));
}
for (int i = 0; i < 10; i++) {
@@ -54,38 +55,35 @@ public class MyGame extends GameController {
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.
+ * Take a number n in mind and let all the contestants guess. The
+ * contestants that are the closest to n get a point.
*
- * @todo Maximum execution time waiting for a contestant to answer
+ * @todo Maximum execution time waiting for a contestant to answer
*/
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
+ // 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.");
+ contestant.writeGuess();
+ guesses[i++] = Math.abs(n - contestant.getAnswer());
+ System.err.println("he says " + contestant.getAnswer()
+ + ", 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++) {
@@ -96,12 +94,13 @@ public class MyGame extends GameController {
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);
+ contestants.get(i).setScore(
+ contestants.get(i).getScore() + 1);
}
}
} catch (IOException e) {
// This shouldn't happen.
}
}
-
+
}