diff --git a/src/org/chess/ChessBoard.java b/src/org/chess/ChessBoard.java index 75b1261..915cd28 100644 --- a/src/org/chess/ChessBoard.java +++ b/src/org/chess/ChessBoard.java @@ -1,6 +1,9 @@ package org.chess; +import java.awt.Point; import java.io.IOException; +import java.util.LinkedList; +import java.util.List; /** * This is the file you can edit. You will need to populate isPlayerInCheck @@ -8,7 +11,7 @@ * You are also allowed to add any other new methods you need. */ public class ChessBoard extends AbstractChessBoard { - + List attackingPieces = new LinkedList(); public ChessBoard(String fileName) throws IOException { super(fileName); @@ -27,9 +30,81 @@ public ChessBoard(AbstractChessBoard other) { */ @Override public boolean isPlayerInCheck(char c) { + Point kingPos = playerKingPos(c); + if (kingPos != null) + { + Point chessPiecePos = new Point(); + ChessPiece piece = null; + for (int x = 0; x<= 7; x++) + { + for (int y = 0; y <= 7; y++) + { + piece = get(x,y); + chessPiecePos.x = x; + chessPiecePos.y = y; + if (piece == null) + { + continue; + } + if (piece.getColor() == c) + { + continue; + } + if (canMove(piece, kingPos.x, kingPos.y) == null) + { + attackingPieces.add(piece); + return true; + } + } + } + } return false; } + public Point playerKingPos(char c) { + Point chessPieceXY = new Point(); + ChessPiece piece = null; + for (int x = 0; x <= 7; x++) + { + for (int y = 0; y <= 7; y++) + { + piece = get(x,y); + if (piece == null) { + continue; + } + if (piece.getColor() != c) { + continue; + } + if (piece.isEssential()){ + chessPieceXY.x = x; + chessPieceXY.y = y; + return chessPieceXY; + } + } + } + return chessPieceXY; + } + + public ChessPiece playerKingPiece(char c) { + ChessPiece piece = null; + for (int x = 0; x <= 7; x++) + { + for (int y = 0; y <= 7; y++) + { + piece = get(x,y); + if (piece == null) { + continue; + } + if (piece.getColor() == c && piece.isEssential()) + { + return piece; + } + + } + } + return piece; + } + /** * For a player 'W' or 'B', return whether that player has an essential piece * that's in checkmate. @@ -39,6 +114,131 @@ public boolean isPlayerInCheck(char c) { */ @Override public boolean isPlayerInCheckmate(char c) { - return false; + if (!isPlayerInCheck(c)) + { + return false; + } + ChessPiece kingPiece = playerKingPiece(c); + List alliedPieces = getAlliedPieces(c); + List kingMovableSpaces = getKingMovableSpaces(c); + List attackingSpaces = getAttackingSpaces(c); + + if (kingPiece != null && !attackingPieces.isEmpty()) + { + for (ChessPiece alliedPiece : alliedPieces) + { + for(Point attackingSpace : attackingSpaces) + { + if (canMove(alliedPiece, attackingSpace.x, attackingSpace.y) == null) + { + return false; + } + + for (ChessPiece attackingPiece : attackingPieces) + { + if(canMove(alliedPiece, attackingPiece.x, attackingPiece.y) == null) + { + return false; + } + if (canMove(kingPiece, attackingPiece.x, attackingPiece.y) == null) + { + return false; + } + } + } + } + + if (!kingMovableSpaces.isEmpty()) + { + return false; + } + } + + return true; + } + + + public List getAlliedPieces(char c) + { + List alliedPieces = new LinkedList(); + ChessPiece kingPiece = playerKingPiece(c); + for (int x = 0; x<= 7; x++) + { + for (int y = 0; y <= 7; y++) + { + ChessPiece piece = get(x,y); + if (piece == null) + { + continue; + } + if (piece.getColor() == c && piece != kingPiece) + { + alliedPieces.add(piece); + } + } + } + return alliedPieces; } + + public List getKingMovableSpaces(char c) + { + Point kingPos = playerKingPos(c); + ChessPiece piece = playerKingPiece(c); + List movableSpaces = new LinkedList(); + if (kingPos != null && piece != null) + { + Point boardSpace = new Point(); + for (int x = 0; x<= 7; x++) + { + for (int y = 0; y <= 7; y++) + { + boardSpace.x = x; + boardSpace.y = y; + if (canMove(piece, x, y) == null) + { + movableSpaces.add(boardSpace); + } + } + } + } + return movableSpaces; + } + + public List getAttackingSpaces(char c) + { + List attackingSpaces = new LinkedList(); + for (ChessPiece attackingPiece : attackingPieces) + { + Point kingPos = playerKingPos(c); + int dx, dy, p, x, y; + dx = attackingPiece.x - kingPos.x; + dy = attackingPiece.y - kingPos.y; + x = kingPos.x; + y = kingPos.y; + p = 2 * dy - dx; + while(x < attackingPiece.x) + { + Point space = new Point(); + if(p >= 0) + { + space.x = x; + space.y = y; + attackingSpaces.add(space); + y = y + 1; + p = p + 2 * dy - 2 * dx; + } + else + { + + space.x = x; + space.y = y; + attackingSpaces.add(space); + p = p + 2 * dy;} + x = x + 1; + } + } + + return attackingSpaces; + } + }