Question: How do I write the JUnit for the following: public abstract class AbstractChessPiece implements ChessPiece { private int row; private int column; private Color color;
How do I write the JUnit for the following:
public abstract class AbstractChessPiece implements ChessPiece {
private int row;
private int column;
private Color color;
protected AbstractChessPiece(int row, int column, Color color) {
verifyRowandColumn(row, column);
this.row = row;
this.column = column;
this.color = color;
}
public int getRow() {
return this.row;
}
public int getColumn() {
return this.column;
}
public Color getColor() {
return this.color;
}
protected boolean isValid (int n) {
return n >= 0 && n <= 7;
}
protected void verifyRowandColumn (int row, int column) {
if (!isValid(row) || !isValid(column)) {
throw new IllegalArgumentException ("Non-negative number only");
}
}
public boolean canMove(int row, int column) {
verifyRowandColumn(row, column);
return true;
}
public boolean canKill(ChessPiece piece) {
return piece.getColor() != this.getColor();
}
}
--------------------------------------------------------------------------------------------------------------------------
public class Bishop extends AbstractChessPiece {
public Bishop(int row, int column, Color color) {
super(row, column, color);
}
@Override
public boolean canMove(int row, int column) {
return super.canMove(row, column) && this.getRow() != row && this.getColumn() != column && Math.abs(row - this.getRow()) == Math.abs(column - this.getColumn());
}
@Override
public boolean canKill(ChessPiece piece) {
return super.canKill(piece) && canMove(piece.getRow(), piece.getColumn());
}
}
----------------------------------------------------------------
public interface ChessPiece {
int getRow();
int getColumn();
Color getColor();
boolean canMove(int row, int column);
boolean canKill(ChessPiece piece);
}
----------------------------------------------------------------
public enum Color {
BLACK, WHITE
}
---------------------------------------------------------------------------------------------------------------------------
public class Knight extends AbstractChessPiece {
public Knight (int row, int column, Color color) {
super(row, column, color);
}
@Override
public boolean canMove(int row, int column) {
return super.canMove(row, column) && Math.abs(this.getRow() - row) * Math.abs(this.getColumn() - column) == 2 ;
}
@Override
public boolean canKill(ChessPiece piece) {
return super.canKill(piece) && canMove(piece.getRow(), piece.getColumn());
}
}
---------------------------------------------------------------------------------------------------
public class Pawn extends AbstractChessPiece {
public Pawn(int row, int column, Color color) {
super(row, column, color);
}
@Override
public boolean canMove(int row, int column) {
return super.canMove(row, column) && this.getColumn() == column && row - this.getRow() == 1 ;
}
@Override
public boolean canKill(ChessPiece piece) {
return super.canKill(piece) && piece.getRow() - this.getRow() == 1 && Math.abs(this.getColumn() - piece.getColumn()) == 1;
}
}
------------------------------------------------------------------------------------
public class Queen extends AbstractChessPiece {
private Rook rook;
private Bishop bishop;
public Queen (int row, int column, Color color) {
super(row, column, color);
this.rook = new Rook(row, column, color);
this.bishop = new Bishop(row, column, color);
}
@Override
public boolean canMove(int row, int column) {
return super.canMove(row, column) && (rook.canMove(row, column) || bishop.canMove(row, column));
}
@Override
public boolean canKill(ChessPiece piece) {
return super.canKill(piece) && canMove(piece.getRow(), piece.getColumn());
}
}
---------------------------------------------------
public class Rook extends AbstractChessPiece {
public Rook (int row, int column, Color color) {
super(row, column, color);
}
@Override
public boolean canMove (int row, int column) {
return super.canMove(row, column) && (this.getRow() == row || this.getColumn() == column);
}
@Override
public boolean canKill(ChessPiece piece) {
return super.canKill(piece) && canMove(piece.getRow(), piece.getColumn());
}
}
all the classes needed are listed, please add to the code if needed and show!
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
