Question
Having problems implementing this class Step 1. First, you need to implement the GamePiece class in the GamePiece.java file. It should have one instance for
Having problems implementing this class
Step 1. First, you need to implement the GamePiece class in the GamePiece.java file. It should have one instance for label (String). In addition, the following methods need to be implemented as follows. Method Description of the Method public GamePiece( ) Constructs a GamePiece object by assigning the label with the default value ---. public GamePiece(String newLabel) Constructs a GamePiece object by assigning the label with the newLabel provided. public String getLabel( ) Returns the instance variable label. public String toString ( ) Constructs a String of length 3 from the label. If the label is shorter than length 3, then the new String should be the label with spaces appended to make it the correct length. If the label is longer than 3, then use the first 3 characters of the label. Step 2. You will be creating a class called GameBoard. This class should be defined in a file named " GameBoard.java". The class GameBoard will contain a 2-dimensional array called "board" of GamePiece objects as its instance variable. The class GameBoard must include the following constructor and methods. (If your class does not contain any of the following methods, points will be deducted.) Method Description of the Method public GameBoard(int rows, int cols) Instantiates the 2-dimensional array board to the size rows x cols. Then instantiates each GamePiece in board using the default constructor. public GamePiece getPiece(int row, int col) Returns a GamePiece at the indexes row and seat (specified by the parameters of this method) of the array "board". public boolean isSpaceValid(int row, int col) The method checks if the parameters row and col are valid. If at least one of the parameters "row" or "col" is less than 0 or larger than the last index of the array (note that the number of rows and columns can be different), then it returns false. Otherwise it returns true. public boolean addPiece(int row, int col, GamePiece piece) This method should validate that the space specified by row and col is valid and that the space is not occupied by a piece. If the GamePiece at the space has the default label, the space should be considered not occupied. If the space is both valid and not already occupied, then the space should replaced by the parameter piece and the method should return true. Otherwise, return false. public boolean movePiece(int srcRow, int srcCol, int destRow, int destCol) This method should validate the both the src and dest spaces are valid and that the dest space is not occupied. If both conditions pass, then the piece located at (srcRow, srcCol) should be moved to (destRow, destCol). The space at (srcRow, srcCol) should be replaced by the default GamePiece. If this method moves the piece, return true, otherwise return false. public String toString( ) Returns a String representation of the board. It should show the list of pieces placed on the board using the toString method of the class GamePiece (it shows the first 3 characters of each piece). Use the following format: The GameBoard -------------------- --- Kin --- Paw --- --- --- --- Paw Please see the sample output listed below. After compiling GamePiece.java, GameBoard.java, and Assignment.java files, you need to execute Assignment.class.
Sample Output: (the inputs entered by a user are shown in green) Make sure that your program works at least with this scenario. You should test your code with other scenarios as well. C:\MyJava\applications>java Assignment Please enter the number of rows for the GameBoard. 3 Please enter the number of columns for the GameBoard. 3 Please enter a label for a new piece. Enter "Q" when done. Kin Please enter a row for the piece. 0 Please enter a column for the piece. 1 New piece "Kin" added. Please enter a label for a new piece. Enter "Q" when done. Paw Please enter a row for the piece. 1 Please enter a column for the piece. 0 New piece "Paw" added. Please enter a label for a new piece. Enter "Q" when done. Pawn Please enter a row for the piece. 2 Please enter a column for the piece. 2 New piece "Pawn" added. Please enter a label for a new piece. Enter "Q" when done. Queen Please enter a row for the piece. 4 Please enter a column for the piece. 5 Invalid row or column. New piece "Queen" not added. Please enter a label for a new piece. Enter "Q" when done. King Please enter a row for the piece. -1 Please enter a column for the piece. 2 Invalid row or column. New piece "King" not added. Please enter a label for a new piece. Enter "Q" when done. Q
=======================================
public class GameBoard { // TODO create instance variables private int xrow; private int xcols; private String xPiece; public GameBoard(int rows, int cols) { // TODO implement method this.xrow = rows; this.xcols = cols; } public GamePiece getPiece(int row, int col) { // TODO implement method return null; } public boolean isSpaceValid(int row, int col) { // TODO implement method return false; } public boolean addPiece(int row, int col, GamePiece piece) { // TODO implement method return false; } public boolean movePiece(int srcRow, int srcCol, int destRow, int destCol) { // TODO implement method return false; } public String toString() { // TODO implement method return ""; } }
=========================
public class GamePiece { // TODO add instance variable private String xpiece; public GamePiece() { // TODO implement method this.xpiece = "---"; } public GamePiece(String newLabel) { // TODO implement method this.xpiece=newLabel; } public String getLabel() { // TODO implement method return this.xpiece; } public String toString() { // TODO implement method return System.out.print(this.xpiece); } }
Step 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