Skip to content

Commit

Permalink
made container classes for error detectioons
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWang18 committed Dec 18, 2020
1 parent 9fe2dd7 commit d79f777
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 42 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<version>1.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>

<build>
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/domain/Logic/Errors.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import domain.Pieces.*;

public class Errors {

private Errors(){
throw new IllegalStateException("Silly goose");
}
Expand All @@ -27,7 +26,10 @@ public static void moveException(){
System.out.println("Move did not follow through, try again..");
}

public static void pathIsBlocked(Piece startPiece, Piece blockingPiece, Pair atHere){
public static void pathIsBlocked(Message m){
Piece startPiece = m.startPiece;
Piece blockingPiece = m.blockPiece;
Pair atHere = m.whereXY;
StringBuilder sb = new StringBuilder(startPiece.getReadablePiece() + " cannot hop over the "
+ blockingPiece.getReadablePiece() + " at " + atHere);
System.out.println(sb.toString());
Expand All @@ -37,12 +39,27 @@ public static void piecesBlockingCastle(){
System.out.println("You need to move your other pieces out of the way before castling!");
}

public static void castleIsThreatened(){
System.out.println("You cannot castle as your King will be threatend in the process at ");
public static void castleIsThreatened(Pair xy){
System.out.println("You cannot castle as your King will be threatend in the process at " + xy);
}

public static void cannotCastleActivePieces(){
System.out.println("You have moved your king or rook already... cannot castle");
}
}
class Message{ //would not work as an inner class as i cant make an instance of the Errors util class
protected Piece startPiece;
protected Piece blockPiece;
protected Pair whereXY;
protected boolean value;

public Message(Piece startPiece, Piece blockPiece, Pair whereXY, boolean value){
this.startPiece = startPiece;
this.blockPiece = blockPiece;
this.whereXY = whereXY;
this.value = value;
}
public boolean getBool(){
return this.value;
}
}
96 changes: 58 additions & 38 deletions src/main/java/domain/Logic/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.tuple.ImmutablePair;

import domain.Logic.Color.ColorType;
import domain.Pieces.King;
import domain.Pieces.Pawn;
Expand Down Expand Up @@ -115,7 +117,7 @@ private boolean checkPawnMove(Square start, Square end, Piece pawn, Piece killed

Pawn pPawn = (Pawn) pawn;
boolean test = pPawn.validOrNah(start.getCoord(), end.getCoord(), killedPiece) && checkPiecesPath(
pawn.getPiecePath(start.getCoord(), end.getCoord()), start.getCoord(), end.getCoord());
pawn.getPiecePath(start.getCoord(), end.getCoord()), start.getCoord(), end.getCoord()).value;

if (test) {
executeMove(start, end, pawn, killedPiece); // passes
Expand Down Expand Up @@ -172,7 +174,6 @@ private void executeEnPassant(Square start, Square end, Piece pawn){


private boolean isValidPromotion(Piece pawn, Square start, Square end) { //need 2 check for soundness
if(checkSoundMove(start, end, pawn, null)) return false;

Pair startXY = start.getCoord();
Pair endXY = end.getCoord();
Expand All @@ -197,14 +198,17 @@ private boolean atEndOfBoard(Piece pawn, Pair endXY) {
private boolean checkStandardMove(Square start, Square end, Piece startPiece, Piece killedPiece) {
boolean testValidMove = !isPawn(startPiece)
&& startPiece.validOrNah(start.getCoord(), end.getCoord());

//Message holds data about the legality of move
Message im = checkPiecesPath(startPiece.getPiecePath(start.getCoord(), end.getCoord()),
start.getCoord(), end.getCoord());

boolean testPath = checkPiecesPath(startPiece.getPiecePath(start.getCoord(), end.getCoord()),
start.getCoord(), end.getCoord());
boolean testPath = im.value;

if (testValidMove && testPath) {
executeMove(start, end, startPiece, killedPiece);
} else if (!testPath) {
// raise cannot hop over piece at xy
Errors.pathIsBlocked(im);
return false;
} else {
Errors.pieceBreakRules(start, startPiece);
Expand Down Expand Up @@ -261,7 +265,7 @@ private boolean checkValidCastling(Square kingSQ, Square rookSQ) {
}

if (!checkPiecesPath(rook.getPiecePath(rookSQ.getCoord(), kingSQ.getCoord()), rookSQ.getCoord(),
kingSQ.getCoord())) {
kingSQ.getCoord()).value) {
Errors.piecesBlockingCastle();
return false;
}
Expand All @@ -271,13 +275,30 @@ private boolean checkValidCastling(Square kingSQ, Square rookSQ) {
return false;
}

if (kingIsThreatened(rookSQ.getCoord(), kingSQ.getCoord())) {
if (kingIsThreatened(rookSQ.getCoord(), kingSQ.getCoord())) { //cannot move through an enemy threatened square
return false;
}

return king.getType() == PieceType.KING && rook.getType() == PieceType.ROOK;
}

private boolean kingIsThreatened(Pair rookXY, Pair kingXY) {
int length = Math.abs(rookXY.getX() - kingXY.getX());

// see if a square on the kings horiz move can be reached
for (int i = 0; i < length; i++) {
Pair currentXY = new Pair(kingXY.getX() + i, kingXY.getY());
ImmutablePair<Pair,Boolean> ip = isPieceBeingAttkd(currentXY);

if (Boolean.TRUE.equals(ip.right)){
Errors.castleIsThreatened(ip.left);
return true;
}

}
return false;
}

// check if the square matches any squares in the move list already
private boolean hasPieceMoved(Square sq) {

Expand Down Expand Up @@ -316,21 +337,6 @@ private void castlePieces(Square kingSQ, Square rookSQ) {
switchCurrentPlayer();
}

//
private boolean kingIsThreatened(Pair rookXY, Pair kingXY) {
int length = Math.abs(rookXY.getX() - kingXY.getX());


// see if a square on the kings horiz move can be reached
for (int i = 0; i < length; i++) {
Pair currentXY = new Pair(kingXY.getX() + i, kingXY.getY());
System.out.println("king pos currently is "+ currentXY);
if (isPieceBeingAttkd(currentXY))
return true;
}
return false;
}

private void executeMove(Square start, Square end, Piece startPiece, Piece killedPiece) {
addMove(start, end, startPiece, killedPiece);
switchCurrentPlayer();
Expand All @@ -344,7 +350,7 @@ private void addMove(Square start, Square end, Piece startPiece, Piece killedPie
previousMoves.add(new Move(start, end, startPiece, killedPiece)); // killedPiece could be null,
}

//Scans the current move to see it implicates check
//Scans the current move to see it implicates check..
private boolean moveMakesCheck(Square start, Square end) {
Piece temp = end.getPiece();
end.setPiece(start.getPiece());
Expand All @@ -353,10 +359,12 @@ private boolean moveMakesCheck(Square start, Square end) {
boolean causedCheck = false;

Pair kingXY = getKingPos(board.getBoard()); /*returning null when King is the piece moving and attacking pieces cannot have a valid path to a null end pair*/

if (isPieceBeingAttkd(kingXY)) {
System.out.println(kingXY);

ImmutablePair<Pair,Boolean> ip = isPieceBeingAttkd(kingXY);
if (Boolean.TRUE.equals(ip.right)) {
causedCheck = true;
System.out.println(end.getPiece().getColor() + " is in check");
System.out.println(end.getPiece().getColor() + " is in check at "+ ip.left);
}

start.setPiece(end.getPiece()); // reset it back into place
Expand All @@ -381,7 +389,7 @@ private Pair getKingPos(Square[][] bd) {
/*
param will usually be the Kings position on the board
*/
private boolean isPieceBeingAttkd(Pair endXY) {
private ImmutablePair<Pair,Boolean> isPieceBeingAttkd(Pair endXY) { //return a tuple to store the pair where error message should point
Square[][] bd = board.getBoard();
for (int rank = 0; rank < bd.length; rank++) {
for (int file = 0; file < bd.length; file++) {
Expand All @@ -390,12 +398,12 @@ private boolean isPieceBeingAttkd(Pair endXY) {
Piece pieceHere = sq.getPiece();

if (sq.hasPiece()
&& pieceHasValidPath(pieceHere, sq.getCoord(), endXY))
return true;
&& pieceHasValidPath(pieceHere, sq.getCoord(), endXY)) {
return new ImmutablePair<Pair,Boolean>(endXY,true);}

}
}
return false;
return new ImmutablePair<Pair,Boolean>(endXY, false);
}

/*
Expand All @@ -407,31 +415,33 @@ private boolean pieceHasValidPath(Piece startPiece, Pair startXY, Pair endXY) {
if(startPiece instanceof Pawn){
Pawn pPawn = (Pawn) startPiece;
return !isOwnedPiece(startPiece) && pPawn.validOrNah(startXY, endXY, new King(currentplayer))
&& checkPiecesPath(startPiece.getPiecePath(startXY, endXY), startXY, endXY);
&& checkPiecesPath(startPiece.getPiecePath(startXY, endXY), startXY, endXY).value;
}

//We make sure its not a friendly piece attacking first
//Then check if it is a feasible path within the pieces ruleset
//Finally see if the path has pieces blocking it

return !isOwnedPiece(startPiece) && startPiece.validOrNah(startXY, endXY)
&& checkPiecesPath(startPiece.getPiecePath(startXY, endXY), startXY, endXY);
&& checkPiecesPath(startPiece.getPiecePath(startXY, endXY), startXY, endXY).value;

}

private boolean checkPiecesPath(List<Pair> path, Pair startXY, Pair endXY) { //checking discovered checks is causing error message to be raised even if done implicitly
private Message checkPiecesPath(List<Pair> path, Pair startXY, Pair endXY) { //checking discovered checks is causing error message to be raised even if done implicitly
Square[][] bd = board.getBoard();
Piece startPiece = bd[startXY.getY()][startXY.getX()].getPiece();
boolean flag = true;

Message flag = new Message(startPiece, null, null, true);
//iterate thru list of pair values taken by Pieces path to end goal
for (Pair pair : path) {
if (squareIsOccupied(bd, pair, startXY, endXY)) {
flag = false;
Errors.pathIsBlocked(startPiece, bd[pair.getY()][pair.getX()].getPiece(), pair);
flag = new Message(startPiece, bd[pair.getY()][pair.getX()].getPiece(), pair, false);
//*******Errors.pathIsBlocked(startPiece, bd[pair.getY()][pair.getX()].getPiece(), pair);

break;
}
}

return flag;
}

Expand Down Expand Up @@ -534,5 +544,15 @@ public void printBoard() {
* score = 39 - p.stream().mapToInt(Piece::getValue).sum();
* System.out.println("'s score: " + score); } } to do ---- has logic errors
*/
public class Tuple2<K,V>{

private final K e1;

private final V e2;

public Tuple2(K ele1, V ele2){
this.e1 = ele1;
this.e2 = ele2;
}
}
}
Binary file modified target/classes/domain/Logic/Errors.class
Binary file not shown.
Binary file added target/classes/domain/Logic/Game$Tuple2.class
Binary file not shown.
Binary file modified target/classes/domain/Logic/Game.class
Binary file not shown.
Binary file added target/classes/domain/Logic/Message.class
Binary file not shown.

0 comments on commit d79f777

Please sign in to comment.