aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGame.java99
1 files changed, 41 insertions, 58 deletions
diff --git a/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGame.java b/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGame.java
index da710d7..ba26a97 100644
--- a/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGame.java
+++ b/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGame.java
@@ -1,9 +1,10 @@
package nl.camilstaps.botleagues;
-import java.io.File;
-import java.io.IOException;
+import java.util.List;
import java.util.Random;
+import nl.camilstaps.botleagues.roundbasedgame.RoundBasedGameController;
+
/**
* 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
@@ -11,7 +12,9 @@ import java.util.Random;
*
* @author Camil Staps <info@camilstaps.nl>
*/
-public class MyGame extends GameController {
+public class MyGame extends RoundBasedGameController {
+ MyTurn turn = new MyTurn();
+ private int numberToGuess;
/**
* Start the game. In the command line arguments should be descriptions of
@@ -26,7 +29,7 @@ public class MyGame extends GameController {
}
/**
- * Start a new game, and do some rounds. Print the winner.
+ * Setup a game, adds bots and runs it
*
* @param bots
* descriptions of the bots (array of strings). For example:
@@ -40,67 +43,47 @@ public class MyGame extends GameController {
* 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));
- }
-
- for (int i = 0; i < 10; i++) {
- doRound();
- }
- System.out.println(getWinner());
- } catch (IOException e) {
- System.err.println(e.getMessage());
- }
+ addBots(bots);
+
+ Random rand = new Random();
+ int n = rand.nextInt(100) + 1;
+ numberToGuess = n;
+
+ run();
}
/**
- * 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.
*
- * @todo Maximum execution time waiting for a contestant to answer
+ * @return next Contestant
*/
- public void doRound() {
- Random rand = new Random();
- int n = rand.nextInt(100) + 1;
-
- System.err.println("Contestants have to guess " + n);
+ @Override
+ public Contestant getNextContestant() {
+ Contestant currentContestant = getCurrentContestant();
+ List<Contestant> contestants = getContestants();
+ if (currentContestant == null) {
+ Random rand = new Random();
+ int n = rand.nextInt(contestants.size() - 1);
+ currentContestant = contestants.get(n);
+ return contestants.get(n + 1);
+ }
+ for (int i = 0; i < contestants.size(); i++) {
+ if (contestants.get(i).getUID() == currentContestant.getUID())
+ if (contestants.size() != i + 1)
+ return contestants.get(i + 1);
+ else
+ return null;
+ }
+ return null;
- try {
- // Let each contestant guess
- // 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) {
- System.err.print("Asking contestant " + i + "...");
- 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++) {
- if (guesses[i] < lowest) {
- lowest = guesses[i];
- }
- }
- 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);
- }
- }
- } catch (IOException e) {
- // This shouldn't happen.
- }
+ @Override
+ public MyTurn getTurn() {
+ return turn;
+ }
+
+ public int getNumberToGuess() {
+ return numberToGuess;
}
}