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

Commit

Permalink
Remote working, local 2 player game fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Procio committed May 23, 2019
1 parent 5236478 commit b11807f
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 115 deletions.
81 changes: 44 additions & 37 deletions src/main/java/client/GUI/MenuScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public class MenuScreen{
private Scene scene;
private Text titleText;
private GridPane pane;
/* button4 is reserved for "back to main menu" */
private Button mainMenuBtn,
vsComputerBtn,
twoPlayerBtn;
private TextField tf;
private EventHandler<ActionEvent> eh;
private Button mainMenuBtn;
private Button vsComputerBtn;
private Button twoPlayerBtn;
private Button connectAndPlayBtn;
private TextField portTextField;
private TextField addressTextField;
private EventHandler<ActionEvent> portTFEventHandler;
private EventHandler<ActionEvent> addressTFEventHandler;

public void mainMenu(Stage stage){
this.pane = new GridPane();
Expand All @@ -47,46 +49,34 @@ public void mainMenu(Stage stage){
private EventHandler<MouseEvent> newHandler(int i, final Stage stage){
switch(i){
case 0:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
return (MouseEvent) -> {
System.out.println("[MenuScreen] BackToMainMenu Button clicked");
mainMenu(stage);
}
};
case 1:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
return (MouseEvent) -> {
System.out.println("[MenuScreen] NewGame Button clicked");
newGameMenu(stage);
}
};
case 2:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
return (MouseEvent) -> {
System.out.println("[MenuScreen] LoadGame Button clicked");
loadGameMenu(stage);
}
};
case 3:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
return (MouseEvent) -> {
System.out.println("[MenuScreen] WatchGame Button clicked");
watchGameMenu(stage);
}
};
case 4:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
return (MouseEvent) -> {
System.out.println("[MenuScreen] LocalGame Button clicked");
localGameMenu(stage);
}
};
case 5:
return new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
return (MouseEvent) -> {
System.out.println("[MenuScreen] RemoteGame Button clicked");
remoteGameMenu(stage);
}
};
default:
return null;
Expand Down Expand Up @@ -145,12 +135,14 @@ private void localGameMenu(final Stage stage){
stage.setScene(this.scene);
}

private void remoteGameMenu(final Stage stage){
public void remoteGameMenu(final Stage stage){
this.pane = new GridPane();
this.pane.setAlignment(Pos.CENTER);
this.pane.setVgap(5);
this.pane.add(this.tf, 0, 0);
this.pane.add(this.mainMenuBtn, 0, 1);
this.pane.add(this.addressTextField, 0, 0);
this.pane.add(this.portTextField, 0, 1);
this.pane.add(this.connectAndPlayBtn, 0, 2);
this.pane.add(this.mainMenuBtn, 0, 3);
this.scene = new Scene(this.pane, MinDims.SCENE.width, MinDims.SCENE.height);
stage.setScene(this.scene);
}
Expand All @@ -161,20 +153,35 @@ public Button getVsComputerBtn(){
public Button getTwoPlayerBtn(){
return this.twoPlayerBtn;
}
public GridPane getPane(){
return this.pane;
public TextField getPortTextField(){
return this.portTextField;
}
public void setPortTextField(TextField portTextField){
this.portTextField = portTextField;
}
public TextField getAddressTextField() {
return addressTextField;
}
public void setAddressTextField(TextField addressTextField) {
this.addressTextField = addressTextField;
}
public Button getConnectAndPlayBtn() {
return connectAndPlayBtn;
}
public EventHandler<ActionEvent> getPortTFEventHandler() {
return portTFEventHandler;
}
public TextField getTf(){
return this.tf;
public void setPortTFEventHandler(EventHandler<ActionEvent> portTFEventHandler) {
this.portTFEventHandler = portTFEventHandler;
}
public void setTf(TextField tf){
this.tf = tf;
public EventHandler<ActionEvent> getAddressTFEventHandler() {
return addressTFEventHandler;
}
public EventHandler<ActionEvent> getEh(){
return this.eh;
public void setAddressTFEventHandler(EventHandler<ActionEvent> addressTFEventHandler) {
this.addressTFEventHandler = addressTFEventHandler;
}
public void setEh(EventHandler<ActionEvent> eh){
this.eh = eh;
public void setConnectAndPlayBtn(Button connectAndPlayBtn) {
this.connectAndPlayBtn = connectAndPlayBtn;
}
public void setVsComputerBtn(Button vsComputerBtn) {
this.vsComputerBtn = vsComputerBtn;
Expand Down
145 changes: 95 additions & 50 deletions src/main/java/client/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@

import client.GUI.GameScreen;
import client.GUI.MenuScreen;
import client.GUI.MinDims;
import client.communication.ClientCommunication;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import logic.Board;
import logic.PlayerColor;
import logic.PosXY;

import java.awt.event.ActionEvent;

/*TODO adaptive GUI to window size */
public class Runner extends Application{
private ClientCommunication comm;
private int port;
private String address;
private Integer port;
private MenuScreen ms;
private GameScreen gameScreen;
private Board board;
Expand All @@ -38,29 +41,36 @@ public static void main(String[] args){
public void start(final Stage stage) {
this.ms = new MenuScreen();
this.ms.setVsComputerBtn(new Button("Player vs Computer"));
EventHandler<MouseEvent> vsCompBtnEh = new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
System.out.println("[client/Runner] Game vs computer clicked");
setupAndRun(stage, false, true, PlayerColor.WHITE);
}
};
this.ms.getVsComputerBtn().setOnMouseClicked((MouseEvent) -> {
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("[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("[client/Runner] PortNum entered in TextField");
setPort(stage);
}
this.ms.getTwoPlayerBtn().setOnMouseClicked((mouseEvent -> {
System.out.println("[client/Runner] Game vs player clicked");
setupAndRun(stage, false, false, PlayerColor.WHITE);
}));
this.ms.setAddressTextField(new TextField("Enter server address here"));
this.ms.setAddressTFEventHandler((ActionEvent) -> {
System.out.println("[client/Runner] AddressNum entered in TextField");
setAddress(stage);
});
this.ms.getAddressTextField().setOnAction(this.ms.getAddressTFEventHandler());
this.ms.setPortTextField(new TextField("Enter port number here"));
this.ms.setPortTFEventHandler((ActionEvent) -> {
System.out.println("[client/Runner] PortNum entered in TextField");
setPort(stage);
});
this.ms.getPortTextField().setOnAction(this.ms.getPortTFEventHandler());
this.ms.setConnectAndPlayBtn(new Button("Connect and Play"));
this.ms.getConnectAndPlayBtn().setOnMouseClicked((MouseEvent) -> {
System.out.println("[client/Runner] Connect and Play clicked");
if(this.address == null)
System.out.println("[client/Runner] Address is empty");
if(this.port == null)
System.out.println("[client/Runner] Port is empty");
setupAndRun(stage, true, false, PlayerColor.WHITE);
});
this.ms.getTf().setOnAction(this.ms.getEh());
this.ms.getVsComputerBtn().setOnMouseClicked(vsCompBtnEh);
this.ms.getTwoPlayerBtn().setOnMouseClicked(twoPlayerBtnEh);
this.ms.mainMenu(stage);
}

Expand All @@ -87,68 +97,93 @@ private void setupAndRun(Stage stage, Boolean remote,
System.out.println("[client/Runner] Server port:\t" + this.port);
new Thread(server).start();
}
this.comm = new ClientCommunication(this.port, this.remote, this.color);
this.comm = new ClientCommunication(this.address, this.port, this.remote, this.color);
new Thread(this.comm).start();
this.gameScreen = new GameScreen(stage, this.board.getState(), this.color);
setupGameService();
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();
//}
gameScreen.getBoardPane().setMoveButtons();
}
};
/* game vs remote player */
if(this.remote) {
/* we are black -> enemy starting */
if (color.equals(PlayerColor.BLACK)) {
System.out.println("[client/Runner] waiting for move from server");
move = comm.getInMove().take();
System.out.println("[client/Runner] setting move in logic");
gameScreen.getBoardPane().setMove(move);
System.out.println("[client/Runner] updating GUI");
Platform.runLater(updateGUI);
}
while (!this.board.isOver()) {
System.out.println("[client/Runner] waiting for move from GUI");
getMove();
System.out.println("[client/Runner] move: " + this.move[0].toString() + " " + this.move[1].toString());
this.board.move(this.color, this.move);
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());
System.out.println("[client/Runner] waiting for move from server");
move = comm.getInMove().take();
System.out.println("[client/Runner] setting move in logic");
gameScreen.getBoardPane().setMove(move);
System.out.println("[client/Runner] updating GUI");
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) {
while(!this.board.isOver()) {
System.out.println("[client/Runner] waiting for move from GUI");
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());
this.color = this.color.otherColor();
if (this.computer) {
System.out.println("[client/Runner] getting move from AI");
this.move = board.getComputerMove(this.color.otherColor());
this.move = board.getComputerMove(this.color);
System.out.println("[client/Runner] move: " + this.move[0].toString() + " " + this.move[1].toString());
this.board.move(this.color.otherColor(), this.move);
this.board.move(this.color, this.move);
System.out.println("[client/Runner] putting computer move out to communication");
this.comm.getOutMove().put(move);
System.out.println("[client/Runner] setting move in logic");
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());
}else{
System.out.println("[client/Runner] waiting for move from GUI");
getMove();
System.out.println("[client/Runner] move: " + this.move[0].toString() + " " + this.move[1].toString());
this.board.move(this.color, this.move);
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());
}
this.color = this.color.otherColor();
}
}
}
Expand Down Expand Up @@ -196,16 +231,26 @@ void getMove() throws Exception {
}
}

private void setAddress(final Stage stage){
try {
/* get address from user to connect to in the GUI*/
this.address = this.ms.getAddressTextField().getText();
}catch(Exception e){
System.out.println("[client/Runner] address number wrong");
this.ms.setAddressTextField(new TextField("Try again"));
this.ms.getAddressTextField().setOnAction(this.ms.getAddressTFEventHandler());
this.ms.remoteGameMenu(stage);
}
}
private void setPort(final Stage stage){
try {
/* get port from user to connect to in the GUI*/
this.port = Integer.valueOf(ms.getTf().getText());
setupAndRun(stage, true, false, PlayerColor.WHITE);
this.port = Integer.valueOf(ms.getPortTextField().getText());
}catch(Exception e){
this.ms.getTf().setOnAction(this.ms.getEh());
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());
System.out.println("[client/Runner] Port number wrong");
this.ms.setPortTextField(new TextField("Try again"));
this.ms.getPortTextField().setOnAction(this.ms.getPortTFEventHandler());
this.ms.remoteGameMenu(stage);
}
}
}
Loading

0 comments on commit b11807f

Please sign in to comment.