chessai
college code for ai playing chess in java
git clone https://9o.is/git/chessai.git
BenCarleChessServer.java
(4637B)
1 package chess.server;
2
3 import chess.*;
4 import chess.utils.ChessType;
5 import net.sf.json.JSONObject;
6 import net.sf.json.JSONSerializer;
7
8 import java.io.BufferedReader;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.net.HttpURLConnection;
12 import java.net.MalformedURLException;
13 import java.net.URL;
14
15 /**
16 *
17 */
18 public class BenCarleChessServer {
19
20 private static final String protocol = "http";
21 private static final String host = "www.bencarle.com";
22 private static final String statusPath = "chess/poll";
23 private static final String movePath = "chess/move";
24
25 private int gameId;
26 private int teamNo;
27 private String secret;
28
29 public BenCarleChessServer(int gameId, int teamNo, String secret) {
30 this.gameId = gameId;
31 this.teamNo = teamNo;
32 this.secret = secret;
33 }
34
35 public StatusResponse poll() {
36 StatusResponse response = new StatusResponse();
37 try {
38 URL url = new URL(statusUrl());
39 HttpURLConnection conn =
40 (HttpURLConnection) url.openConnection();
41 conn.setRequestMethod("GET");
42 conn.setRequestProperty("Accept", "application/json");
43
44 if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
45 throw new RuntimeException(
46 "Failed : HTTP error code : "
47 + conn.getResponseCode());
48 }
49
50 BufferedReader br = new BufferedReader(
51 new InputStreamReader((conn.getInputStream())));
52
53 String output;
54 JSONObject json = null;
55 while ((output = br.readLine()) != null) {
56 json = (JSONObject) JSONSerializer.toJSON(output);
57 }
58
59 if(json.has("ready"))
60 response.setReady(json.getBoolean("ready"));
61 if(json.has("secondsleft"))
62 response.setSecondsLeft((int)json.getDouble("secondsleft"));
63 if(json.has("lastmove"))
64 response.setLastMove(json.getString("lastmove"));
65 if(json.has("lastmovenumber"))
66 response.setLastMoveNumber(json.getInt("lastmovenumber"));
67 if(json.has("gameover"))
68 response.setGameover(json.getBoolean("gameover"));
69 if(json.has("winner"))
70 response.setWinner(json.getInt("winner"));
71
72 conn.disconnect();
73
74 } catch (MalformedURLException e) {
75 e.printStackTrace();
76 } catch (IOException e) {
77 e.printStackTrace();
78 }
79 return response;
80 }
81
82 public MoveResponse push(ChessState state, ChessMove move) {
83 MoveResponse response = new MoveResponse();
84 try {
85 URL url = new URL(moveUrl(state,move));
86 HttpURLConnection conn =
87 (HttpURLConnection) url.openConnection();
88 conn.setDoOutput(true);
89 conn.setRequestMethod("GET");
90 conn.setRequestProperty("Accept", "application/json");
91
92 if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
93 throw new RuntimeException("Failed : HTTP error code : "
94 + conn.getResponseCode());
95 }
96
97 BufferedReader br = new BufferedReader(
98 new InputStreamReader((conn.getInputStream())));
99
100 String output;
101 JSONObject json = null;
102 while ((output = br.readLine()) != null) {
103 json = (JSONObject) JSONSerializer.toJSON(output);
104 }
105
106 if(json.has("result"))
107 response.setResult(json.getBoolean("result"));
108 if(json.has("message"))
109 response.setMessage(json.getString("message"));
110
111 conn.disconnect();
112
113 } catch (MalformedURLException e) {
114 e.printStackTrace();
115 } catch (IOException e) {
116 e.printStackTrace();
117 }
118 return response;
119 }
120
121 private String statusUrl() {
122 return protocol+"://"+host+"/"+statusPath+"/"+
123 gameId+"/"+teamNo+"/"+secret+"/";
124 }
125
126 private String moveUrl(ChessState state, ChessMove move) {
127 final ChessCoordinate src = move.getCoordinate();
128 final ChessCoordinate dest = src.getCoordinate(state, move);
129 final ChessType type =
130 ChessPiece.find(state.get(src)).getType();
131
132 final String moveString =
133 type.toString()+src.toString()+dest.toString();
134
135 return protocol+"://"+host+"/"+movePath+"/"+
136 gameId+"/"+teamNo+"/"+secret+"/"+moveString+"/";
137 }
138 }