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.

Javascript Inheritance comes up naturally in many games. If you were writing

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 7

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!