From 016b7582dec3e35ac36619f6b8f3e6219dd85637 Mon Sep 17 00:00:00 2001 From: The Vu Date: Sat, 21 Feb 2015 20:32:09 +0100 Subject: timeoutexception, improved contestants --- .../botleagues/GuessANumberController.java | 94 ++++++++++++++++++++++ .../nl/camilstaps/botleagues/GuessANumberMove.java | 15 ++++ .../nl/camilstaps/botleagues/MyGameController.java | 91 --------------------- .../src/nl/camilstaps/botleagues/MyMove.java | 15 ---- 4 files changed, 109 insertions(+), 106 deletions(-) create mode 100644 GuessANumber/Controller/src/nl/camilstaps/botleagues/GuessANumberController.java create mode 100644 GuessANumber/Controller/src/nl/camilstaps/botleagues/GuessANumberMove.java delete mode 100644 GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGameController.java delete mode 100644 GuessANumber/Controller/src/nl/camilstaps/botleagues/MyMove.java diff --git a/GuessANumber/Controller/src/nl/camilstaps/botleagues/GuessANumberController.java b/GuessANumber/Controller/src/nl/camilstaps/botleagues/GuessANumberController.java new file mode 100644 index 0000000..411f518 --- /dev/null +++ b/GuessANumber/Controller/src/nl/camilstaps/botleagues/GuessANumberController.java @@ -0,0 +1,94 @@ +package nl.camilstaps.botleagues; + +import java.util.List; +import java.util.Random; + +import nl.camilstaps.botleagues.exceptions.TimeOutException; +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 + * (in GameController) and an extension. + * + * @author Camil Staps + */ +public class GuessANumberController extends RoundBasedGameController { + private int numberToGuess; + + /** + * Start the game. In the command line arguments should be descriptions of + * the bots, as described in @see MyGame() + * + * @param args + * command line arguments + */ + public static void main(String[] args) { + @SuppressWarnings("unused") + GuessANumberController game = new GuessANumberController(args); + } + + /** + * Setup a game, adds bots and runs it + * + * @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 + */ + public GuessANumberController(String[] bots) { + addBots(bots); + if (getContestants().size() == 0) { + System.out.println("Hello"); + return; + + } + setCurrentContestant(getContestants().get(0)); + + Random rand = new Random(); + int n = rand.nextInt(100) + 1; + numberToGuess = n; + + try { + run(); + } catch (TimeOutException e) { + getContestants().remove(getCurrentContestant().getIndex()); + } + } + + + @Override + public void setNextContestant() { + Contestant currentContestant = getCurrentContestant(); + List contestants = getContestants(); + + if (contestants.size() != currentContestant.getIndex() + 1) { + setCurrentContestant(contestants + .get(currentContestant.getIndex() + 1)); + } + + } + + public int getNumberToGuess() { + return numberToGuess; + } + + @Override + public GuessANumberMove nextTurn() throws TimeOutException { + + getCurrentContestant().write("Guess\n"); + int guess; + guess = Integer.parseInt(getCurrentContestant().getLine(5000)); + GuessANumberMove currentMove = new GuessANumberMove(guess); + return currentMove; + + } + + +} diff --git a/GuessANumber/Controller/src/nl/camilstaps/botleagues/GuessANumberMove.java b/GuessANumber/Controller/src/nl/camilstaps/botleagues/GuessANumberMove.java new file mode 100644 index 0000000..95b2270 --- /dev/null +++ b/GuessANumber/Controller/src/nl/camilstaps/botleagues/GuessANumberMove.java @@ -0,0 +1,15 @@ +package nl.camilstaps.botleagues; + +import nl.camilstaps.botleagues.roundbasedgame.Move; + +public class GuessANumberMove extends Move { + private int guess; + + public GuessANumberMove(int guess) { + this.guess = guess; + } + + public int getGuess() { + return guess; + } +} diff --git a/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGameController.java b/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGameController.java deleted file mode 100644 index eda2f90..0000000 --- a/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyGameController.java +++ /dev/null @@ -1,91 +0,0 @@ -package nl.camilstaps.botleagues; - -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 - * (in GameController) and an extension. - * - * @author Camil Staps - */ -public class MyGameController extends RoundBasedGameController { - private int numberToGuess; - - /** - * Start the game. In the command line arguments should be descriptions of - * the bots, as described in @see MyGame() - * - * @param args - * command line arguments - */ - public static void main(String[] args) { - @SuppressWarnings("unused") - MyGameController game = new MyGameController(args); - } - - /** - * Setup a game, adds bots and runs it - * - * @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 - */ - public MyGameController(String[] bots) { - addBots(bots); - setCurrentContestant(getContestants().get(0)); - - Random rand = new Random(); - int n = rand.nextInt(100) + 1; - numberToGuess = n; - - run(); - } - - /** - * - * @return next Contestant - */ - @Override - public Contestant getNextContestant() { - Contestant currentContestant = getCurrentContestant(); - List contestants = getContestants(); - for (int i = 0; i < contestants.size(); i++) { - if (contestants.get(i).getUID() == currentContestant.getUID()) { - if (contestants.size() != i + 1) { - setCurrentContestant(contestants.get(i + 1)); - return contestants.get(i + 1); - } else { - return null; - } - } - } - return null; - - } - - public int getNumberToGuess() { - return numberToGuess; - } - - @Override - public MyMove nextTurn(Contestant nextContestant) { - - nextContestant.write("Guess\n"); - int guess = Integer.parseInt(nextContestant.getLine()); - MyMove currentMove = new MyMove(guess); - return currentMove; - - } - -} diff --git a/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyMove.java b/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyMove.java deleted file mode 100644 index b059cf5..0000000 --- a/GuessANumber/Controller/src/nl/camilstaps/botleagues/MyMove.java +++ /dev/null @@ -1,15 +0,0 @@ -package nl.camilstaps.botleagues; - -import nl.camilstaps.botleagues.roundbasedgame.Move; - -public class MyMove extends Move { - private int guess; - - public MyMove(int guess) { - this.guess = guess; - } - - public int getGuess() { - return guess; - } -} -- cgit v1.2.3