Skip to content

Commit

Permalink
added checkmate detection
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWang18 committed Dec 21, 2020
1 parent 4591411 commit 819d93b
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 36 deletions.
4 changes: 4 additions & 0 deletions src/main/java/domain/Logic/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public void clearBoard(){
}
}

public static int getSize(){
return SIZE;
}

public void setWhitePieces(){
for (int file = 0; file < bd.length; file++) {
bd[6][file].setPiece(new Pawn(ColorType.White));
Expand Down
83 changes: 58 additions & 25 deletions src/main/java/domain/Logic/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

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

Expand Down Expand Up @@ -45,7 +46,7 @@ public static Game getGame() { // Implementation of Singleton Pattern, Only one
return g;
}

public boolean isCheckMate() {
public boolean isGameOver() {
return over;
}

Expand Down Expand Up @@ -77,8 +78,8 @@ public boolean tryMove(int startx, int starty, int endx, int endy) {
if (isPawn(startPiece)) {
return checkPawnMove(start, end, startPiece, killedPiece);
}

if (checkValidCastling(start, end)) {
if (startPiece.getColor() == killedPiece.getColor() && checkValidCastling(start, end)) {
castlePieces(start, end);
return true;
}
Expand All @@ -102,7 +103,7 @@ private boolean checkPawnMove(Square start, Square end, Piece pawn, Piece killed

if (isValidPromotion(pawn, start, end)) { // checks if it is promotion move
/*
TODO
could offer better signifers bc just executing move says a queen made promotion
*/
executeMove(start, end, pawn, killedPiece);
Expand Down Expand Up @@ -229,9 +230,13 @@ private boolean checkSoundMove(Square start, Square end, Piece startPiece, Piece
return false;
}

test = moveMakesCheck(start, end);
ImmutablePair<Pair, Boolean> testCheck = moveMakesCheck(start, end);

if (test) {
if (Boolean.TRUE == testCheck.right) {
if(isCheckMate(testCheck.left)){
over = true;
return false;
}
System.out.println("Cannot move " + startPiece.getReadablePiece() + " at " + start.getCoord()
+ " as you are in check");

Expand Down Expand Up @@ -347,7 +352,7 @@ private void addMove(Square start, Square end, Piece startPiece, Piece killedPie
}

//Scans the current move to see it implicates check..
private boolean moveMakesCheck(Square start, Square end) {
private ImmutablePair<Pair, Boolean> moveMakesCheck(Square start, Square end) {
Piece temp = end.getPiece();
end.setPiece(start.getPiece());
start.killPiece();
Expand All @@ -366,7 +371,50 @@ private boolean moveMakesCheck(Square start, Square end) {
start.setPiece(end.getPiece()); // reset it back into place
end.setPiece(temp);

return causedCheck;
return new ImmutablePair<Pair,Boolean>(kingXY, causedCheck);
}

private boolean isCheckMate(Pair currentKingXY) { //change method header name{

int x = currentKingXY.getX();
int y = currentKingXY.getY();
int GIRTH = Board.getSize();

List<Pair> possibleKingPos = new ArrayList<>(8);

//Adding possible moves for the passed king

if((y + 1) < GIRTH){ //upper level
possibleKingPos.add(new Pair(x, y + 1));
if(x - 1 >= 0){
possibleKingPos.add(new Pair(x-1, y+1));
}
if(x + 1 < GIRTH){
possibleKingPos.add(new Pair(x+1, y+1));
}
}

if((y - 1) >= 0){ //lower
possibleKingPos.add(new Pair(x,y-1));
if(x - 1 >= 0){
possibleKingPos.add(new Pair(x-1, y-1));
}
if(x + 1 < GIRTH){
possibleKingPos.add(new Pair(x+1, y-1));
}
}

if(x - 1 >= 0){ //same level
possibleKingPos.add(new Pair(x-1,y));
}
if(x + 1 < GIRTH){
possibleKingPos.add(new Pair(x+1,y));
}

List<? extends Pair> validKingMoves = possibleKingPos.stream().filter(p -> !isPieceBeingAttkd(p).right).collect(Collectors.toList());

//if list is empty than there is no valid place for the king to go
return validKingMoves.isEmpty();
}

private Pair getKingPos(Square[][] bd) {
Expand Down Expand Up @@ -520,23 +568,8 @@ public void printBoard() {
board.showBoard();
// bSystem.out.println("\t White");
}

/*
* private boolean isCheckMate(){ if(!isCheck(currentplayer)) return false;
* Square[][] bd = board.getBoard(); List<Pair> possibleKingPos = new
* ArrayList<>(8); Pair currentKingPos = getKingPos(board.getBoard(), 0, 0); int
* x = currentKingPos.getX(); int y = currentKingPos.getY(); //if not at end or
* edge of the board 8 possible moves for(int rank = 0; rank < 3; rank++){
* //right if(!bd[x+1][y+rank].hasPiece()) possibleKingPos.add(new Pair(x+1,
* y+rank));
*
* } for(int rank = 0; rank < 3; rank++){ //left if(!bd[x-1][y+rank].hasPiece())
* possibleKingPos.add(new Pair(x-1, y+rank)); } for(int rank = 0; rank < 2;
* rank++){ //left if(!bd[x][y+rank].hasPiece()) possibleKingPos.add(new
* Pair(x-1, y+rank)); } return false; //get current King moves, }
*/
/*
* private void showScore() { for (List<Piece> p : pieceskilled.values()) { int

/* private void showScore() { for (List<Piece> p : pieceskilled.values()) { int
* score = 39 - p.stream().mapToInt(Piece::getValue).sum();
* System.out.println("'s score: " + score); } } to do ---- has logic errors
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/domain/Pieces/Rook.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Rook(ColorType color) {

@Override
public String toString() {
if(super.getColor() == ColorType.Black) return "b";
if(super.getColor() == ColorType.Black) return "r";

return "R";
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/domain/UserInterface/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class App {
public static void main(String... strings) throws IOException
{


Runnable ui = new UI(Game.getGame(), new BufferedReader(new InputStreamReader(System.in)));
Thread t1 = new Thread(ui, "Chess");

Expand Down
28 changes: 19 additions & 9 deletions src/main/java/domain/UserInterface/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@
public class UI implements Runnable {

private Game game;
private static final String ENDCASE = "^(?i)(exit|stop)$"; //embedded flag ? specifies case insensitive

private static final String ENDCASE = "^(?i)(exit|stop)+$"; //embedded flag ? specifies case insensitive, + one or more times

private Reader read;

// private Visitor<> visitor;

public UI(Game g, Reader r) throws IOException {
this.game = g;
this.read = r;

showGreeting();
}

@Override
public void run() {
showGreeting();
if(this.read instanceof BufferedReader){ //testing if we are doing user input
BufferedReader br = (BufferedReader) read;
try {
Expand All @@ -40,7 +42,7 @@ public void run() {
}

public void showGreeting() {
System.out.println("Welcome to "+ Thread.currentThread().getName()+"! \nLowercase letters is black, and uppercase is white. \nLet's begin");
System.out.println("Welcome to CL Chess! \nLowercase letters is black, and uppercase is white. \nLet's begin");
game.printBoard();
}

Expand All @@ -49,20 +51,28 @@ private void gameIsOver() {
}

private boolean isInputValid(String input) {
if(input.matches(ENDCASE))
System.exit(0);

return !(input.isBlank() || (input.length() > 2));

}

public boolean userEndCase(String... inputs){
for (String str : inputs) {
if(str.matches(ENDCASE)) return true;
}
return false;
}

//Prompting for user input until game is over
public void getMoves(BufferedReader br) throws IOException {
while (!game.isCheckMate()) {
String input1 = "", input2 = "";

while (!game.isGameOver() && !userEndCase(input1, input2)) { //not working
try {
System.out.print(game.getTurn() + " to move, ");

System.out.println("enter the square you wish to move from.");
String input1 = br.readLine();
input1 = br.readLine();

if(!isInputValid(input1))
continue;
Expand All @@ -78,7 +88,7 @@ public void getMoves(BufferedReader br) throws IOException {
int startrank = startingPair.getY();

System.out.println("Enter the square you wish to move to.");
String input2 = br.readLine();
input2 = br.readLine();

if (!isInputValid(input2))
continue;
Expand Down
Binary file modified target/classes/domain/Logic/Board.class
Binary file not shown.
Binary file modified 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 modified target/classes/domain/Pieces/Rook.class
Binary file not shown.
Binary file modified target/classes/domain/UserInterface/App.class
Binary file not shown.
Binary file modified target/classes/domain/UserInterface/UI.class
Binary file not shown.

0 comments on commit 819d93b

Please sign in to comment.