aboutsummaryrefslogtreecommitdiff
path: root/backyard
diff options
context:
space:
mode:
authorCamil Staps2015-02-17 20:57:20 +0100
committerCamil Staps2015-02-17 20:57:20 +0100
commitc0c2afd05a61e63db6116922c9306df56d4dda16 (patch)
treeb0003e43283d43b2560c5d86b831ecd3d841710b /backyard
parentAdded example bot (diff)
Updated gamecontroller example
Diffstat (limited to 'backyard')
-rw-r--r--backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/GameController.java22
-rw-r--r--backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/MyGame.java62
2 files changed, 80 insertions, 4 deletions
diff --git a/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/GameController.java b/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/GameController.java
index 0ad9d2c..73fa1da 100644
--- a/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/GameController.java
+++ b/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/GameController.java
@@ -1,8 +1,9 @@
-/**
- *
- */
package nl.camilstaps.botleagues;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+
/**
* The idea is to let this be a generic class that can be used for any game.
* It should take care of functions like:
@@ -10,7 +11,20 @@ package nl.camilstaps.botleagues;
* * Starting up bots
* * Sending data to bots
* * Receiving data from bots
+ *
+ * @author Camil Staps <info@camilstaps.nl>
*/
public abstract class GameController {
-
+
+ ArrayList<Contestant> contestants;
+
+ protected void addContestant(String contestant) throws IOException {
+ ProcessBuilder pb = new ProcessBuilder("java", contestant);
+ contestants.add(new Contestant(pb.start()));
+ }
+
+ protected void rank() {
+ Collections.sort(contestants);
+ }
+
}
diff --git a/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/MyGame.java b/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/MyGame.java
index ecce818..4dd5410 100644
--- a/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/MyGame.java
+++ b/backyard/java/trunk/GameController/src/nl/camilstaps/botleagues/MyGame.java
@@ -3,10 +3,72 @@
*/
package nl.camilstaps.botleagues;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+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.
*/
public class MyGame extends GameController {
+
+ private int[] scores;
+
+ public static void main(String[] args) {
+ @SuppressWarnings("unused")
+ MyGame game = new MyGame(args);
+ }
+
+ public MyGame(String[] bots) {
+ try {
+ for (String bot : bots) {
+ addContestant(bot);
+ }
+ scores = new int[bots.length];
+
+ for (int i = 0; i < 10; i++) {
+ doRound();
+ }
+ System.out.println(getWinner());
+ } catch (IOException e) {
+ System.err.println(e.getMessage());
+ }
+ }
+
+ public void doRound() {
+ Random rand = new Random();
+ int n = rand.nextInt(100) + 1;
+
+ try {
+ int[] guesses = new int[contestants.size()];
+ int i = 0;
+ for (Contestant contestant : contestants) {
+ Process process = contestant.getProcess();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
+ writer.write("Guess");
+
+ int answer = Integer.parseInt(reader.readLine());
+ guesses[i++] = Math.abs(n - answer);
+ }
+
+
+ } catch (IOException e) {
+ }
+ }
+
+ public void incrementScore(int i) {
+ scores[i]++;
+ }
+
+ public Contestant getWinner() {
+ rank();
+ return contestants.get(0);
+ }
+
}