Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Method to see if space puts a piece in jeopardy
- * Implementing this as an isolated method is more extensible
- * than implementing the same logic in whiteCheck or blackCheck
- * because we can use it to see if this is a valid move for the king
- * @input the space to check and the player who is moving
- * @return True if the space is safe, False if otherwise
- *
- * @note this method is super ineffecient. I should find a way to break it up!
- */
- @SuppressWarnings("Duplicates")
- public boolean spaceIsSafe(int x, int y, Player player){
- Board board = Board.getInstance();
- //check for rooks and queens
- //check up
- for(int i = (x - 1); i >= 0; i--){
- Piece piece = board.board[i][y];
- if(piece.getPlayer() != player){
- if(piece.getType() == Type.ROOK || piece.getType() == Type.QUEEN){
- return false;
- }
- }
- else{
- break;
- }
- }
- //check down
- for(int i = (x + 1); i <= 7; i++){
- Piece piece = board.board[i][y];
- if(piece.getPlayer() != player){
- if(piece.getType() == Type.ROOK || piece.getType() == Type.QUEEN){
- return false;
- }
- }
- else{
- break;
- }
- }
- //check right
- for(int i = (y + 1); i <= 7; i++){
- Piece piece = board.board[x][i];
- if(piece.getPlayer() != player){
- if(piece.getType() == Type.ROOK || piece.getType() == Type.QUEEN){
- return false;
- }
- }
- else{
- break;
- }
- }
- //check left
- for(int i = (y - 1); i >= 0; i++){
- Piece piece = board.board[x][i];
- if(piece.getPlayer() != player){
- if(piece.getType() == Type.ROOK || piece.getType() == Type.QUEEN){
- return false;
- }
- }
- else{
- break;
- }
- }
- //check for bishops and queens
- //check up-left
- for(int i = 1; x - i >= 0 && y - i >= 0; i ++){
- Piece piece = board.board[x-i][y-i];
- if(piece.getPlayer() != player){
- if(piece.getType() == Type.BISHOP || piece.getType() == Type.QUEEN){
- return false;
- }
- }
- else{
- break;
- }
- }
- //check up-right
- for(int i = 1; x - i >= 0 && y + i <= 7; i ++){
- Piece piece = board.board[x-i][y+i];
- if(piece.getPlayer() != player){
- if(piece.getType() == Type.BISHOP || piece.getType() == Type.QUEEN){
- return false;
- }
- }
- else{
- break;
- }
- }
- //check down-left
- for(int i = 1; x + i <= 7 && y - i >= 0; i ++){
- Piece piece = board.board[x+i][y-i];
- if(piece.getPlayer() != player){
- if(piece.getType() == Type.BISHOP || piece.getType() == Type.QUEEN){
- return false;
- }
- }
- else{
- break;
- }
- }
- //check down-right
- for(int i = 1; x + i <= 7 && y + i <= 7; i ++){
- Piece piece = board.board[x+i][y+i];
- if(piece.getPlayer() != player){
- if(piece.getType() == Type.BISHOP || piece.getType() == Type.QUEEN){
- return false;
- }
- }
- else{
- break;
- }
- }
- //check for pawns
- if (player.color == Color.WHITE){
- if (x > 0){
- if(y > 0) {
- if (board.board[x - 1][y-1].getPlayer() != player &&
- board.board[x - 1][y-1].getType() == Type.PAWN) {
- return false;
- }
- }
- if(y < 7){
- if (board.board[x - 1][y+1].getPlayer() != player &&
- board.board[x - 1][y+1].getType() == Type.PAWN) {
- return false;
- }
- }
- }
- }
- else{
- if (x < 7){
- if(y > 0) {
- if (board.board[x + 1][y-1].getPlayer() != player &&
- board.board[x + 1][y-1].getType() == Type.PAWN) {
- return false;
- }
- }
- if(y < 7){
- if (board.board[x + 1][y+1].getPlayer() != player &&
- board.board[x + 1][y+1].getType() == Type.PAWN) {
- return false;
- }
- }
- }
- }
- //check for kings
- int[][] directions = new int[][]{{-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0, -1}};
- for(int i = 0; i < directions.length; i++){
- int tempX = x + directions[i][0];
- int tempY = y + directions[i][1];
- if (tempX < 0 ||
- tempY < 0 ||
- tempX > 7 ||
- tempY > 7){
- continue;
- }
- else{
- if (board.board[tempX][tempY].getPlayer() != player &&
- board.board[tempX][tempY].getType() == Type.KING){
- return false;
- }
- }
- }
- //check for knights
- directions = new int[][]{{-2,-1}, {-2,1}, {2,-1}, {2,1}, {1,-2}, {1,2}, {-1,-2}, {-1, 2}};
- for(int i = 0; i < directions.length; i++){
- int tempX = x + directions[i][0];
- int tempY = y + directions[i][1];
- if (tempX < 0 ||
- tempY < 0 ||
- tempX > 7 ||
- tempY > 7){
- continue;
- }
- else{
- if (board.board[tempX][tempY].getPlayer() != player &&
- board.board[tempX][tempY].getType() == Type.KNIGHT){
- return false;
- }
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement