Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A flexible piece is one that can move right or left as well as up or downthis leads to slow flexible and fast flexible pieces.

A flexible piece is one that can move right or left as well as up or downthis leads to slow flexible and fast flexible pieces. Extend the class hierarchy developed so far in Problems P1.5 through P1.7 to include these two new kind of pieces. First, define an interface called Flexible that prescribes up and down directions as constants. Then define class SlowFlexible that implements Flexible and extends SlowPiece, and class FastFlexible that implements Flexible and extends FastPiece. Define methods in these classes as needed.

Problem P1.5

Most board games have pieces that move across the board with certain set rules about how far and in which direction they may move. In the following set of problems, you will set up the infrastructure for a board game. Start by defining an abstract class called Piece with the following fields that describe the piece:

Its name.

Its color. A piece may be black, white, red, blue, or green.

Its position. Assume that the games for which a piece may be used are played on 8 * 8 grid boardsthe position is a coordinate (i,j) on such a board, with each coordinate taking on a value between 0 and 7. (The top-left corner of the board is taken to be the origin, (0,0).)

All pieces can make a move to the left or a move to the right. However, how far they go either left or right in one move is not known. Implement:

A public method in Piece to move the piece left or right by one space. This method should accept a direction parameter. If a move is requested that would take the piece off the edge of the board, simply return without doing anything.

Protected methods in Piece to move the piece left or right by as many spaces as needed. Both of these methods will accept number of spaces as parameter. As before, if a move is requested that would take the piece off the edge of the board, simply return without doing anything.

Also define operations to retrieve values of the various attributes, as well as the toString method to print a string representation. Define a constructor that accepts name, color, and location of the piece. Finally, ensure that all parameters to methods are legal values. (Would you find it useful to define constants for left and right directions, as well as for all valid colors?)

Problem P1.6

A slow piece isapiece that moves left or right exactly one step in every move. Define a class called SlowPiece that extends Piece, implementing a constructor and redefining the inherited toString method.

Problem P1.7

A fast piece isapiece that can move left or right as many steps as specified. Define a class called FastPiece that extends Piece, implementing a constructor and redefining the inherited toString method. In addition, define a public move method that accepts a direction and a number of spaces to move. (Why did we not have to define a move method in SlowPiece?)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Combinatorial Testing In Cloud Computing

Authors: Wei-Tek Tsai ,Guanqiu Qi

1st Edition

9811044805, 978-9811044809

More Books

Students also viewed these Programming questions