Question
Javascript Inheritance comes up naturally in many games. If you were writing a chess program, for example, you could represent the pieces by defining an
Javascript
Inheritance comes up naturally in many games. If you were writing a chess program, for example, you could represent the pieces by defining an abstract ChessPiece class along with the subclasses King, Queen, Rook, Bishop, Knight, and Pawn for the color and location of the piece. The individual subclasses extend this common framework by implementing the moves for that particular piece.
Write the code necessary to define the classes shown in the UML diagram attached. The factor methods for the concrete classes each take an argument bw, which is either "B" or "W" and designation for the location of the piece composed of the letter indicating the column and a number indicating the row.
Figure 11-10 UML diagram for the chessPiece hierarchy ChessPiece set Color (w) getColor() set Location(s) get Location() get Moves() King Queen 1 Rook Bishop Knight Pawn King (hw, y) get Moves() Queen (bw, Ay) get Moves () Rook (bw, sq) getMoves () Bishop (bw, 59) get Moves() Knight (bw, 24) getMoves() Pawn (w, 19) Iget Moves () For example, calling Queen ("W", "d1") would create a white queen on d1, which is its initial square. The interesting challenge in this exercise is to implement the getMoves method for each of the concrete subclasses. This method should return an array of all the two character locations to which that piece could move from its current square, assuming that the rest of the board were empty. Figure 11-11 shows how the different pieces move, in case you are unfamiliar with the rules of chess. The white pieces can move to any of the squares marked with an x, and the black pieces can move to any square marked with an o. The white pawn in the last diagram can move either one or two squares because it is in its initial position on row 2, but the black pawn can only move one square because it has already moved from its initial position on row 7. Figure 11-10 UML diagram for the chessPiece hierarchy ChessPiece set Color (w) getColor() set Location(s) get Location() get Moves() King Queen 1 Rook Bishop Knight Pawn King (hw, y) get Moves() Queen (bw, Ay) get Moves () Rook (bw, sq) getMoves () Bishop (bw, 59) get Moves() Knight (bw, 24) getMoves() Pawn (w, 19) Iget Moves () For example, calling Queen ("W", "d1") would create a white queen on d1, which is its initial square. The interesting challenge in this exercise is to implement the getMoves method for each of the concrete subclasses. This method should return an array of all the two character locations to which that piece could move from its current square, assuming that the rest of the board were empty. Figure 11-11 shows how the different pieces move, in case you are unfamiliar with the rules of chess. The white pieces can move to any of the squares marked with an x, and the black pieces can move to any square marked with an o. The white pawn in the last diagram can move either one or two squares because it is in its initial position on row 2, but the black pawn can only move one square because it has already moved from its initial position on row 7Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started