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

Commit

Permalink
Disable king move into check and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Procio committed May 24, 2019
1 parent 4f4ccae commit ed552e7
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/main/java/client/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ void runGame() throws Exception{
System.out.println("[client/Runner] updating GUI");
Platform.runLater(updateGUI);
System.out.println("[client/Runner] board:\n" + board.toString());
if(this.board.isOver())
break;
this.color = this.color.otherColor();
if (this.computer) {
System.out.println("[client/Runner] getting move from AI");
Expand Down
86 changes: 71 additions & 15 deletions src/main/java/logic/Board.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package logic;


import javafx.scene.layout.Pane;
import logic.pieces.*;

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

public class Board {
private ArrayList<Piece> pieces;
Expand Down Expand Up @@ -89,8 +91,6 @@ public PosXY[] getComputerMove(PlayerColor color){
}
return ret;
}
/*TODO check if position is attacked in board for King*/
/*TODO change pawn on last square */
/*TODO en passant */
/*TODO castling */
/*TODO check */
Expand All @@ -99,30 +99,80 @@ public Boolean isMoveLegal(PlayerColor color, PosXY[] move){
for (Piece p :
this.pieces) {
if (p.getPosXY().equals(move[0]) && p.getColor().equals(color)) {
return p.moveValid(move[1], state);
/* check if destination is attacked for king move*/
if(p instanceof King){
if(destAttacked(color, move[1]))
return false;
// /* castling */
// if(!((King) p).hasMoved && moveIsCastle(p, move))
// return true;
}
return p.moveValid(move[1], this.state);
}
}
return false;
}
public Integer[][] getState() {
return state;
}
public boolean isOver() {
return this.isOver;

/**
*
* @param color color of our piece
* @param dest destination of our piece
* @return true if enemy piece is attacking square at dest
*/
public Boolean destAttacked(PlayerColor color, PosXY dest){
for (Piece p :
this.pieces) {
if(p.getColor().equals(color))
continue;
/* for pawn only diagonal moves */
if(p instanceof Pawn){
Integer[][] newState = new Integer[this.state.length][];
for (int i = 0; i < this.state.length; i++) {
newState[i] = this.state[i].clone();
}
newState[dest.getX()][dest.getY()] = color.equals(PlayerColor.WHITE) ?
1 : -1;
if(p.moveValid(dest, newState)) {
System.out.println("[Board] square " + dest.toString()
+ " attacked by " + p.toString());
return true;
}
}else if(p.moveValid(dest, this.state)) {
System.out.println("[Board] square " + dest.toString()
+ " attacked by " + p.toString());
return true;
}
}
return false;
}
/*TODO return bool and check in runner */
/*TODO let player choose pawn replacement */
public void move(PosXY[] move){
Piece p = getPiece(move[0]);
if(p == null){
Piece sourcePiece = getPiece(move[0]);
Piece destPiece = getPiece(move[1]);
if(sourcePiece == 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(sourcePiece);
/* Change Pawn */
this.state = sourcePiece.move(move[1], this.state);
sourcePiece.setPosXY(move[1]);
if(sourcePiece instanceof Pawn &&
((sourcePiece.getColor().equals(PlayerColor.WHITE)
&& move[1].getX().equals(0))
||(sourcePiece.getColor().equals(PlayerColor.BLACK)
&& move[1].getX().equals(7)))){
sourcePiece = new Queen(sourcePiece.getColor(),
move[1].getX(), move[1].getY());
}
this.pieces.remove(p);
this.pieces.add(sourcePiece);
if(destPiece == null)
System.out.println("[Board] Couldn't find destination piece");
else
this.pieces.remove(destPiece);
}

Piece getPiece(PosXY pos){
for (Piece p:
this.pieces) {
Expand All @@ -144,6 +194,12 @@ public String toString() {
sb.setLength(sb.length()-1);
return sb.toString();
}
public Integer[][] getState() {
return state;
}
public boolean isOver() {
return this.isOver;
}
public void setState(Integer[][] state) {
this.state = state;
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/logic/Piece.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public Boolean squareOccupied(PosXY to, Integer[][] state){
}
return false;
}
public PlayerColor getColor() {
return color;
}

public void setColor(PlayerColor color) {
this.color = color;
}
Expand All @@ -56,9 +54,15 @@ public Integer getX(){
public Integer getY(){
return this.posXY.getY();
}
public PlayerColor getColor() {
return color;
}
public PosXY getPosXY() {
return posXY;
}
public void setPosXY(PosXY posXY) {
this.posXY = posXY;
}
@Override
public String toString() {
return this.posXY.toString() + " " + this.color.toString();
Expand Down
1 change: 0 additions & 1 deletion src/main/java/logic/pieces/Bishop.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public ArrayList<PosXY> getAllDestinations() {
if(x+i <= 7 && y+i <= 7)
ret.add(new PosXY(x+i, y+i));
}
System.out.println(ret.toString());
return ret;
}

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/logic/pieces/King.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import java.util.Random;

public class King extends Piece {
public Boolean hasMoved;

public King(PlayerColor color, Integer x, Integer y) {
super(color, x, y);
this.hasMoved = false;
}

@Override
Expand All @@ -34,17 +37,23 @@ public ArrayList<PosXY> getAllDestinations() {
int x = this.getX();
int y = this.getY();
if(x-1 >= 0){
ret.add(new PosXY(x-1, y));
if(y-1 >= 0)
ret.add(new PosXY(x-1, y-1));
if(y+1 <= 7)
ret.add(new PosXY(x-1, y+1));
}
if(x+1 <= 7){
ret.add(new PosXY(x+1, y));
if(y-1 >= 0)
ret.add(new PosXY(x+1, y-1));
ret.add(new PosXY(x + 1, y - 1));
if(y+1 <= 7)
ret.add(new PosXY(x+1, y+1));
ret.add(new PosXY(x + 1, y + 1));
}
if (y - 1 >= 0)
ret.add(new PosXY(x, y-1));
if(y+1 <= 7)
ret.add(new PosXY(x, y+1));
return ret;
}
@Override
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/server/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void run() {
while (true) {
System.out.println("[Connection] Server waiting for move from client...");
recvMove();
System.out.println("[Connection] board:\n" + this.board.toString());
this.color = this.color.equals(PlayerColor.WHITE) ?
PlayerColor.BLACK : PlayerColor.WHITE;
}
Expand All @@ -62,6 +63,7 @@ private void recvMove() throws Exception {
System.out.println("[Connection] Server received move:\n" + moveString);
move = ClientCommunication.posFromString(moveString);
if (this.board.isMoveLegal(this.color, move)) {
this.board.move(move);
this.outToClient.writeBytes("ok\n");
break;
}
Expand Down
Binary file removed target/classes/client/GUI/BoardPane$1.class
Binary file not shown.
Binary file modified target/classes/client/GUI/BoardPane.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 modified target/classes/client/Runner.class
Binary file not shown.
Binary file modified target/classes/logic/Board.class
Binary file not shown.
Binary file modified target/classes/logic/Piece.class
Binary file not shown.
Binary file modified target/classes/logic/pieces/Bishop.class
Binary file not shown.
Binary file modified target/classes/logic/pieces/King.class
Binary file not shown.
Binary file modified target/classes/server/Connection.class
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
client/GUI/BoardPane$1.class
server/Connection.class
logic/PieceValue.class
logic/pieces/King.class
Expand Down

0 comments on commit ed552e7

Please sign in to comment.