Write an abstract Java class ChessPiece that represents a chess piece, and creat
ID: 3585405 • Letter: W
Question
Write an abstract Java class ChessPiece that represents a chess piece, and create concrete (i.e., non-abstract) subclasses Rook, Bishop, and Knight. These classes should all have constructors that take two arguments, row and column, which should be integers between 1 and 8, and should all have no-argument accessor methods getRow and getColumn that return the row and column number, respectively.
All three concrete classes should also provide a method
that returns true if and only if it is valid to move that piece to the specified square. It is invalid to move a piece to the square it is already on, or one that is off the board. Do not consider any other pieces that may be on the chess board in deciding what squares a piece may move to.
The toString method for all three concrete classes should return a string "piece at (row,column)", where piece is either Rook, Bishop or Knight, and row and column comprise the location of the piece. For example, toString for a rook at row 3 column 4 would return “Rook at (3,4)”.
Recall that in chess, rooks move either vertically or horizontally as far as the edge of the board. Bishops move diagonally (either northeast, northwest, southeast, or southwest) as far as the edge of the board. Knights may move one square vertically and two squares horizontally, or one square horizontally and two squares vertically. The rook, bishop, and knight moves, respectively, may be visualised as:
A key requirement is that ChessPiece should be a Java type that supports the four methods above. For example, it must be possible to do this:
to print out “Knight at (6,2)”. Note that the class of the piece variable is ChessPiece. Declaring it to be of type Knight is not good enough.
654321 654321 654321Explanation / Answer
//Abstract class ChessPiece definition
abstract class ChessPiece
{
//To store current row
int currentRow;
//To store current column
int currentCol;
//For board
static String board[][] = new String[8][8];
//For class name
Object o;
//Constructor
ChessPiece(int r, int c)
{
//Stores the current class name
o = this;
//Loops for 8 time for row
for(int ro = 0; ro < 8; ro++)
{
//Loops for 8 time for column
for(int co = 0; co < 8; co++)
//Initializes board values to null
board[ro][co] = null;
}//End of for loop
//Checks the validity of the row and column
if(validPosition(r,c))
{
//If valid assign row and column
currentRow = r;
currentCol = c;
//Assign the class name to the board row and column position
board[r][c] = o.getClass().getName();
}//End of if
}//End of method
//Method to return if the row and column is valid
boolean validPosition(int r, int c)
{
//Check is the row and column is between 0 and 7 then return true
if(r >= 0 && r < 8 && c >= 0 && c < 8)
return true;
//Otherwise return false
else
return false;
}//End of method
//Method to return the current row number
int getRow()
{
return currentRow;
}//End of method
//Method to return the current column number
int getCol()
{
return currentCol;
}//End of method
//Abstract method to check valid move
abstract public boolean validMove(int toRow, int toColumn);
}//End of class
//Class Rook derived from ChessPiece
class Rook extends ChessPiece
{
//Parameterized constructor
Rook(int r, int c)
{
//Calls the base class constructor
super(r, c);
}//End of constructor
public String toString()
{
return "Rock at (" + getRow() + ", " + getCol() + ")";
}//End of method
//Method to return true if it is a valid move otherwise false
public boolean validMove(int toRow, int toColumn)
{
//Initializes flag to zero
int f = 0;
//Initializes boolean result to false
boolean res = false;
//Checks for valid row and column position
if(validPosition(toRow, toColumn))
{
//Loops 8 times for row
for(int r = 0; r < 8; r++)
{
//Loops 8 times for column
for(int c = 0; c < 8; c++)
{
//Checks if the board row and column position contains Rock then rock is on the board
//So set the flag to one
if(board[r][c] == "Rook")
{
//Set the flag to one
f = 1;
//Come out of the loop
break;
}//End of if
}//End of inner for loop
}//End of outer for loop
//Checks if the flag is one then Rook is on the board
if(f == 1)
{
//Checks if board row and column position is null i.e., no other piece is available
if(board[toRow][toColumn] == null)
{
//Checks horizontal and vertical move only
if((toRow > currentRow && toColumn == currentCol) || (toRow < currentRow && toColumn == currentCol) ||
(toColumn > currentCol && toRow == currentRow) || (toColumn < currentCol && toRow == currentRow))
res = true;
}//End of inner if
}//End of outer if
}//End of outer most if
//Otherwise
else
res = false;
//Returns the result
return res;
}//End of method
}//End of class
//Class Bishop derived from ChessPiece
class Bishop extends ChessPiece
{
//Parameterized constructor
Bishop(int r, int c)
{
//Calls the base class constructor
super(r, c);
}//End of constructor
//Overrides toString() method
public String toString()
{
return "Bishop at (" + getRow() + ", " + getCol() + ")";
}//End of method
//Method to return true if it is a valid move otherwise false
public boolean validMove(int toRow, int toColumn)
{
//Initializes flag to zero
int f = 0;
//Initializes boolean result to false
boolean res = false;
//Checks for valid row and column position
if(validPosition(toRow, toColumn))
{
//Loops 8 times for row
for(int r = 0; r < 8; r++)
{
//Loops 8 times for column
for(int c = 0; c < 8; c++)
{
//Checks if the board row and column position contains Rock then Bishop is on the board
//So set the flag to one
if(board[r][c] == "Bishop")
{
//Set the flag to one
f = 1;
//Come out of the loop
break;
}//End of if
}//End of inner for loop
}//End of outer for loop
//Checks if the flag is one then Rook is on the board
if(f == 1)
{
//Checks if board row and column position is null i.e., no other piece is available
if(board[toRow][toColumn] == null)
//Checks for diagonal position
if(toRow == toColumn)
res = true;
}//End of if
}//End of outer most
//Otherwise assign false to result
else
res = false;
//Returns result
return res;
}//End of method
}//End of class
//Class Knight derived from ChessPiece
class Knight extends ChessPiece
{
//Parameterized constructor
Knight(int r, int c)
{
//Calls the base class constructor
super(r, c);
}//End of constructor
//Overrides toString() method
public String toString()
{
return "Knight at (" + getRow() + ", " + getCol() + ")";
}//End of method
//Method to return true if it is a valid move otherwise false
public boolean validMove(int toRow, int toColumn)
{
//Initializes flag to zero
int f = 0;
//Initializes boolean result to false
boolean res = false;
//Checks for valid row and column position
if(validPosition(toRow, toColumn))
{
//Loops 8 times for row
for(int r = 0; r < 8; r++)
{
//Loops 8 times for column
for(int c = 0; c < 8; c++)
{
//Checks if the board row and column position contains Knight then Bishop is on the board
//So set the flag to one
if(board[r][c] == "Knight")
{
//Set the flag to one
f = 1;
//Come out of the loop
break;
}//End of if
}//End of inner for loop
}//End of outer for loop
//Checks if the flag is one then Knight is on the board
if(f == 1)
{
//Checks if board row and column position is null i.e., no other piece is available
if(board[toRow][toColumn] == null)
//Checks for diagonal position
if((currentRow+2 == toRow && currentCol+1 == toColumn)
|| (currentRow+2 == toRow && currentCol-1 == toColumn)
|| (currentRow+1 == toRow && currentCol+2 == toColumn)
|| (currentRow+1 == toRow && currentCol-2 == toColumn)
|| (currentRow-1 == toRow && currentCol+2 == toColumn)
|| (currentRow-1 == toRow && currentCol+2 == toColumn)
||(currentRow-2 == toRow && currentCol+1 == toColumn)
||(currentRow-2 == toRow && currentCol-1 == toColumn) )
res = true;
}//End of if
}//End of outer most
//Otherwise assign false to result
else
res = false;
//Returns result
return res;
}//End of method
}//End of class
//Driver class ChessPieceDemo definition
public class ChessPieceDemo
{
//Main method definition
public static void main(String[] args)
{
int moveR, moveC;
//For Bishop
moveR = moveC = 4;
ChessPiece piece = new Bishop(moveR,moveC);
moveR = moveC = 3;
if(piece.validMove(moveR, moveC))
System.out.println(" " + piece + " Valid Move for Bishop (" + moveR + ", " + moveC + ")");
else
System.out.println(" " + piece + " Invalid Move for Bishop (" + moveR + ", " + moveC + ")");
moveR = 2; moveC = 3;
if(piece.validMove(moveR, moveC))
System.out.println(" " + piece + " Valid Move for Bishop (" + moveR + ", " + moveC + ")");
else
System.out.println(" " + piece + " Invalid Move for Bishop (" + moveR + ", " + moveC + ")");
//For Rook
moveR = 6; moveC = 4;
ChessPiece piece1 = new Rook(moveR,moveC);
moveR = 6; moveC = 6;
if(piece1.validMove(moveR, moveC))
System.out.println(" " + piece1 + " Valid Move for Bishop (" + moveR + ", " + moveC + ")");
else
System.out.println(" " + piece1 + " Invalid Move for Bishop (" + moveR + ", " + moveC + ")");
moveR = 4; moveC = 5;
if(piece1.validMove(moveR, moveC))
System.out.println(" " + piece1 + " Valid Move for Bishop (" + moveR + ", " + moveC + ")");
else
System.out.println(" " + piece1 + " Invalid Move for Bishop (" + moveR + ", " + moveC + ")");
//For Knight
moveR = 2; moveC = 1;
ChessPiece piece2 = new Knight(moveR,moveC);
moveR = 4; moveC = 2;
if(piece2.validMove(moveR, moveC))
System.out.println(" " + piece2 + " Valid Move for Knight (" + moveR + ", " + moveC + ")");
else
System.out.println(" " + piece2 + " Invalid Move for Knight (" + moveR + ", " + moveC + ")");
moveR = 6; moveC = 2;
if(piece2.validMove(moveR, moveC))
System.out.println(" " + piece2 + " Valid Move for Knight (" + moveR + ", " + moveC + ")");
else
System.out.println(" " + piece2 + " Invalid Move for Knight (" + moveR + ", " + moveC + ")");
}//End of main method
}//End of class
Sample Run:
Bishop at (4, 4) Valid Move for Bishop (3, 3)
Bishop at (4, 4) Invalid Move for Bishop (2, 3)
Rock at (6, 4) Valid Move for Bishop (6, 6)
Rock at (6, 4) Invalid Move for Bishop (4, 5)
Knight at (2, 1) Valid Move for Knight (4, 2)
Knight at (2, 1) Invalid Move for Knight (6, 2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.