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

Commit

Permalink
piece logic except en passant, castling and king moves, untested
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Procio committed May 20, 2019
1 parent f5fdbdc commit da6aebd
Show file tree
Hide file tree
Showing 42 changed files with 439 additions and 140 deletions.
20 changes: 1 addition & 19 deletions src/main/java/client/GUI/BoardPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public class BoardPane extends GridPane {
private PosXY[] move;
private PlayerColor playerColor;
private LinkedBlockingQueue<Boolean> outMoveReady;
private LinkedBlockingQueue<Boolean> outMoveOkay;
private LinkedBlockingQueue<Boolean> inMoveReady;

/*TODO flip board based on color */
public BoardPane(Integer[][] state, PlayerColor color){
Expand Down Expand Up @@ -71,16 +69,13 @@ public void handle(MouseEvent mouseEvent) {
c++;
}
this.outMoveReady = new LinkedBlockingQueue<Boolean>(1);
this.outMoveOkay = new LinkedBlockingQueue<Boolean>(1);
this.inMoveReady = new LinkedBlockingQueue<Boolean>(1);
}

/**
* Set move based on mouseEvent and wait for move in moveReady
* @param mouseEvent
*/
public void setMove(MouseEvent mouseEvent){
System.out.println("setMove()");
if(this.move[0] != null && this.move[1] != null){
this.move[0] = null;
this.move[1] = null;
Expand All @@ -89,7 +84,7 @@ public void setMove(MouseEvent mouseEvent){
this.move[0] = (PosXY) ((Node) mouseEvent.getSource()).getUserData();
}else{
this.move[1] = (PosXY) ((Node) mouseEvent.getSource()).getUserData();
/* get move out to GameScreen */
/* get move out to Runner */
try {
this.outMoveReady.put(true);
}catch (Exception e){
Expand All @@ -104,20 +99,13 @@ public void setMoveButtons(){
y1 = this.move[0].getY(),
y2 = this.move[1].getY();
String pieceTxt = this.squareButtons[x1][y1].getText();
System.out.println("we here");
for (Node node :
this.getChildren()) {
if (GridPane.getRowIndex(node).equals(x1) && GridPane.getColumnIndex(node).equals(y1))
((Button) node).setText("");
if (GridPane.getRowIndex(node).equals(x2) && GridPane.getColumnIndex(node).equals(y2))
((Button) node).setText(pieceTxt);
}
// this.buttons[x1][y1].setText("");
// this.buttons[x2][y2].setText(pieceTxt);
// this.boardPane.getChildren().remove(y1, x1);
// this.boardPane.getChildren().remove(y2, x2);
// this.boardPane.add(this.buttons[x1][y1], y1, x1);
// this.boardPane.add(this.buttons[x2][y2], y2, x2);
}
public void setButtons(){
for (int i = 0; i < this.state.length; i++) {
Expand Down Expand Up @@ -169,12 +157,6 @@ public void setButtons(){
public LinkedBlockingQueue<Boolean> getOutMoveReady() {
return outMoveReady;
}
public LinkedBlockingQueue<Boolean> getOutMoveOkay() {
return outMoveOkay;
}
public LinkedBlockingQueue<Boolean> getInMoveReady() {
return inMoveReady;
}
public PosXY[] getMove() {
return move;
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/client/GUI/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ public GameScreen(Stage stage, Integer[][] state, PlayerColor color){
public BoardPane getBoardPane() {
return boardPane;
}

}
157 changes: 105 additions & 52 deletions src/main/java/client/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import client.GUI.GameScreen;
import client.GUI.MenuScreen;
import client.communication.Utils;
import client.communication.ClientCommunication;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Service;
Expand All @@ -21,39 +21,85 @@

/*TODO adaptive GUI to window size */
public class Runner extends Application{
private Utils comm;
private ClientCommunication comm;
private int port;
private MenuScreen ms;
private GameScreen gameScreen;
private Board board;
private PosXY[] move;
private PlayerColor color;
private Service<Void> service;
private Service<Void> gameService;
private Task<Void> task;
private Boolean remote;
private Boolean computer;

public static void main(String[] args){
launch();
}
/*TODO connect to remote game */
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("Game vs computer clicked");
runGame(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);
}
};
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");
setPort(stage);
}
});
this.ms.getTf().setOnAction(this.ms.getEh());
this.ms.getVsComputerBtn().setOnMouseClicked(vsCompBtnEh);
this.ms.getTwoPlayerBtn().setOnMouseClicked(twoPlayerBtnEh);
this.ms.mainMenu(stage);
}

/**
* @param stage stage for GameScreen
* @param remote true if playing online
* @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 runGame(Stage stage, Boolean remote,
Boolean computer, final PlayerColor color) {
/* Garbage collector should clean ms */
this.ms = null;
/* setup runner */
this.color = color;
this.computer = computer;
this.remote = remote;
this.board = new Board();
if(!remote) {
/* run server locally */
server.Runner server = new server.Runner(remote);
if(!this.remote) {
System.out.println("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);
new Thread(server).start();
}
this.comm = new Utils(this.port, remote, computer);
this.comm = new ClientCommunication(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();
}

/**
* service running in parallel with GUI without blocking it
*/
void setupGameService(){
/* get move from GUI, check, update gui,
* send to server, wait for response, update gui */
this.task = new Task<Void>() {
Expand All @@ -68,70 +114,77 @@ public void run() {
}
}
};
getMove();
Platform.runLater(updateGUI);
latch.await();
comm.getOutMove().put(move);
move = comm.getInMove().take();
gameScreen.getBoardPane().setMove(move);
Platform.runLater(updateGUI);
/* 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);
}
}
return null;
}
};
this.service = new Service<Void>() {
this.gameService = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return task;
}
};
this.service.start();
}

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

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

}
}

public static void main(String[] args){
launch();
}

/*TODO connect to remote game */
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("Game vs computer clicked");
runGame(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);
}
};
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");
setPort(stage);
}
});
this.ms.getTf().setOnAction(this.ms.getEh());
this.ms.getVsComputerBtn().setOnMouseClicked(vsCompBtnEh);
this.ms.getTwoPlayerBtn().setOnMouseClicked(twoPlayerBtnEh);
this.ms.mainMenu(stage);
}

private void setPort(final Stage stage){
try {
/* get port from user to connect to in the GUI*/
Expand Down
Loading

0 comments on commit da6aebd

Please sign in to comment.