Question: Write the following classes and enums: Color an enum with two values, WHITE and BLACK which represent the colors of the chess pieces. Square a
Write the following classes and enums:
Color an enum with two values, WHITE and BLACK which represent the colors of the chess pieces.
Square a class to represent squares on a chess board. Square should be instantiable and have the following constructors and methods: a public constructor Square(char file, char rank) which uses a file name such as 'a' and rank name such as '1' to initialize instance variables that store the file and rank (as chars), and optionally the String name of the square that would be returned by toString() (see below). Ideally, this constructor should delegate to the other constructor, described below. a public constructor Square(String name) which uses a square name such as "a1" to initialize the instance variables described in the other constructor above. a public instance method toString() which returns a String representation of the square name, e.g., "a1". a properly written equals method that overrides the equals method from java.lang.Object and returns true for Square objects that have the same file and rank values, false otherwise. Piece a class to represent chess pieces (Some people distinguish between pawns and pieces, well call pawns pieces as well.) Piece should be abstract and have the following constructors and methods: a public constructor that takes a Color parameter and stores its value in an instance variable a public getColor() instance method that returns the Color of the piece a public abstract instance method algebraicName() which returns a String containing the algebraic name of the piece, e.g., "" for pawns, or one of "K", "Q", "B", "N", "R". a public abstract instance method fenName() which returns a String containing the FEN name for the piece. a public abstract instance method movesFrom(Square square) which returns a Square[] containg all the squares the piece could move to from square on a chess board containing only the piece. A subclass of Piece named King which overrides Pieces abstract methods appropriately A subclass of Piece named Queen which overrides Pieces abstract methods appropriately A subclass of Piece named Bishop which overrides Pieces abstract methods appropriately A subclass of Piece named Knight which overrides Pieces abstract methods appropriately A subclass of Piece named Rook which overrides Pieces abstract methods appropriately A subclass of Piece named Pawn which overrides Pieces abstract methods appropriately For each class include Javadoc comments as described in the CS 1331 style guide. We will test your classes by simply using them, for example: Piece knight = new Knight(Color.BLACK); assert knight.algebraicName().equals("N"); assert knight.fenName().equals("n"); Square[] attackedSquares = knight.movesFrom(new Square("f6")); // test that attackedSquares contains e8, g8, etc. Square a1 = new Square("a1"); Square otherA1 = new Square('a', '1'); Square h8 = new Square("h8"); assert a1.equals(otherA1); assert !a1.equals(h8);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
