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

Commit

Permalink
Bugfixes and better logging. Logic still has trouble
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Procio committed May 23, 2019
1 parent da6aebd commit 9a6213c
Show file tree
Hide file tree
Showing 51 changed files with 191 additions and 127 deletions.
8 changes: 4 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ public class Main {
public static void main(String[] args) throws Exception {
if(args == null || args.length == 0){
/* run client */
System.out.println("Running client");
System.out.println("[Main] Running client");
client.Runner.main(args);
}else if(args.length == 1) {
if (args[0].equals("client")) {
/* run client */
System.out.println("Running client");
System.out.println("[Main] Running client");
client.Runner.main(args);
} else if (args[0].equals("server")) {
/* run server */
System.out.println("Running server");
System.out.println("[Main] Running server");
server.Runner srv = new server.Runner(8888, true);
Integer port = srv.getPort();
System.out.println("server port: " + port.toString());
System.out.println("[Main] server port: " + port.toString());
srv.run();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/client/GUI/BoardPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void handle(MouseEvent mouseEvent) {
char c = 'a';
for (int i = 0; i < 8; i++) {
Label label = new Label(String.valueOf(c));
Label label1 = new Label(String.valueOf(i));
Label label1 = new Label(String.valueOf(i+1));
label.setMinSize(MinDims.BUTTON.width, MinDims.BUTTON.height);
label1.setMinSize(MinDims.BUTTON.width, MinDims.BUTTON.height);
label.setAlignment(Pos.CENTER);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/client/GUI/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public GameScreen(Stage stage, Integer[][] state, PlayerColor color){
this.pane.add(this.boardPane, 0, 0);
this.saveGame = new Button("Save Game");
this.pane.add(this.saveGame, 0, 1);
this.scene = new Scene(this.pane);
this.scene = new Scene(this.pane, MinDims.SCENE.width, MinDims.SCENE.height);
stage.setScene(this.scene);
stage.show();
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/client/GUI/MenuScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,42 +49,42 @@ private EventHandler<MouseEvent> newHandler(int i, final Stage stage){
case 0:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("BackToMainMenu Button clicked");
System.out.println("[MenuScreen] BackToMainMenu Button clicked");
mainMenu(stage);
}
};
case 1:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("NewGame Button clicked");
System.out.println("[MenuScreen] NewGame Button clicked");
newGameMenu(stage);
}
};
case 2:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("LoadGame Button clicked");
System.out.println("[MenuScreen] LoadGame Button clicked");
loadGameMenu(stage);
}
};
case 3:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("WatchGame Button clicked");
System.out.println("[MenuScreen] WatchGame Button clicked");
watchGameMenu(stage);
}
};
case 4:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("LocalGame Button clicked");
System.out.println("[MenuScreen] LocalGame Button clicked");
localGameMenu(stage);
}
};
case 5:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("RemoteGame Button clicked");
System.out.println("[MenuScreen] RemoteGame Button clicked");
remoteGameMenu(stage);
}
};
Expand Down
135 changes: 73 additions & 62 deletions src/main/java/client/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import logic.PlayerColor;
import logic.PosXY;

import java.util.concurrent.CountDownLatch;

/*TODO adaptive GUI to window size */
public class Runner extends Application{
private ClientCommunication comm;
Expand All @@ -42,21 +40,21 @@ public void start(final Stage stage) {
this.ms.setVsComputerBtn(new Button("Player vs Computer"));
EventHandler<MouseEvent> vsCompBtnEh = new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("Game vs computer clicked");
runGame(stage, false, true, PlayerColor.WHITE);
System.out.println("[client/Runner] Game vs computer clicked");
setupAndRun(stage, false, true, PlayerColor.WHITE);
}
};
this.ms.setTwoPlayerBtn(new Button("Player vs Player"));
EventHandler<MouseEvent> twoPlayerBtnEh = new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("Game vs player clicked");
runGame(stage, false, false, PlayerColor.WHITE);
System.out.println("[client/Runner] Game vs player clicked");
setupAndRun(stage, false, false, PlayerColor.WHITE);
}
};
this.ms.setTf(new TextField("Enter localhost port here"));
this.ms.setEh(new EventHandler<ActionEvent>() {
public void handle(ActionEvent actionEvent) {
System.out.println("PortNum entered in TextField");
System.out.println("[client/Runner] PortNum entered in TextField");
setPort(stage);
}
});
Expand All @@ -72,21 +70,21 @@ public void handle(ActionEvent actionEvent) {
* @param computer true if playing vs computer
* @param color color of player moving pieces of his GUI
*/
private void runGame(Stage stage, Boolean remote,
Boolean computer, final PlayerColor color) {
private void setupAndRun(Stage stage, Boolean remote,
Boolean computer, final PlayerColor color) {
/* Garbage collector should clean ms */
this.ms = null;
/* setup runner */
/* setupAndRun runner */
this.color = color;
this.computer = computer;
this.remote = remote;
this.board = new Board();
if(!this.remote) {
System.out.println("Running server locally");
System.out.println("[client/Runner] Running server locally");
server.Runner server = new server.Runner(this.remote);
this.port = server.getPort();
System.out.println("Server address:\t" + server.getAddress());
System.out.println("Server port:\t" + this.port);
System.out.println("[client/Runner] Server address:\t" + server.getAddress());
System.out.println("[client/Runner] Server port:\t" + this.port);
new Thread(server).start();
}
this.comm = new ClientCommunication(this.port, this.remote, this.color);
Expand All @@ -96,6 +94,65 @@ private void runGame(Stage stage, Boolean remote,
this.gameService.start();
}

void runGame() throws Exception{
//final CountDownLatch latch = new CountDownLatch(1);
Runnable updateGUI = new Runnable() {
public void run() {
//try{
gameScreen.getBoardPane().setMoveButtons();
//}finally{
// latch.countDown();
//}
}
};
/* game vs remote player */
if(this.remote) {
if (color.equals(PlayerColor.BLACK)) {
move = comm.getInMove().take();
gameScreen.getBoardPane().setMove(move);
Platform.runLater(updateGUI);
}
getMove();
Platform.runLater(updateGUI);
//latch.await();
comm.getOutMove().put(move);
move = comm.getInMove().take();
gameScreen.getBoardPane().setMove(move);
Platform.runLater(updateGUI);
}
/* local game */
else {
while(true) {
getMove();
System.out.println("[client/Runner] move: " + this.move[0].toString() + " " + this.move[1].toString());
this.board.move(this.color, this.move);
//latch.await();
System.out.println("[client/Runner] putting player move out to communication");
this.comm.getOutMove().put(move);
System.out.println("[client/Runner] waiting for server confirmation");
this.comm.getMoveAcceptedByServer().take();
System.out.println("[client/Runner] updating GUI");
Platform.runLater(updateGUI);
System.out.println("[client/Runner] board:\n" + board.toString());
if (this.computer) {
System.out.println("[client/Runner] getting move from AI");
this.move = board.getComputerMove(this.color.otherColor());
System.out.println("[client/Runner] move: " + this.move[0].toString() + " " + this.move[1].toString());
this.board.move(this.color.otherColor(), this.move);
System.out.println("[client/Runner] putting computer move out to communication");
this.comm.getOutMove().put(move);
this.gameScreen.getBoardPane().setMove(move);
System.out.println("[client/Runner] waiting for server confirmation");
this.comm.getMoveAcceptedByServer().take();
System.out.println("[client/Runner] updating GUI");
Platform.runLater(updateGUI);
//latch.await();
System.out.println("[client/Runner] board:\n" + board.toString());
}
}
}
}

/**
* service running in parallel with GUI without blocking it
*/
Expand All @@ -104,53 +161,7 @@ void setupGameService(){
* send to server, wait for response, update gui */
this.task = new Task<Void>() {
protected Void call() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
Runnable updateGUI = new Runnable() {
public void run() {
try{
gameScreen.getBoardPane().setMoveButtons();
}finally{
latch.countDown();
}
}
};
/* local game */
if(!remote){
PlayerColor secondColor = color.equals(PlayerColor.WHITE) ?
PlayerColor.BLACK : PlayerColor.WHITE;
while(true){
getMove();
Platform.runLater(updateGUI);
latch.await();
comm.getOutMove().put(move);
if(computer){
move = board.getComputerMove(secondColor);
comm.getOutMove().put(move);
Platform.runLater(updateGUI);
latch.await();
}
}
}
/* game vs remote player */
else {
if (color.equals(PlayerColor.BLACK)) {
move = comm.getInMove().take();
gameScreen.getBoardPane().setMove(move);
Platform.runLater(updateGUI);
}
while (true) {
getMove();
Platform.runLater(updateGUI);
latch.await();
comm.getOutMove().put(move);
move = comm.getInMove().take();
/* end game received from server */
if (receivedGameOver(move))
break;
gameScreen.getBoardPane().setMove(move);
Platform.runLater(updateGUI);
}
}
runGame();
return null;
}
};
Expand Down Expand Up @@ -189,10 +200,10 @@ private void setPort(final Stage stage){
try {
/* get port from user to connect to in the GUI*/
this.port = Integer.valueOf(ms.getTf().getText());
runGame(stage, true, false, PlayerColor.WHITE);
setupAndRun(stage, true, false, PlayerColor.WHITE);
}catch(Exception e){
this.ms.getTf().setOnAction(this.ms.getEh());
this.ms.setTf(new TextField("Try again"));
this.ms.setTf(new TextField("[client/Runner] Try again"));
this.ms.getPane().add(this.ms.getTf(), 0, 0);
this.ms.getTf().setOnAction(this.ms.getEh());
}
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/client/communication/ClientCommunication.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ClientCommunication implements Runnable{
private DataOutputStream outToServer;
private LinkedBlockingQueue<PosXY[]> outMove;
private LinkedBlockingQueue<PosXY[]> inMove;
private LinkedBlockingQueue<Boolean> updateGUI;
private LinkedBlockingQueue<Boolean> moveAcceptedByServer;
private Boolean remote;
private PlayerColor color;

Expand All @@ -26,7 +26,7 @@ public ClientCommunication(int port, Boolean remote, PlayerColor color){
this.color = color;
this.outMove = new LinkedBlockingQueue<PosXY[]>(1);
this.inMove = new LinkedBlockingQueue<PosXY[]>(1);
this.updateGUI = new LinkedBlockingQueue<Boolean>(1);
this.moveAcceptedByServer = new LinkedBlockingQueue<Boolean>(1);

}
public void run() {
Expand Down Expand Up @@ -62,20 +62,16 @@ public void connect() throws Exception {
*/
public void remoteGame() throws Exception {
if(this.color.equals(PlayerColor.BLACK)) {
System.out.println("Client waiting for move from server...");
recvMove();
}
while(true){
System.out.println("Client waiting for move from GUI...");
sendMove();
System.out.println("Client waiting for move from server...");
recvMove();
}
}

public void localGame() throws Exception{
while(true){
System.out.println("Client waiting for move from GUI...");
sendMove();
}
}
Expand All @@ -88,12 +84,14 @@ public void sendMove() throws Exception{
while(true) {
/* move is valid according to board logic */
PosXY move[] = this.outMove.take();
System.out.println("[ClientCommunication] client sending move to server");
this.outToServer.writeBytes(move[0].toString() + " " + move[1].toString() + "\n");
String response = this.inFromServer.readLine();
if (response.equals("ok")) {
//move was accepted -> update GUI
this.updateGUI.put(true);
this.moveAcceptedByServer.put(true);
break;
}else{
this.moveAcceptedByServer.put(false);
}
}
}
Expand All @@ -115,4 +113,8 @@ public LinkedBlockingQueue<PosXY[]> getOutMove() {
public LinkedBlockingQueue<PosXY[]> getInMove() {
return inMove;
}

public LinkedBlockingQueue<Boolean> getMoveAcceptedByServer() {
return moveAcceptedByServer;
}
}
Loading

0 comments on commit 9a6213c

Please sign in to comment.