Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Implemented pins
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyAZ committed May 26, 2017
1 parent 3452827 commit b010751
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
31 changes: 28 additions & 3 deletions Java Problem Sets/Chess/src/control/GameConductor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.Arrays;
import java.util.HashSet;

import javax.swing.JOptionPane;

import org.json.JSONException;

import board.Board;
Expand All @@ -18,21 +20,44 @@ public class GameConductor implements MouseListener {

public Board board;
public Piece selectedPiece, whiteKing, blackKing;
public Network network = new Network();
public Network network;
public boolean side, mySide;
public HashSet<Piece> whitePieces, blackPieces;
private String[] notation;

public GameConductor() {
board = Main.board;
network = new Network(determinePin());
notation = new String[] { "a", "b", "c", "d", "e", "f", "g", "h" };
whitePieces = new HashSet<>();
blackPieces = new HashSet<>();
mySide = false;
side = false;

}

private int determinePin() {
Object[] answer = { "New Game", "Join Game" };
int input = JOptionPane.showOptionDialog(null, "Start a new game or join an existing one", "Start game",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, answer, answer[0]);
if (input == JOptionPane.YES_OPTION) {
try {
int pin = Network.startNewGame();
JOptionPane.showMessageDialog(null, "Your game pin is " + pin, "Start game",
JOptionPane.INFORMATION_MESSAGE, null);
mySide = false;
return pin;
} catch (IOException e) {
e.printStackTrace();
}
} else if (input == JOptionPane.NO_OPTION) {
mySide = true;
return Integer.parseInt(
JOptionPane.showInputDialog(null, "Enter pin", "Start game", JOptionPane.PLAIN_MESSAGE), 10);
}

return 0;
}

private void updateHandler() {
executeNotationMove(network.state.get(network.state.size() - 1));
nextTurn();
Expand Down Expand Up @@ -70,7 +95,7 @@ private void nextTurn() {
selectedPiece = null;
side = !side;
isGameOver();
Main.window.f.setTitle("Chess - " + (side ? "Black's " : "White's ") + "Move");
Main.window.f.setTitle("Chess - " + (side ? "Black's " : "White's ") + "Move Pin:" + network.GAME_PIN);
Main.window.repaint();

}
Expand Down
1 change: 0 additions & 1 deletion Java Problem Sets/Chess/src/control/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public static void main(String[] args) {
gc = new GameConductor();
board.placePieces();
window = new Window();
gc.mySide = true;
if (gc.mySide == true)
gc.startListening();
}
Expand Down
36 changes: 14 additions & 22 deletions Java Problem Sets/Chess/src/control/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,16 @@ public class Network {
public static ArrayList<String> state;
public Board board = Main.board;


/* Default constructor creates a new game pin for others to join */
public Network() {

try {
GAME_PIN = startNewGame();
System.out.println("MULTIPLAYER GAME PIN: "+GAME_PIN);
} catch (IOException e1) {
e1.printStackTrace();
}

public Network(int pin) {

GAME_PIN = pin;
System.out.println("MULTIPLAYER GAME PIN: " + GAME_PIN);

state = new ArrayList<>();

try {
URL url = new URL(HOST+":"+GAME_PIN+"/games");
URL url = new URL(HOST + ":" + GAME_PIN + "/games");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setDoOutput(true);
Expand All @@ -55,28 +50,25 @@ public Network() {
}

}

/* Constructor joins another game given a game pin */
public Network(int pin) {
GAME_PIN = pin;
}

public int startNewGame() throws IOException {
URL url = new URL(HOST+":9000/new");

public static int startNewGame() throws IOException {
URL url = new URL(HOST + ":9000/new");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(10000);
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println("Success");
int r = Integer.parseInt(rd.readLine(), 10);
rd.close();
return r;
}

public void sendLocalChange(String move) throws IOException {

state.add(move);
System.out.println(state.toString());

URL url = new URL(HOST+":"+GAME_PIN+"/games");
URL url = new URL(HOST + ":" + GAME_PIN + "/games");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setDoOutput(true);
Expand All @@ -92,7 +84,7 @@ public void sendLocalChange(String move) throws IOException {

public ArrayList<String> listenForNetworkChange() throws IOException, JSONException {
StringBuilder result = new StringBuilder();
URL url = new URL(HOST+":"+GAME_PIN+"/games");
URL url = new URL(HOST + ":" + GAME_PIN + "/games");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
Expand Down

0 comments on commit b010751

Please sign in to comment.