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

Commit

Permalink
Added chess pieces to GUI
Browse files Browse the repository at this point in the history
Having trouble with getting move out of gui to pass to communication
thread.
  • Loading branch information
Adam Procio committed May 6, 2019
1 parent f78ddf8 commit b5768a6
Show file tree
Hide file tree
Showing 33 changed files with 233 additions and 78 deletions.
170 changes: 146 additions & 24 deletions src/main/java/client/GUI/BoardGUI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package client.GUI;

import client.communication.Utils;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
Expand All @@ -8,56 +9,109 @@
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import logic.Board;
import logic.PlayerColor;
import logic.PosXY;

public class BoardGUI{
import java.util.concurrent.LinkedBlockingQueue;

public class BoardGUI {
private Thread cliThread,
servThread;
private int port;
private Scene scene;
private GridPane pane, boardPane;
private EventHandler<MouseEvent> gridButtonHandler;
private Button saveGame;
private Board board;
private Integer[][] state;
private Button[][] buttons;
private PosXY[] move;
private LinkedBlockingQueue<PosXY[]> moves;
private PlayerColor color;

public BoardGUI(Integer[][] state){
this.state = state;
public BoardGUI(Integer[][] state, Board board, LinkedBlockingQueue<PosXY[]> moves){
if(state == null)
this.state = board.getState();
else
this.state = state;
for (int i = 0; i < this.state.length; i++) {
for (int j = 0; j < this.state[i].length; j++) {
System.out.printf(this.state[i][j] + " ");
}
System.out.println();
}
this.board = board;
this.moves = moves;
this.move = new PosXY[2];
this.move[0] = null;
this.move[1] = null;
this.buttons = new Button[8][8];
this.gridButtonHandler = new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
if(move[0] != null && move[1] != null){
move[0] = null;
move[1] = null;
}
if (move[0] == null) {
move[0] = (PosXY) ((Node) mouseEvent.getSource()).getUserData();
}else{
move[1] = (PosXY) ((Node) mouseEvent.getSource()).getUserData();
System.out.println(move[0].toString() + "\n" + move[1].toString());
}
setMove(mouseEvent);
}
};
}

public void runGame2Players() throws InterruptedException {
boolean whiteTurn = true;
/*TODO 1. get move from gui
TODO 2. process it in board
TODO 3. send to server
TODO 4. wait for server response (move ok, not ok)
TODO 5. if move not ok -> 1.
TODO 6. wait for new state(move?) from server
TODO 7. update gui
TODO 8. 1.
*/
while(!this.board.isOver()){
System.out.println("we here");
PosXY move[] = this.moves.take();
if(!this.board.isMoveLegal(whiteTurn, move))
System.out.println("we here");
}
}

public void setMove(MouseEvent mouseEvent){
if(move[0] != null && move[1] != null){
move[0] = null;
move[1] = null;
}
if (move[0] == null) {
move[0] = (PosXY) ((Node) mouseEvent.getSource()).getUserData();
}else{
move[1] = (PosXY) ((Node) mouseEvent.getSource()).getUserData();
//TODO test in board, if correct, move is stored in board
try {
moves.put(move);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public Integer[][] getState() {
return state;
}

public void start(Stage stage) throws Exception {
public void start(Stage stage, Boolean remote, Boolean computer, int port) throws Exception {
this.color = PlayerColor.WHITE;
this.port = port;
this.pane = new GridPane();
this.pane.setAlignment(Pos.CENTER);
this.boardPane = new GridPane();
//this.boardPane.setGridLinesVisible(true);
this.boardPane.setAlignment(Pos.CENTER);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
Button button = new Button("");
button.setMinSize(MinDims.BUTTON.width, MinDims.BUTTON.height);
button.setUserData(new PosXY(i, j));
button.setOnMouseClicked(this.gridButtonHandler);
this.boardPane.add(button, i, j);
button.setStyle("-fx-font: 18 arial;");
this.buttons[i][j] = button;
this.boardPane.add(buttons[i][j], j, i);
}
}
char c = 'a';
Expand All @@ -68,6 +122,8 @@ public void start(Stage stage) throws Exception {
label1.setMinSize(MinDims.BUTTON.width, MinDims.BUTTON.height);
label.setAlignment(Pos.CENTER);
label1.setAlignment(Pos.CENTER);
label.setStyle("-fx-font: 16 arial;");
label1.setStyle("-fx-font: 16 arial;");
this.boardPane.add(label1, 8, 7-i);
this.boardPane.add(label, i, 8);
c++;
Expand All @@ -77,14 +133,80 @@ public void start(Stage stage) throws Exception {
this.pane.add(this.saveGame, 0, 1);
this.scene = new Scene(this.pane);
stage.setScene(this.scene);
stage.show();
if(!remote) {
/* run server locally */
server.Runner server = new server.Runner();
this.port = server.getPort();
this.servThread = new Thread(server);
this.servThread.start();
}
this.cliThread = new Thread(new Utils(this.port));
this.cliThread.start();
//if(computer){
// //TODO run game vs computer
// //runGameVsComputer();
//}else if(remote){
// //TODO run game vs player
// //runGameVsPlayer();
//}else {
// //two players 1 pc
// runGame2Players();
//}
updateGUI();
}

/**TODO get move from gui (square from, square to)
*/

public void updateGUI(Integer[][] state){
/*TODO poll each square and for each piece that doesn't
*TODO correspond update button
*/
public void updateGUI(){
for (int i = 0; i < this.state.length; i++) {
for (int j = 0; j < this.state[i].length; j++) {
switch(this.state[i][j]) {
case 1:
this.buttons[i][j].setText(new String(Character.toChars(ChessSymbols.WHITE_PAWN.val)));
break;
case 2:
this.buttons[i][j].setText(new String(Character.toChars(ChessSymbols.WHITE_KNIGHT.val)));
break;
case 3:
this.buttons[i][j].setText(new String(Character.toChars(ChessSymbols.WHITE_BISHOP.val)));
break;
case 4:
this.buttons[i][j].setText(new String(Character.toChars(ChessSymbols.WHITE_ROOK.val)));
break;
case 5:
this.buttons[i][j].setText(new String(Character.toChars(ChessSymbols.WHITE_QUEEN.val)));
break;
case 6:
this.buttons[i][j].setText(new String(Character.toChars(ChessSymbols.WHITE_KING.val)));
break;
case -1:
this.buttons[i][j].setText(new String(Character.toChars(ChessSymbols.BLACK_PAWN.val)));
break;
case -2:
this.buttons[i][j].setText(new String(Character.toChars(ChessSymbols.BLACK_KNIGHT.val)));
break;
case -3:
this.buttons[i][j].setText(new String(Character.toChars(ChessSymbols.BLACK_BISHOP.val)));
break;
case -4:
this.buttons[i][j].setText(new String(Character.toChars(ChessSymbols.BLACK_ROOK.val)));
break;
case -5:
this.buttons[i][j].setText(new String(Character.toChars(ChessSymbols.BLACK_QUEEN.val)));
break;
case -6:
this.buttons[i][j].setText(new String(Character.toChars(ChessSymbols.BLACK_KING.val)));
break;
default:
this.buttons[i][j].setText("");
break;
}
}
}
}
public Board getBoard(){
return this.board;
}
public LinkedBlockingQueue<PosXY[]> getMoves(){
return this.moves;
}
}
22 changes: 22 additions & 0 deletions src/main/java/client/GUI/ChessSymbols.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package client.GUI;

public enum ChessSymbols {
WHITE_KING (9812),
WHITE_QUEEN (9813),
WHITE_ROOK (9814),
WHITE_BISHOP (9815),
WHITE_KNIGHT (9816),
WHITE_PAWN (9817),
BLACK_KING (9818),
BLACK_QUEEN (9819),
BLACK_ROOK (9820),
BLACK_BISHOP (9821),
BLACK_KNIGHT (9822),
BLACK_PAWN (9823);

public int val;

ChessSymbols(int value){
this.val = value;
}
}
40 changes: 6 additions & 34 deletions src/main/java/client/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,25 @@
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import logic.Board;
import logic.PosXY;

import java.util.concurrent.LinkedBlockingQueue;


public class Runner extends Application{
private Integer port;
private int port;
private MenuScreen ms;
private BoardGUI boardGUI;
private Board board;
private Thread logicThread,
cliThread,
servThread;

private void runGame(Stage stage, Boolean remote, Boolean computer){
private void runGame(Stage stage, Boolean remote, Boolean computer) {
/* Garbage collector should clean ms */
this.ms = null;
this.boardGUI = new BoardGUI(null);
this.boardGUI = new BoardGUI(null, new Board(),
new LinkedBlockingQueue<PosXY[]>(4));
try {
this.boardGUI.start(stage);
this.boardGUI.start(stage, remote, computer, this.port);
}catch (Exception e){
e.printStackTrace();
}
if(!remote) {
/* queue for server port */
LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>();
/* run server */
server.Runner server = new server.Runner();
this.port = server.getPort();
this.servThread = new Thread(server);
this.servThread.start();
}
this.cliThread = new Thread(new Utils(this.port));
this.cliThread.start();
this.board = new Board();
this.logicThread = new Thread(this.board);
this.logicThread.start();
while(true){
/*TODO 1. get move from gui
TODO 2. process it in board
TODO 3. send to server
TODO 4. wait for server response (move ok, not ok)
TODO 5. if move not ok -> 1.
TODO 6. wait for new state(move?) from server
TODO 7. update gui
TODO 8. 1.
*/
}
}

public static void main(String[] args) throws Exception {
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/logic/Board.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package logic;

public class Board implements Runnable{
public class Board {
private Player whitePlayer;
private Player blackPlayer;
private Integer[][] state;
private boolean isOver;

public Board(){
/* default setup */
this.isOver = false;
this.state = new Integer[][]{
{-4,-2,-3,-5,-6,-3,-2,-4},
{-1,-1,-1,-1,-1,-1,-1,-1},
Expand All @@ -17,22 +19,30 @@ public Board(){
{ 1, 1, 1, 1, 1, 1, 1, 1},
{ 4, 2, 3, 6, 5, 3, 2, 4}
};
this.whitePlayer = new Player(true, this.state);
this.blackPlayer = new Player(false, this.state);
this.whitePlayer = new Player(PlayerColor.WHITE, this.state);
this.blackPlayer = new Player(PlayerColor.BLACK, this.state);
}

public boolean isMoveLegal(boolean color, PosXY[] move){
if(color == PlayerColor.WHITE.color) {
//white player
return false;
}else{
//black player
return true;
}
//return false;
}
public Board(Integer[][] state){
this.state = state;
}

public Integer[][] getState() {
return state;
}

public void setState(Integer[][] state) {
this.state = state;
}

public void run() {
System.out.println("Running Board logic");
public boolean isOver() {
return this.isOver;
}
}
4 changes: 2 additions & 2 deletions src/main/java/logic/Piece.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package logic;

public abstract class Piece implements PieceIF{
Boolean color;
PlayerColor color;
PosXY posXY;

public Piece(Boolean color, Integer x, Integer y){
public Piece(PlayerColor color, Integer x, Integer y){
this.color = color;
this.posXY = new PosXY(x,y);
}
Expand Down
Loading

0 comments on commit b5768a6

Please sign in to comment.