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

Commit

Permalink
restructuring before finishing up logic, bigger GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Procio committed May 24, 2019
1 parent b11807f commit 4f4ccae
Show file tree
Hide file tree
Showing 18 changed files with 113 additions and 140 deletions.
12 changes: 4 additions & 8 deletions src/main/java/client/GUI/BoardPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,14 @@ public BoardPane(Integer[][] state, PlayerColor color){
this.move = new PosXY[2];
this.setAlignment(Pos.CENTER);
this.squareButtons = new Button[8][8];
this.gridButtonHandler = new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
setMove(mouseEvent);
}
};
this.gridButtonHandler = mouseEvent -> setMove(mouseEvent);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
this.squareButtons[i][j] = new Button("");
this.squareButtons[i][j].setMinSize(MinDims.BUTTON.width, MinDims.BUTTON.height);
this.squareButtons[i][j].setUserData(new PosXY(i, j));
this.squareButtons[i][j].setOnMouseClicked(this.gridButtonHandler);
this.squareButtons[i][j].setStyle("-fx-font: 18 arial;");
this.squareButtons[i][j].setStyle("-fx-font: 30 arial;");
if((i+j) % 2 != 0)
this.squareButtons[i][j].setBackground(new Background(
new BackgroundFill(Paint.valueOf("grey"),
Expand All @@ -62,8 +58,8 @@ public void handle(MouseEvent mouseEvent) {
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;");
label.setStyle("-fx-font: 30 arial;");
label1.setStyle("-fx-font: 30 arial;");
this.add(label1, 8, 7-i);
this.add(label, i, 8);
c++;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/client/GUI/MinDims.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package client.GUI;

public enum MinDims{
BUTTON (40, 40),
SCENE (400, 400);
BUTTON (80, 80),
SCENE (800, 800);

public int width, height;

Expand Down
15 changes: 5 additions & 10 deletions src/main/java/client/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class Runner extends Application{
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"));
Expand Down Expand Up @@ -105,11 +104,7 @@ private void setupAndRun(Stage stage, Boolean remote,
}

void runGame() throws Exception{
Runnable updateGUI = new Runnable() {
public void run() {
gameScreen.getBoardPane().setMoveButtons();
}
};
Runnable updateGUI = () -> gameScreen.getBoardPane().setMoveButtons();
/* game vs remote player */
if(this.remote) {
/* we are black -> enemy starting */
Expand All @@ -125,7 +120,7 @@ public void run() {
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);
this.board.move(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");
Expand All @@ -147,7 +142,7 @@ public void run() {
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);
this.board.move(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");
Expand All @@ -160,7 +155,7 @@ public void run() {
System.out.println("[client/Runner] getting move from AI");
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, this.move);
this.board.move(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");
Expand All @@ -174,7 +169,7 @@ public void run() {
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);
this.board.move(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");
Expand Down
124 changes: 100 additions & 24 deletions src/main/java/logic/Board.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package logic;


import logic.pieces.*;

import java.util.ArrayList;
import java.util.Random;

public class Board {
private Player whitePlayer;
private Player blackPlayer;
private ArrayList<Piece> pieces;
private Integer[][] state;
private boolean isOver;

Expand All @@ -20,31 +24,112 @@ public Board(){
{ 1, 1, 1, 1, 1, 1, 1, 1},
{ 4, 2, 3, 5, 6, 3, 2, 4}
};
this.whitePlayer = new Player(PlayerColor.WHITE, this.state);
this.blackPlayer = new Player(PlayerColor.BLACK, this.state);
this.pieces = new ArrayList<>();
setPieces();
}
public Board(Integer[][] state){
this.isOver = false;
this.state = state;
this.pieces = new ArrayList<>();
setPieces();
}
public void setPieces(){
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
switch (state[i][j]){
case 1:
this.pieces.add(new Pawn(PlayerColor.WHITE, i, j));
break;
case -1:
this.pieces.add(new Pawn(PlayerColor.BLACK, i, j));
break;
case 2:
this.pieces.add(new Knight(PlayerColor.WHITE, i, j));
break;
case -2:
this.pieces.add(new Knight(PlayerColor.BLACK, i, j));
break;
case 3:
this.pieces.add(new Bishop(PlayerColor.WHITE, i, j));
break;
case -3:
this.pieces.add(new Bishop(PlayerColor.BLACK, i, j));
break;
case 4:
this.pieces.add(new Rook(PlayerColor.WHITE, i, j));
break;
case -4:
this.pieces.add(new Rook(PlayerColor.BLACK, i, j));
break;
case 5:
this.pieces.add(new Queen(PlayerColor.WHITE, i, j));
break;
case -5:
this.pieces.add(new Queen(PlayerColor.BLACK, i, j));
break;
case 6:
this.pieces.add(new King(PlayerColor.WHITE, i, j));
break;
case -6:
this.pieces.add(new King(PlayerColor.BLACK, i, j));
break;
}
}
}
}
public PosXY[] getComputerMove(PlayerColor color){
return color.equals(PlayerColor.WHITE) ?
this.whitePlayer.getComputerMove(this.state):
this.blackPlayer.getComputerMove(this.state);
PosXY[] ret = null;
Random rand = new Random();
ArrayList<Piece> tempPieces = new ArrayList<Piece>(this.pieces);
while(ret == null && !tempPieces.isEmpty()) {
Piece p = tempPieces.remove(rand.nextInt(tempPieces.size()));
if(p.getColor().equals(color.otherColor()))
continue;
ret = p.getRandomMove(state);
}
return ret;
}
/*TODO change pawn on last square */
/*TODO check if position is attacked in board for King*/
/*TODO change pawn on last square */
/*TODO en passant */
/*TODO castling */
/*TODO check */
/*TODO check mate */
public Boolean isMoveLegal(PlayerColor color, PosXY[] move){
if(color.equals(PlayerColor.WHITE))
return this.whitePlayer.isMoveLegal(move, this.state);
return this.blackPlayer.isMoveLegal(move, this.state);
for (Piece p :
this.pieces) {
if (p.getPosXY().equals(move[0]) && p.getColor().equals(color)) {
return p.moveValid(move[1], state);
}
}
return false;
}
public Integer[][] getState() {
return state;
}
public void setState(Integer[][] state) {
this.state = state;
public boolean isOver() {
return this.isOver;
}
public void move(PosXY[] move){
Piece p = getPiece(move[0]);
if(p == null){
System.out.println("[Board] Couldn't find source piece");
return;
}
this.state = p.move(move[1], this.state);
p = getPiece(move[1]);
if(p != null){
System.out.println("[Board] Couldn't find destination piece");
}
this.pieces.remove(p);
}
Piece getPiece(PosXY pos){
for (Piece p:
this.pieces) {
if(p.getPosXY().equals(pos))
return p;
}
return null;
}
@Override
public String toString() {
Expand All @@ -59,16 +144,7 @@ public String toString() {
sb.setLength(sb.length()-1);
return sb.toString();
}
public boolean isOver() {
return this.isOver;
}
public void move(PlayerColor color, PosXY[] move){
if(color.equals(PlayerColor.WHITE)) {
this.state = whitePlayer.move(move[0], move[1], this.state);
this.blackPlayer.enemyMove(move[1]);
}else {
this.state = blackPlayer.move(move[0], move[1], this.state);
this.whitePlayer.enemyMove(move[1]);
}
public void setState(Integer[][] state) {
this.state = state;
}
}
91 changes: 0 additions & 91 deletions src/main/java/logic/Player.java

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/java/logic/pieces/King.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public Integer[][] move(PosXY to, Integer[][] state) {
}

public Boolean moveValid(PosXY to, Integer[][] state) {
if(!this.getAllDestinations().contains(to))
return false;
if(state[to.getX()][to.getY()] < 0 && this.getColor().equals(PlayerColor.BLACK))
return false;
if(state[to.getX()][to.getY()] > 0 && this.getColor().equals(PlayerColor.WHITE))
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/LogicTest.java

This file was deleted.

Binary file modified target/classes/client/GUI/BoardPane.class
Binary file not shown.
Binary file modified target/classes/client/GUI/MinDims.class
Binary file not shown.
Binary file modified target/classes/client/Runner$1.class
Binary file not shown.
Binary file modified target/classes/client/Runner$2.class
Binary file not shown.
Binary file removed target/classes/client/Runner$3.class
Binary file not shown.
Binary file modified target/classes/client/Runner.class
Binary file not shown.
Binary file modified target/classes/logic/Board.class
Binary file not shown.
Binary file removed target/classes/logic/Player.class
Binary file not shown.
Binary file modified target/classes/logic/pieces/King.class
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ client/GUI/BoardPane$1.class
server/Connection.class
logic/PieceValue.class
logic/pieces/King.class
client/Runner$3.class
client/Runner$2.class
client/GUI/ChessSymbols.class
Main.class
Expand All @@ -23,6 +22,5 @@ logic/Piece.class
logic/pieces/Rook.class
logic/PlayerColor.class
client/GUI/MenuScreen.class
logic/Player.class
client/Runner$1.class
client/Runner.class
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
/home/adam_procio/School/PJV/prociada/src/main/java/logic/PlayerColor.java
/home/adam_procio/School/PJV/prociada/src/main/java/client/GUI/MinDims.java
/home/adam_procio/School/PJV/prociada/src/main/java/logic/pieces/King.java
/home/adam_procio/School/PJV/prociada/src/main/java/logic/Player.java
/home/adam_procio/School/PJV/prociada/src/main/java/logic/PosXY.java
/home/adam_procio/School/PJV/prociada/src/main/java/server/Runner.java
/home/adam_procio/School/PJV/prociada/src/main/java/client/GUI/BoardPane.java
Expand Down

0 comments on commit 4f4ccae

Please sign in to comment.