From a248465f92583f1a065074c4a63ddf14a2349fa5 Mon Sep 17 00:00:00 2001 From: thevu31 Date: Fri, 20 Feb 2015 23:13:13 +0100 Subject: abstractify --- src/nl/camilstaps/botleagues/Contestant.java | 58 +++++++++++++++++++++------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/src/nl/camilstaps/botleagues/Contestant.java b/src/nl/camilstaps/botleagues/Contestant.java index 77fab8e..d639999 100644 --- a/src/nl/camilstaps/botleagues/Contestant.java +++ b/src/nl/camilstaps/botleagues/Contestant.java @@ -1,5 +1,11 @@ package nl.camilstaps.botleagues; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; + /** * A contestant is a process, a unique identifier and has a score. * @@ -13,50 +19,74 @@ public class Contestant implements Comparable { 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 + * @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 + * @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 + * writes the guess + * + * @throws IOException */ - public Process getProcess() { - return process; + + public void writeGuess() throws IOException { + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( + process.getOutputStream())); + writer.write("Guess\n"); + writer.flush(); + + } + + /** + * + * @return answer + * @throws NumberFormatException + * @throws IOException + */ + public int getAnswer() throws NumberFormatException, IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader( + process.getInputStream())); + int answer = Integer.parseInt(reader.readLine()); + return answer; } @Override @@ -67,10 +97,10 @@ public class Contestant implements Comparable { public int compareTo(Contestant arg0) { return ((Integer) getScore()).compareTo(arg0.getScore()); } - + @Override public String toString() { return "Contestant " + uid + " (" + score + "): " + process.toString(); } - + } -- cgit v1.2.3