All the classes for this assignment are provided below with the questions // Che
ID: 3880624 • Letter: A
Question
All the classes for this assignment are provided below with the questions
// ChessColour enum class
public enum ChessColour {
WHITE,BLACK
}
// ChessPieces enum class
public enum ChessPieces {
PAWN ('P'),
KNIGHT ('N'),
BISHOP ('B'),
ROOK ('R'),
QUEEN ('Q'),
KING ('K');
private final char shortName;
ChessPieces (char shortName) {
this.shortName = shortName;
}
public char getShortName() {
return this.shortName;
}
}
// Coordinate class
public class Coordinate {
private int column;
private int row;
public Coordinate(int column, int row) throws IndexOutOfBoundsException {
if ((column < 0) || (column > 7))
throw new IndexOutOfBoundsException("column must be between 0 and 7,inclusive");
if ((row < 0) || (row > 7))
throw new IndexOutOfBoundsException("row must be between 0 and 7,inclusive");
this.column = column;
this.row = row;
}
public Coordinate(char column, char row) throws IndexOutOfBoundsException {
if ((column < 'a') || (column > 'h'))
throw new IndexOutOfBoundsException("column must be between a and h,inclusive");
if ((row < '1') || (row > '8'))
throw new IndexOutOfBoundsException("row must be between 1 and 8,inclusive");
this.column = column - 'a';
this.row = row - '1';
}
public Coordinate(String coordinate) throws IndexOutOfBoundsException {
if (coordinate.length() != 2)
throw new IllegalArgumentException("Coordinate is a 2-character string");
char column = coordinate.charAt(0);
char row = coordinate.charAt(1);
// this(x,y) except it must be the first statement!par
if ((column < 'a') || (column > 'h'))
throw new IndexOutOfBoundsException("x must be between a and h, inclusive");
if ((row < '1') || (row > '8'))
throw new IndexOutOfBoundsException("y must be between 1 and 8, inclusive");
this.column = column - 'a';
this.row = row - '1';
}
public int getColumnNumber() {
return column;
}
public int getRowNumber() {
return row;
}
public char getColumn() {
return (char) ('a'+column);
}
public char getRow() {
return (char) ('1'+row);
}
@Override
public String toString() {
return "(" + column + "," + row + ")";
}
public String name() {
return ""+getColumn()+getRow();
}
}
// Piece class
public class Piece {
private ChessColour colour;
private ChessPieces name;
private char shortName;
public Piece(ChessColour colour, ChessPieces name) {
this.colour = colour;
this.name = name;
if (ChessColour.BLACK.equals(colour)) {
this.shortName = Character.toLowerCase(name.getShortName());
} else {
this.shortName = name.getShortName();
}
}
public Piece(char shortName) {
this.shortName = shortName;
if(Character.isLowerCase(shortName)) {
this.colour = ChessColour.BLACK;
} else {
this.colour = ChessColour.WHITE;
}
shortName = Character.toUpperCase(shortName);
for(ChessPieces name : ChessPieces.values()) {
if(name.getShortName() == shortName) {
this.name = name;
}
}
if (name == null) {
throw new IllegalArgumentException();
}
}
public ChessColour getColour() {
return colour;
}
public ChessPieces getName() {
return name;
}
public char getShortName() {
return shortName;
}
@Override
public String toString() {
return colour + " " + name;
}
}
// Square class
public class Square {
private Coordinate coordinate;
private Piece piece;
public Square(Coordinate coordinate) {
this.coordinate = coordinate;
}
public Square(Coordinate coordinate, Piece piece) {
this.coordinate = coordinate;
this.piece = piece;
}
public char getColumn() {
return coordinate.getColumn();
}
public char getRow() {
return coordinate.getRow();
}
public int getColumnNumber() {
return coordinate.getColumnNumber();
}
public int getRowNumber() {
return coordinate.getRowNumber();
}
public Coordinate getCoordinate() {
return coordinate;
}
public Piece getPiece() {
return piece;
}
public Piece addPiece(Piece newPiece) {
Piece prev = this.piece;
this.piece = newPiece;
return prev;
}
public Piece deletePiece() {
Piece prev = this.piece;
this.piece = null;
return prev == null ? null : prev;
}
public boolean isOccupied() {
return this.piece != null;
}
@Override
public String toString() {
if (piece == null) {
return "Square"+coordinate.toString()+": ";
} else {
return "Square"+coordinate.toString()+":"+piece.toString();
}
}
Part 4: Build the Game board At this point, we are ready to create the ChessBoard itself. This time, I will not provide you the test code. You must write both the ChessBoard class and the (client) test application. Note: The most difficult part ahead (other than objects) is the use of 2D arrays. ChessBoard Now we have 6 classes, and look at the relationships between classes! -activeColour:ChessColour -fullMove:int -board:Squarelll Square +ChessBoard() +ChessBoard(positions:Coordinatell, pieces:Pieces[) Coordinate +getSquare(c:Coordinate): Square Piece +reset() +move(src:Coordinate, dest:Coordinate):boolean ChessPieces ChessColour +toString():String +toFEN():String . The default constructor should initialize the chessboard to the initial position (as given in the image on the first page) The two-argument constructor should initialize the chessboard to an intermediate state, given by the list of (positions,pieces) provided in the two arguments. . o The two arrays are meant to be synchronized. They should have the same length, and piece[0] should be put on the square at position[0], piece[1] should be put on the square at position[1] and so on. Below you are provided with the starting code for this method that checks that the lengths of both arrays are identical. You must complete the code from there. It is meant oExplanation / Answer
// ChessColour enum class
public enum ChessColour {
WHITE,BLACK
}
// ChessPieces enum class
public enum ChessPieces {
PAWN ('P'),
KNIGHT ('N'),
BISHOP ('B'),
ROOK ('R'),
QUEEN ('Q'),
KING ('K');
private final char shortName;
ChessPieces (char shortName) {
this.shortName = shortName;
}
public char getShortName() {
return this.shortName;
}
}
// Coordinate class
public class Coordinate {
private int column;
private int row;
public Coordinate(int column, int row) throws IndexOutOfBoundsException {
if ((column < 0) || (column > 7))
throw new IndexOutOfBoundsException("column must be between 0 and 7,inclusive");
if ((row < 0) || (row > 7))
throw new IndexOutOfBoundsException("row must be between 0 and 7,inclusive");
this.column = column;
this.row = row;
}
public Coordinate(char column, char row) throws IndexOutOfBoundsException {
if ((column < 'a') || (column > 'h'))
throw new IndexOutOfBoundsException("column must be between a and h,inclusive");
if ((row < '1') || (row > '8'))
throw new IndexOutOfBoundsException("row must be between 1 and 8,inclusive");
this.column = column - 'a';
this.row = row - '1';
}
public Coordinate(String coordinate) throws IndexOutOfBoundsException {
if (coordinate.length() != 2)
throw new IllegalArgumentException("Coordinate is a 2-character string");
char column = coordinate.charAt(0);
char row = coordinate.charAt(1);
// this(x,y) except it must be the first statement!par
if ((column < 'a') || (column > 'h'))
throw new IndexOutOfBoundsException("x must be between a and h, inclusive");
if ((row < '1') || (row > '8'))
throw new IndexOutOfBoundsException("y must be between 1 and 8, inclusive");
this.column = column - 'a';
this.row = row - '1';
}
public int getColumnNumber() {
return column;
}
public int getRowNumber() {
return row;
}
public char getColumn() {
return (char) ('a'+column);
}
public char getRow() {
return (char) ('1'+row);
}
@Override
public String toString() {
return "(" + column + "," + row + ")";
}
public String name() {
return ""+getColumn()+getRow();
}
}
// Piece class
public class Piece {
private ChessColour colour;
private ChessPieces name;
private char shortName;
public Piece(ChessColour colour, ChessPieces name) {
this.colour = colour;
this.name = name;
if (ChessColour.BLACK.equals(colour)) {
this.shortName = Character.toLowerCase(name.getShortName());
} else {
this.shortName = name.getShortName();
}
}
public Piece(char shortName) {
this.shortName = shortName;
if(Character.isLowerCase(shortName)) {
this.colour = ChessColour.BLACK;
} else {
this.colour = ChessColour.WHITE;
}
shortName = Character.toUpperCase(shortName);
for(ChessPieces name : ChessPieces.values()) {
if(name.getShortName() == shortName) {
this.name = name;
}
}
if (name == null) {
throw new IllegalArgumentException();
}
}
public ChessColour getColour() {
return colour;
}
public ChessPieces getName() {
return name;
}
public char getShortName() {
return shortName;
}
@Override
public String toString() {
return colour + " " + name;
}
}
// Square class
public class Square {
private Coordinate coordinate;
private Piece piece;
public Square(Coordinate coordinate) {
this.coordinate = coordinate;
}
public Square(Coordinate coordinate, Piece piece) {
this.coordinate = coordinate;
this.piece = piece;
}
public char getColumn() {
return coordinate.getColumn();
}
public char getRow() {
return coordinate.getRow();
}
public int getColumnNumber() {
return coordinate.getColumnNumber();
}
public int getRowNumber() {
return coordinate.getRowNumber();
}
public Coordinate getCoordinate() {
return coordinate;
}
public Piece getPiece() {
return piece;
}
public Piece addPiece(Piece newPiece) {
Piece prev = this.piece;
this.piece = newPiece;
return prev;
}
public Piece deletePiece() {
Piece prev = this.piece;
this.piece = null;
return prev == null ? null : prev;
}
public boolean isOccupied() {
return this.piece != null;
}
@Override
public String toString() {
if (piece == null) {
return "Square"+coordinate.toString()+": ";
} else {
return "Square"+coordinate.toString()+":"+piece.toString();
}
}
}