Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. JAVA How to implement canMove function in Chess game GUI classes:- pawn class , Bishop class , Knight class , Queen class , King

1. JAVA How to implement canMove function in Chess game GUI classes:- pawn class, Bishop class, Knight class, Queen class, King class AND Piece class.

- For reference canMove function is already given for Rook class.

pawn can move forward 1 or 2 on that pieces FIRST move, 1 forward afterwards.

Castle/Rook can move horizontal or vertical unlimited number of spaces as long as the way is clear of other pieces

Bishop can move on the diagonal unlimited number of spaces as long as the way is clear of other pieces

Knight moves in an L pattern of 2-1 or 1-2 in any direction, the path does not need to be clear

Queen can move in any direction unlimited number of spaces as long as the way is clear of other pieces

King can move in any direction 1 space

///////////////////////////Rook.java

public class Rook extends Piece {

public Rook(int x, int y, boolean is_white, String file_path, Board board) { super(x,y,is_white,file_path, board); } @Override public boolean canMove(int destination_x, int destination_y) { // Remember: A rook can move as many squares as he wants either forward, // backward, or sideways without jumping any pieces. He cannot attack // his own pieces. //if there is a piece at destination and it is your own, don't let us move there Piece possiblePiece = board.getPiece(destination_x, destination_y); if(possiblePiece !=null) { if(possiblePiece.isWhite() && this.isWhite()) { return false; } if(possiblePiece.isBlack() && this.isBlack()) { return false; } } //if rook try to move somewhere not in straight line, don't let it. if(this.getX() != destination_x && this.getY() != destination_y) { return false; } //First find out what direction we are trying to move String direction = ""; if(destination_y > this.getY()){ direction = "south"; } if(destination_y < this.getY()){ direction = "north"; } if(destination_x > this.getX()){ direction = "east"; } if(destination_x < this.getX()){ direction = "west"; } //whatever direction it is make sure there is nothing in the way. if(direction.equals("south")) { int spaces_to_move = Math.abs(destination_y - this.getY()); for(int i=1; i Piece p =board.getPiece(this.getX(), this.getY() + i); if(p!=null) { return false; } } } if(direction.equals("north")) { int spaces_to_move = Math.abs(destination_y - this.getY()); for(int i=1; i Piece p =board.getPiece(this.getX(), this.getY() - i); if(p!=null) { return false; } } } if(direction.equals("east")) { int spaces_to_move = Math.abs(destination_x - this.getX()); for(int i=1; i Piece p =board.getPiece(this.getX() + i, this.getY()); if(p!=null) { return false; } } } if(direction.equals("west")) { int spaces_to_move = Math.abs(destination_x - this.getX()); for(int i=1; i Piece p =board.getPiece(this.getX() - i, this.getY()); if(p!=null) { return false; } } } return true; } }

/////////////////Bishop.java

public class Bishop extends Piece {

public Bishop(int x, int y, boolean is_white, String file_path, Board board) { super(x,y,is_white,file_path, board); } @Override public boolean canMove(int destination_x, int destination_y) { // Remember: For attacking or just moving, a bishop is allowed to move // as many squares diagonally as it wants without jumping over another // piece. He cannot attack his own pieces. // WRITE CODE HERE

return true; } } ///////////////////////////////King.java

public class King extends Piece {

public King(int x, int y, boolean is_white, String file_path, Board board) { super(x,y,is_white,file_path, board); } @Override public boolean canMove(int destination_x, int destination_y) { // Remember: a king can move one square up, right, left, or down, or // diagonally, but he can never put himself in danger of an oposing // piece attacking him on the next turn. He cannot attack his own pieces. // WRITE CODE HERE

return true; } } ///////////////////////////////////////////Knight.java

public class Knight extends Piece {

public Knight(int x, int y, boolean is_white, String file_path, Board board) { super(x,y,is_white,file_path, board); } @Override public boolean canMove(int destination_x, int destination_y) { // Remember: a knight can move in any L shape and can jump over anyone // in order to do so. He cannot attack his own pieces. // By an L shape, I mean it can move to a square that is 2 squares away // horizontally and 1 square away vertically, or 1 square away horizontally // and 2 squares away vertically. // some examples: // // * * * * * * * // * * * * * * // * * * // WRITE CODE HERE return true; } }

////////////////////////////////Pawn.java

public class Pawn extends Piece {

private boolean has_moved; public Pawn(int x, int y, boolean is_white, String file_path, Board board) { super(x,y,is_white,file_path, board); has_moved = false; } public void setHasMoved(boolean has_moved) { this.has_moved = has_moved; } public boolean getHasMoved() { return has_moved; } @Override public boolean canMove(int destination_x, int destination_y) { // Remember: A pawn may only move towards the oponent's side of the board. // If the pawn has not moved yet in the game, for its first move it can // move two spaces forward. Otherwise, it may only move one space. // When not attacking it may only move straight ahead. // When attacking it may only move space diagonally forward

// WRITE CODE HERE return true; } }

///////////////////////////Queen.java

public class Queen extends Piece {

public Queen(int x, int y, boolean is_white, String file_path, Board board) { super(x,y,is_white,file_path, board); } @Override public boolean canMove(int destination_x, int destination_y) { // Remember: A Queen can move as many squares as she wants forward, // backward, sideways, or diagonally, without jumping over any pieces. // She cannot attack her own pieces. // WRITE CODE HERE

return true; } }

/////////////////////////Piece.java

public class Piece { private int x; private int y; final private boolean is_white; private String file_path; public Board board; public Piece(int x, int y, boolean is_white, String file_path, Board board) { this.is_white = is_white; this.x = x; this.y = y; this.file_path = file_path; this.board = board; } public String getFilePath() { return file_path; } public void setFilePath(String path) { this.file_path = path; } public boolean isWhite() { return is_white; } public boolean isBlack() { return !is_white; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public int getX() { return x; } public int getY() { return y; } public boolean canMove(int destination_x, int destination_y) { return false; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Managing Your Information How To Design And Create A Textual Database On Your Microcomputer

Authors: Tenopir, Carol, Lundeen, Gerald

1st Edition

1555700233, 9781555700232

More Books

Students also viewed these Databases questions

Question

Learn how to diagram and label factorial experiments

Answered: 1 week ago

Question

1. Identify three communication approaches to identity.

Answered: 1 week ago

Question

d. Who are important leaders and heroes of the group?

Answered: 1 week ago

Question

3. Describe phases of minority identity development.

Answered: 1 week ago