Skip to content

Instantly share code, notes, and snippets.

@kenkoooo
Created October 31, 2015 14:39
Show Gist options
  • Save kenkoooo/0bf8b58b70df8e6b4864 to your computer and use it in GitHub Desktop.
Save kenkoooo/0bf8b58b70df8e6b4864 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Sample {
private static final String TOKEN = "?token=4YSXKTZXTEWAW1AD3DTJTJX5HNY9AF5O";
private static final String ENTER = "https://game.coderunner.jp/enter"
+ TOKEN;
private static final String INFO = "https://game.coderunner.jp/info"
+ TOKEN;
public void solve() throws IOException {
int cnt = 0;
while (true) {
String[] result = query(INFO).split(" ");
if (!isNumber(result[0])) {
query(ENTER);
cnt = 0;
continue;
}
long rest = 0;
// パース
long maxHP = 0;
System.out.println();
long power = Long.parseLong(result[0]);
long frontHP = Long.parseLong(result[2]);
long frontBase = Long.parseLong(result[3]);
int N = Integer.parseInt(result[4]);
long player1 = Long.parseLong(result[1]);
long player2 = Long.parseLong(result[N + 7]);
long player3 = Long.parseLong(result[N + 10]);
System.out.println(String.format("%1$,3d", player1) + " "
+ String.format("%1$,3d", player2) + " "
+ String.format("%1$,3d", player3));
System.out.println(String.format("%1$,3d", frontHP) + " "
+ String.format("%1$,3d", frontBase));
System.out.println(String.format("%1$,3d", power));
// 自分の順位
int rank = 0;
if (player1 < player2) {
rank++;
}
if (player1 < player3) {
rank++;
}
long[] HPs = new long[N];
for (int i = 0; i < N; i++) {
HPs[i] = Long.parseLong(result[i + 5]);
maxHP = Math.max(maxHP, HPs[i]);
System.out.println(String.format("%1$,3d", HPs[i]));
}
maxHP = Math.max(maxHP, frontHP);
long point = Math.min(frontHP, power);
if (point > maxHP * 0.5
|| (N <= 1 && power > frontHP * 0.6)
|| maxHP * 5 < power
|| (Math.max(player2, player3) + rest < player1 && cnt % 5 == 0)) {
String attack = query(ENTER);
}
cnt++;
try {
Thread.sleep(200L);
} catch (InterruptedException e) {
}
}
}
private boolean isNumber(String val) {
String regex = "\\A[-]?[0-9]+\\z";
Pattern p = Pattern.compile(regex);
Matcher m1 = p.matcher(val);
return m1.find();
}
private String query(String _url) throws IOException {
URL url = new URL(_url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(
connection.getInputStream(), StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr);
String res = "";
String line = null;
while ((line = reader.readLine()) != null) {
res += line + " ";
}
return res.trim();
}
return "";
}
public static void main(String[] args) throws IOException {
new Sample().solve();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment