From 781c2b265e1389f69f280d9a8c039a59678a78ec Mon Sep 17 00:00:00 2001
From: Camil Staps
Date: Thu, 19 Feb 2015 18:55:43 +0100
Subject: directory structure

---
 GameController/.gitignore                          | 73 +++++++++++++++++++++
 .../src/nl/camilstaps/botleagues/Contestant.java   | 76 ++++++++++++++++++++++
 .../nl/camilstaps/botleagues/GameController.java   | 65 ++++++++++++++++++
 3 files changed, 214 insertions(+)
 create mode 100644 GameController/.gitignore
 create mode 100644 GameController/src/nl/camilstaps/botleagues/Contestant.java
 create mode 100644 GameController/src/nl/camilstaps/botleagues/GameController.java

(limited to 'GameController')

diff --git a/GameController/.gitignore b/GameController/.gitignore
new file mode 100644
index 0000000..b630aee
--- /dev/null
+++ b/GameController/.gitignore
@@ -0,0 +1,73 @@
+# Source: http://www.bmchild.com/2012/06/git-ignore-for-java-eclipse-project.html
+
+# Directories #
+/build/
+/bin/
+target/
+ 
+# OS Files #
+.DS_Store
+ 
+*.class
+ 
+# Package Files #
+*.jar
+*.war
+*.ear
+*.db
+ 
+######################
+# Windows
+######################
+ 
+# Windows image file caches
+Thumbs.db 
+ 
+# Folder config file
+Desktop.ini
+ 
+######################
+# OSX
+######################
+ 
+.DS_Store
+.svn
+ 
+# Thumbnails
+._*
+ 
+# Files that might appear on external disk
+.Spotlight-V100
+.Trashes
+ 
+ 
+######################
+# Eclipse
+######################
+ 
+*.pydevproject
+.project
+.metadata
+bin/**
+tmp/**
+tmp/**/*
+*.tmp
+*.bak
+*.swp
+*~.nib
+local.properties
+.classpath
+.settings/
+.loadpath
+/src/main/resources/rebel.xml
+# External tool builders
+.externalToolBuilders/
+ 
+# Locally stored "Eclipse launch configurations"
+*.launch
+ 
+# CDT-specific
+.cproject
+ 
+# PDT-specific
+.buildpath
diff --git a/GameController/src/nl/camilstaps/botleagues/Contestant.java b/GameController/src/nl/camilstaps/botleagues/Contestant.java
new file mode 100644
index 0000000..77fab8e
--- /dev/null
+++ b/GameController/src/nl/camilstaps/botleagues/Contestant.java
@@ -0,0 +1,76 @@
+package nl.camilstaps.botleagues;
+
+/**
+ * A contestant is a process, a unique identifier and has a score.
+ * 
+ * @author Camil Staps <info@camilstaps.nl>
+ */
+public class Contestant implements Comparable<Contestant> {
+
+	/** The process we're running this on */
+	private final Process process;
+	/** A unique identifier (can be set in the constructor */
+	private final int uid;
+	/** The score */
+	private int score = 0;
+	
+	/**
+	 * Create a new contestant with default uid -1
+	 * 
+	 * @param p the process to link this contestant to
+	 */
+	public Contestant(Process p) {
+		process = p;
+		this.uid = -1;
+	}
+	
+	/**
+	 * Create a new contestant with a custom uid
+	 * 
+	 * @param p the process to link this contestant to
+	 * @param uid the uid
+	 */
+	public Contestant(Process p, int uid) {
+		process = p;
+		this.uid = uid;
+	}
+	
+	/**
+	 * Set a new score
+	 * @param new_score
+	 */
+	public void setScore(int new_score) {
+		score = new_score;
+	}
+	
+	/**
+	 * Get the current score
+	 * @return
+	 */
+	public int getScore() {
+		return score;
+	}
+	
+	/**
+	 * Get the process
+	 * @return
+	 */
+	public Process getProcess() {
+		return process;
+	}
+
+	@Override
+	/**
+	 * We compare contestants based on their score.
+	 * Like this, we can make a ranking.
+	 */
+	public int compareTo(Contestant arg0) {
+		return ((Integer) getScore()).compareTo(arg0.getScore());
+	}
+	
+	@Override
+	public String toString() {
+		return "Contestant " + uid + " (" + score + "): " + process.toString();
+	}
+	
+}
diff --git a/GameController/src/nl/camilstaps/botleagues/GameController.java b/GameController/src/nl/camilstaps/botleagues/GameController.java
new file mode 100644
index 0000000..77eea76
--- /dev/null
+++ b/GameController/src/nl/camilstaps/botleagues/GameController.java
@@ -0,0 +1,65 @@
+package nl.camilstaps.botleagues;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * 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
+ * 
+ * @author Camil Staps <info@camilstaps.nl>
+ */
+public abstract class GameController {
+	
+	/** The contestants in this game */
+	List<Contestant> contestants = new ArrayList<>();
+	
+	/**
+	 * 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
+	 * 
+	 * @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());
+		ProcessBuilder pb = new ProcessBuilder("java", contestant);
+		pb.directory(directory);
+		contestants.add(new Contestant(pb.start(), contestants.size()));
+	}
+	
+	/**
+	 * Rank the contestants by ascending score
+	 */
+	protected void rank() {
+		Collections.sort(contestants);
+	}
+	
+	/**
+	 * 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
+	 */
+	public Contestant getWinner() {
+		rank();
+		return contestants.get(contestants.size() - 1);
+	}
+	
+}
-- 
cgit v1.2.3