/clients/Java/ChessClient.java

http://community-chess.googlecode.com/ · Java · 189 lines · 118 code · 21 blank · 50 comment · 17 complexity · d8959f21588dad9b012290c3cb87cdea MD5 · raw file

  1. import java.io.BufferedInputStream;
  2. import java.io.IOException;
  3. import java.net.MalformedURLException;
  4. import java.net.URL;
  5. import java.net.URLConnection;
  6. /** The ChessClient class should handle the interaction with the server. It also
  7. * stores the basic game information.
  8. */
  9. public class ChessClient {
  10. /** A chess board has 64 fields. */
  11. private static final int BOARD_SIZE = 64;
  12. /** Board-width. */
  13. public static final int BOARD_WIDTH = 8;
  14. /** The url the the server. */
  15. private static final String BASE_URL =
  16. "http://community-chess.com/xhrframework.php";
  17. /** The username. */
  18. private static final String USER_NAME = "abc";
  19. /** The password of the username. */
  20. private static final String USER_PASSWORD = "abc";
  21. /** The cookie. This is a PHPSESSID. */
  22. private static String myCookie = "";
  23. /** The gameID of the game you would like to play. */
  24. private static String gameID = "1";
  25. /** The chess board. */
  26. private static ChessPiece[] board = new ChessPiece[BOARD_SIZE];
  27. /**
  28. * Utility classes should not have a public or default constructor.
  29. */
  30. protected ChessClient() {
  31. // prevents calls from subclass
  32. throw new UnsupportedOperationException();
  33. }
  34. /**
  35. * Reads a web page into a StringBuilder object.
  36. * @param parametersURL The URL you would like to take a look at.
  37. * @return The Websites content as a String.
  38. */
  39. public static String getWebSite(final String parametersURL) {
  40. String strReturn = "";
  41. try {
  42. URL url = new URL(BASE_URL + parametersURL);
  43. URLConnection urlc = url.openConnection();
  44. urlc.setRequestProperty("Cookie", myCookie);
  45. BufferedInputStream buffer = new BufferedInputStream(
  46. urlc.getInputStream()
  47. );
  48. StringBuilder builder = new StringBuilder();
  49. int byteRead;
  50. while ((byteRead = buffer.read()) != -1) {
  51. builder.append((char) byteRead);
  52. }
  53. buffer.close();
  54. strReturn = builder.toString();
  55. } catch (MalformedURLException ex) {
  56. ex.printStackTrace();
  57. } catch (IOException ex) {
  58. // ex.printStackTrace();
  59. System.out.println("You've got an IOException. Do you have an "
  60. + "internet connection?");
  61. System.exit(0);
  62. } catch (Throwable t) {
  63. System.out.println("Woooah. I don't know what you've made! Please "
  64. + "try to reproduce this error and report it to "
  65. + "code.google.com/p/community-chess !");
  66. System.out.println(t.toString());
  67. System.exit(0);
  68. }
  69. return strReturn;
  70. }
  71. /** Get the current games board.
  72. @return The String of the current board. */
  73. public static String getBoard() {
  74. return getWebSite("?action=getBoard&gameID=" + gameID);
  75. }
  76. /** Get all IDs of the current games.
  77. @return String. */
  78. public static String getCurrentGamesIdList() {
  79. return getWebSite("?action=listCurrentGames");
  80. }
  81. /** Get all IDs of the past games.
  82. * @return String.
  83. */
  84. public static String getPastGamesIdList() {
  85. return getWebSite("?action=listPastGames");
  86. }
  87. /** Display the Chessboard. */
  88. public static void printBoard() {
  89. for (ChessPiece piece : board) {
  90. System.out.println(piece.getName());
  91. }
  92. }
  93. /** Get the current games board and store it in this.board. */
  94. public static void setBoard() {
  95. String currentBoard = getBoard();
  96. int x, y;
  97. for (int i = 0; i < BOARD_SIZE; i++) {
  98. x = i % BOARD_WIDTH;
  99. y = (i - x) / BOARD_WIDTH;
  100. if (Character.toLowerCase(currentBoard.charAt(i)) == 'p') {
  101. boolean isWhite = Character.isUpperCase(currentBoard.charAt(i));
  102. board[i] = new Pawn(x, y, isWhite);
  103. } else if (currentBoard.charAt(i) == '0') {
  104. board[i] = new EmptyPiece(x, y);
  105. } else {
  106. System.out.println("Not implemented yet: "
  107. + currentBoard.charAt(i));
  108. board[i] = new EmptyPiece(x, y);
  109. }
  110. //System.out.println(currentBoard.charAt(i));
  111. }
  112. }
  113. /** Login. Save the PHPSESSID to myCookie.
  114. * @return Was the login-process sucessful?
  115. */
  116. public static boolean login() {
  117. String returnVal = getWebSite("?action=login&username="
  118. + USER_NAME
  119. + "&password="
  120. + USER_PASSWORD);
  121. if (returnVal == "ERROR:You are not logged in.") {
  122. return false;
  123. } else {
  124. myCookie = "PHPSESSID=" + returnVal;
  125. return true;
  126. }
  127. }
  128. /** Challenge a player.
  129. * @param playerID The ID of the player you want to challenge.
  130. */
  131. public static void challengePlayer(final String playerID) {
  132. getWebSite("?action=challengeUser&userID=" + playerID);
  133. }
  134. /** Submit a move to the current game.
  135. * @param move The move, specified in
  136. * http://code.google.com/p/community-chess/wiki/NotationOfMoves
  137. */
  138. public static void submitMove(final String move) {
  139. getWebSite("?gameID=" + gameID + "&move=" + move);
  140. }
  141. /** Try to calculate the next move. */
  142. public static void makeMove() {
  143. for (ChessPiece piece : board) {
  144. if (piece.getName() == "Pawn") {
  145. piece.move();
  146. }
  147. }
  148. }
  149. /** The main method.
  150. * @param args Some String arguments. Isn't used at the moment.
  151. */
  152. public static void main(final String[] args) {
  153. System.out.println("Starting Java client.");
  154. if (login()) {
  155. System.out.println("You are logged in.");
  156. setBoard();
  157. System.out.println("Board was set");
  158. makeMove();
  159. System.out.println("I moved.");
  160. //System.out.println(getCurrentGamesIdList());
  161. //System.out.println(getPastGamesIdList());
  162. //challengePlayer("1");
  163. submitMove("1214");
  164. //System.out.println(getBoard());
  165. //printBoard();
  166. }
  167. }
  168. }