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

Commit

Permalink
replaced prints with a Logger, made Move readable
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Procio committed Dec 7, 2020
1 parent 5fc76c3 commit 30a2855
Show file tree
Hide file tree
Showing 23 changed files with 176 additions and 123 deletions.
11 changes: 8 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import java.util.logging.Logger;
import java.util.logging.Level;

public class Main {
private static Logger LOGGER = Logger.getLogger("[Main]");

public static void main(String[] args) {
if(args == null || args.length == 0){
/* run client */
System.out.println("[Main] Running client");
LOGGER.log(Level.ALL, "Running client");
client.Runner.main(args);
}else if(args.length == 1) {
if (args[0].equals("client")) {
/* run client */
System.out.println("[Main] Running client");
LOGGER.log(Level.ALL, "Running client");
client.Runner.main(args);
} else if (args[0].equals("server")) {
/* run server */
System.out.println("[Main] Running server");
LOGGER.log(Level.ALL, "Running server");
server.Runner srv = new server.Runner(true);
srv.run();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/client/GUI/BoardPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

import java.util.concurrent.LinkedBlockingQueue;

/* TODO change tile color on click */
/* TODO flip board based on color */
public class BoardPane extends GridPane {
private Button[][] squareButtons;
private EventHandler<MouseEvent> gridButtonHandler;
private Integer[][] state;
private Move move;
private LinkedBlockingQueue<Boolean> outMoveReady;

/*TODO flip board based on color */

/**
* Constructor builds BoardPane based on starting board state
* @param state starting state
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/client/GUI/MenuScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
import javafx.geometry.Pos;
import javafx.scene.input.MouseEvent;

import java.util.logging.Level;
import java.util.logging.Logger;

public class MenuScreen{
private static final Logger LOGGER = Logger.getLogger("[MenuScreen]");
private Scene scene;
private Text titleText;
private GridPane pane;
Expand Down Expand Up @@ -52,32 +56,32 @@ private EventHandler<MouseEvent> newHandler(int i, final Stage stage){
switch(i){
case 0:
return (MouseEvent) -> {
System.out.println("[MenuScreen] BackToMainMenu Button clicked");
LOGGER.log(Level.ALL, "BackToMainMenu Button clicked");
mainMenu(stage);
};
case 1:
return (MouseEvent) -> {
System.out.println("[MenuScreen] NewGame Button clicked");
LOGGER.log(Level.ALL, "NewGame Button clicked");
newGameMenu(stage);
};
case 2:
return (MouseEvent) -> {
System.out.println("[MenuScreen] LoadGame Button clicked");
LOGGER.log(Level.ALL, "LoadGame Button clicked");
loadGameMenu(stage);
};
case 3:
return (MouseEvent) -> {
System.out.println("[MenuScreen] WatchGame Button clicked");
LOGGER.log(Level.ALL, "WatchGame Button clicked");
watchGameMenu(stage);
};
case 4:
return (MouseEvent) -> {
System.out.println("[MenuScreen] LocalGame Button clicked");
LOGGER.log(Level.ALL, "LocalGame Button clicked");
localGameMenu(stage);
};
case 5:
return (MouseEvent) -> {
System.out.println("[MenuScreen] RemoteGame Button clicked");
LOGGER.log(Level.ALL, "RemoteGame Button clicked");
remoteGameMenu(stage);
};
default:
Expand Down
104 changes: 51 additions & 53 deletions src/main/java/client/Runner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package client;

import java.util.logging.Logger;
import java.util.logging.Level;
import client.GUI.GameScreen;
import client.GUI.MenuScreen;
import client.communication.ClientCommunication;
Expand All @@ -22,6 +24,7 @@
* The gui thread runs as this one, creates new thread(s) for communication
*/
public class Runner extends Application{
private static final Logger LOGGER = Logger.getLogger("[client/Runner]");
private ClientCommunication comm;
private String address;
private Integer port;
Expand All @@ -47,30 +50,30 @@ public void start(final Stage stage) {
this.ms = new MenuScreen();
this.ms.setVsComputerBtn(new Button("Player vs Computer"));
this.ms.getVsComputerBtn().setOnMouseClicked((MouseEvent) -> {
System.out.println("[client/Runner] Game vs computer clicked");
LOGGER.log(Level.ALL, "Game vs computer clicked");
setup(stage, false, true, PlayerColor.WHITE);
});
this.ms.setTwoPlayerBtn(new Button("Player vs Player"));
this.ms.getTwoPlayerBtn().setOnMouseClicked((mouseEvent -> {
System.out.println("[client/Runner] Game vs player clicked");
LOGGER.log(Level.ALL, "Game vs player clicked");
setup(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");
LOGGER.log(Level.ALL, "Address 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");
LOGGER.log(Level.ALL, "Port entered in TextField");
setPort(stage);
});
this.ms.setBlackBtn(new RadioButton("Play as black"));
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");
LOGGER.log(Level.ALL, "Connect and Play clicked");
if(this.ms.getBlackBtn().isSelected()){
setup(stage, true, false, PlayerColor.BLACK);
}else{
Expand All @@ -97,11 +100,11 @@ private void setup(Stage stage, Boolean remote,
this.remote = remote;
this.board = new Board();
if(!this.remote) {
System.out.println("[client/Runner] Running server locally");
LOGGER.log(Level.INFO, "Running server locally");
server.Runner server = new server.Runner(this.remote);
this.port = server.getPort();
System.out.println("[client/Runner] Server address:\t" + server.getAddress());
System.out.println("[client/Runner] Server port:\t" + this.port);
LOGGER.log(Level.INFO, "Server address:\t" + server.getAddress());
LOGGER.log(Level.INFO, "Server port:\t" + this.port);
new Thread(server).start();
}
this.comm = new ClientCommunication(this.address, this.port, this.remote, this.color);
Expand All @@ -116,19 +119,19 @@ private void setup(Stage stage, Boolean remote,
* remote
* local (vs computer)
* local (2 players)
* @throws Exception
* @throws Exception (Interrupted Exception) from LinkedBlockingQueue
*/
void runGame() throws Exception{
Runnable updateGUI = () -> this.gameScreen.getBoardPane().setMoveButtons();
/* game vs remote player */
if(this.remote) {
/* we are black -> enemy starting */
if (this.color.equals(PlayerColor.BLACK)) {
System.out.println("[client/Runner] waiting for move from server");
LOGGER.log(Level.ALL, "Waiting for move from server");
this.move = this.comm.getInMove().take();
System.out.println("[client/Runner] setting move in logic");
LOGGER.log(Level.ALL, "Setting move in logic");
this.gameScreen.getBoardPane().setMove(this.move);
System.out.println("[client/Runner] updating GUI");
LOGGER.log(Level.ALL, "Updating GUI");
Platform.runLater(updateGUI);
}
while (!this.board.isOver()) {
Expand All @@ -144,69 +147,69 @@ void runGame() throws Exception{
}

private void remoteRound(Runnable updateGUI) throws Exception{
System.out.println("[client/Runner] waiting for move from GUI");
LOGGER.log(Level.ALL, "Waiting for move from GUI");
getMove();
System.out.println("[client/Runner] " + this.move.toString());
LOGGER.log(Level.INFO, this.move.toString());
this.board.move(this.move);
System.out.println("[client/Runner] putting player move out to communication");
LOGGER.log(Level.ALL, "Putting player move out to communication");
this.comm.getOutMove().put(this.move);
System.out.println("[client/Runner] waiting for server confirmation");
LOGGER.log(Level.ALL, "Waiting for server confirmation");
this.comm.getMoveAcceptedByServer().take();
System.out.println("[client/Runner] updating GUI");
LOGGER.log(Level.ALL, "Updating GUI");
Platform.runLater(updateGUI);
System.out.println("[client/Runner] board:\n" + this.board.toString());
System.out.println("[client/Runner] waiting for move from server");
LOGGER.log(Level.INFO, "\n" + this.board.toString());
LOGGER.log(Level.ALL, "Waiting for move from server");
this.move = this.comm.getInMove().take();
System.out.println("[client/Runner] setting move in logic");
LOGGER.log(Level.ALL, "Setting move in logic");
this.gameScreen.getBoardPane().setMove(this.move);
System.out.println("[client/Runner] updating GUI");
LOGGER.log(Level.ALL, "Updating GUI");
Platform.runLater(updateGUI);
}

private void localRound(Runnable updateGUI) throws Exception{
System.out.println("[client/Runner] waiting for move from GUI");
LOGGER.log(Level.ALL, "Waiting for move from GUI");
this.getMove();
System.out.println("[client/Runner] move:\n" + this.move.toString());
LOGGER.log(Level.INFO, this.move.toString());
this.board.move(this.move);
if (!this.gameScreen.getBoardPane().isStateEqual(this.board.getState())) {
System.out.println("DESYNCED!!!!!!");
}
System.out.println("[client/Runner] putting player move out to communication");
LOGGER.log(Level.ALL, "Putting player move out to communication");
this.comm.getOutMove().put(this.move);
System.out.println("[client/Runner] waiting for server confirmation");
LOGGER.log(Level.ALL, "Waiting for server confirmation");
this.comm.getMoveAcceptedByServer().take();
System.out.println("[client/Runner] updating GUI");
LOGGER.log(Level.ALL, "Updating GUI");
Platform.runLater(updateGUI);
System.out.println("[client/Runner] board:\n" + this.board.toString());
LOGGER.log(Level.INFO, "\n" + this.board.toString());
if (this.board.isOver())
return;
this.color = this.color.otherColor();
if (this.computer) {
System.out.println("[client/Runner] getting move from AI");
LOGGER.log(Level.ALL, "Getting move from AI");
this.move = this.board.getComputerMove(this.color);
System.out.println("[client/Runner] move:\n" + this.move.toString());
LOGGER.log(Level.INFO, this.move.toString());
this.board.move(this.move);
System.out.println("[client/Runner] putting computer move out to communication");
LOGGER.log(Level.ALL, "Putting computer move out to communication");
this.comm.getOutMove().put(this.move);
System.out.println("[client/Runner] setting move in logic");
LOGGER.log(Level.ALL, "Setting move in logic");
this.gameScreen.getBoardPane().setMove(this.move);
System.out.println("[client/Runner] waiting for server confirmation");
LOGGER.log(Level.ALL, "Waiting for server confirmation");
this.comm.getMoveAcceptedByServer().take();
System.out.println("[client/Runner] updating GUI");
LOGGER.log(Level.ALL, "Updating GUI");
Platform.runLater(updateGUI);
System.out.println("[client/Runner] board:\n" + this.board.toString());
LOGGER.log(Level.INFO, "\n" + this.board.toString());
} else {
System.out.println("[client/Runner] waiting for move from GUI");
LOGGER.log(Level.ALL, "Waiting for move from GUI");
getMove();
System.out.println("[client/Runner] move:\n" + this.move.toString());
LOGGER.log(Level.INFO, this.move.toString());
this.board.move(this.move);
System.out.println("[client/Runner] putting player move out to communication");
LOGGER.log(Level.ALL, "Putting player move out to communication");
this.comm.getOutMove().put(move);
System.out.println("[client/Runner] waiting for server confirmation");
LOGGER.log(Level.ALL, "Waiting for server confirmation");
this.comm.getMoveAcceptedByServer().take();
System.out.println("[client/Runner] updating GUI");
LOGGER.log(Level.ALL, "Updating GUI");
Platform.runLater(updateGUI);
System.out.println("[client/Runner] board:\n" + board.toString());
LOGGER.log(Level.INFO, "\n" + this.board.toString());
}
this.color = this.color.otherColor();
}
Expand All @@ -215,13 +218,13 @@ private void localRound(Runnable updateGUI) throws Exception{
* service running in parallel with GUI without blocking it
*/
void setupGameService(){
this.task = new Task<Void>() {
this.task = new Task<>() {
protected Void call() throws Exception {
runGame();
return null;
}
};
this.gameService = new Service<Void>() {
this.gameService = new Service<>() {
@Override
protected Task<Void> createTask() {
return task;
Expand All @@ -230,34 +233,29 @@ protected Task<Void> createTask() {
}

public Boolean receivedGameOver(PosXY[] move){
if(move[0].getX().equals(-1) &&
return move[0].getX().equals(-1) &&
move[0].getY().equals(-1) &&
move[1].getX().equals(-1) &&
move[1].getY().equals(-1))
return true;
return false;
move[1].getY().equals(-1);
}

/**
* retrieve a valid move from GUI
* @throws Exception
* @throws Exception Interrupted exception from linked blocking queue
*/
private void getMove() throws Exception {
while (true){
do {
this.gameScreen.getBoardPane().getOutMoveReady().take();
this.move = this.gameScreen.getBoardPane().getMove();
if (this.board.isMoveLegal(this.color, this.move)) {
break;
}
}
} while (!this.board.isMoveLegal(this.color, this.move));
}

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");
LOGGER.log(Level.WARNING, "Address number wrong");
this.ms.setAddressTextField(new TextField("Try again"));
this.ms.getAddressTextField().setOnAction(this.ms.getAddressTFEventHandler());
this.ms.remoteGameMenu(stage);
Expand Down
Loading

0 comments on commit 30a2855

Please sign in to comment.