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

Commit

Permalink
fixed computer move under check, Board still gets desynchronized from…
Browse files Browse the repository at this point in the history
… BoardPane though
  • Loading branch information
Adam Procio committed Dec 7, 2020
1 parent 30a2855 commit 90f4e29
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/main/java/client/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import logic.PlayerColor;
import logic.PosXY;

/* TODO BoardPane gets desynchronized from Board */
/**
* Runner class for client
*
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/logic/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logic.pieces.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -98,6 +99,7 @@ public Move getComputerMove(PlayerColor color){
ArrayList<Piece> tempPieces = new ArrayList<>(this.pieces);
while(ret == null && !tempPieces.isEmpty()) {
Piece p = tempPieces.remove(rand.nextInt(tempPieces.size()));
LOGGER.log(Level.ALL, "Finding move for " + p.toString());
if(p.getColor().equals(color.otherColor()))
continue;
ArrayList<PosXY> dests = p.getAllValidDests(this.state);
Expand All @@ -121,19 +123,22 @@ public Boolean isMoveLegal(PlayerColor color, Move move){
if (p.getPos().equals(move.getSrc()) && p.getColor().equals(color)) {
/* if king is attacked, check if move stops check */
if(isKingAttacked(color)){
LOGGER.log(Level.INFO, "CHECK");
/* check state after move, if move stops check, return p.moveValid(), otherwise false */
/* apply given move on Board and check if king still checked,
then return to orig state and return accordingly */
Boolean ret = true;
Integer[][] savedState = this.state.clone();
Integer[][] savedState = cloneState(this.state);
ArrayList<Piece> savedPieces = new ArrayList<>(this.pieces);
this.state = p.move(move.getDest(), savedState);
if(isKingAttacked(color)) ret = false;
this.state = savedState;
this.state = p.move(move.getDest(), this.state);
if(isKingAttacked(color))
ret = false;
this.state = cloneState(savedState);
this.pieces = new ArrayList<>(savedPieces);
return p.moveValid(move.getDest(), this.state);
p.setPos(move.getSrc());
return ret ? p.moveValid(move.getDest(), this.state) : false;
}
/* check if destination is attacked for king move*/
/* check if destination is attacked for king move */
if(p instanceof King){
if(posAttacked(color, move.getDest())) return false;
if(moveIsCastle(p, move)) return true;
Expand Down Expand Up @@ -319,6 +324,16 @@ public void castle(Move move){
}
}
}

public static Integer[][] cloneState(Integer[][] state){
Integer[][] clone = new Integer[8][8];
int i = 0;
for (Integer[] arr : state) {
clone[i] = Arrays.copyOf(arr, 8);
++i;
}
return clone;
}

public Piece getPiece(PosXY pos){
for (Piece p:
Expand Down
Binary file modified target/classes/logic/Board.class
Binary file not shown.

0 comments on commit 90f4e29

Please sign in to comment.