Last active
December 22, 2015 12:59
-
-
Save jhannes/6476529 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package no.something.ftas.solvers; | |
import no.something.ftas.QuestionSolver; | |
public class AdditionSolver extends QuestionSolver { | |
public AdditionSolver() { | |
super("Addition"); | |
} | |
public static void main(String[] args) throws Exception { | |
new AdditionSolver().solveQuestion(); | |
} | |
@Override | |
protected String answerQuestion(String question) { | |
int result = 0; | |
for (String addend : question.split("\\+")) { | |
result += Integer.parseInt(addend); | |
} | |
return String.valueOf(result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package no.something.ftas; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.io.PrintWriter; | |
import java.io.Reader; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
public abstract class QuestionSolver { | |
protected static String BASE_URL; | |
private final String category; | |
public QuestionSolver(String category) { | |
this.category = category; | |
} | |
protected void solveQuestion() throws Exception { | |
String questionUrl = BASE_URL + "game?playerid=" + PLAYER_ID | |
+ "&category=" + category; | |
System.out.println("Reading URL " + questionUrl); | |
JSONArray question = new JSONArray(readUrl(new URL(questionUrl))); | |
System.out.println(question); | |
JSONObject answer = new JSONObject(); | |
answer.put("playerId", PLAYER_ID); | |
answer.put("answers", answerQuestion(question)); | |
System.out.println(answer); | |
String res = httpPost(QuestionSolver.BASE_URL + "game", | |
answer.toString()); | |
System.out.println(res); | |
} | |
protected abstract String answerQuestion(String question); | |
protected JSONArray answerQuestion(JSONArray question) throws JSONException { | |
JSONArray answer = new JSONArray(); | |
for (int i = 0; i < question.length(); i++) { | |
answer.put(answerQuestion(question.getString(i))); | |
} | |
return answer; | |
} | |
protected static String PLAYER_ID; | |
protected static String readUrl(URL url) throws IOException { | |
return toString(url.openConnection().getInputStream()); | |
} | |
protected static String httpPost(String url, String answer) | |
throws Exception { | |
URLConnection conn = new URL(url).openConnection(); | |
conn.setDoOutput(true); | |
try (PrintWriter printWriter = new PrintWriter(new OutputStreamWriter( | |
conn.getOutputStream(), "utf-8"))) { | |
printWriter.append(answer); | |
} | |
return toString(conn.getInputStream()); | |
} | |
private static String toString(InputStream inputStream) throws IOException { | |
try (Reader reader = new BufferedReader(new InputStreamReader( | |
inputStream, "utf-8"))) { | |
StringBuilder result = new StringBuilder(); | |
int c; | |
while ((c = reader.read()) != -1) { | |
result.append((char) c); | |
} | |
return result.toString(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package no.something.ftas; | |
import no.something.ftas.solvers.AdditionSolver; | |
import no.something.ftas.solvers.EchoSolver; | |
import no.something.ftas.solvers.FromRomanSolver; | |
import no.something.ftas.solvers.MinesweeperSolver; | |
import no.something.ftas.solvers.PrimeFactorSolver; | |
import no.something.ftas.solvers.ToRomanSolver; | |
public class SolveAll { | |
public static void main(String[] args) throws Exception { | |
QuestionSolver.BASE_URL = "http://www.anderssandbox.com:8080/fta/"; | |
QuestionSolver.PLAYER_ID = "0261263"; | |
while (true) { | |
solveAll(); | |
Thread.sleep(2000); | |
} | |
} | |
private static void solveAll() throws Exception { | |
new EchoSolver().solveQuestion(); | |
new AdditionSolver().solveQuestion(); | |
new FromRomanSolver().solveQuestion(); | |
new ToRomanSolver().solveQuestion(); | |
new PrimeFactorSolver().solveQuestion(); | |
new MinesweeperSolver().solveQuestion(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment