Question
JAVA: In this assignment, we will be making a program to populate a game board with pieces and to move those pieces around on the
JAVA:
In this assignment, we will be making a program to populate a game board with pieces and to
move those pieces around on the board.
Use the files Assignment.java, GameBoard.java, and GamePiece.java. All files are available on
Canvas. You will need to add header comments to each file along with comments for all the
methods you will be implementing. Do not make any other changes to the files beyond adding
comments and updating the methods described in the following instructions.
Any changes made to the main method of the file will be penalized unless otherwise
instructed
Save all files in the same folder.
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
--- Kin ---
Paw --- ---
--- --- Paw
Would you like to move a piece? Enter "Y" to move a piece
Y
Please enter the piece's row.
0
Please enter the piece's column.
1
Please enter the piece's new row.
0
Please enter the piece's new column.
2
Piece moved to new space.
--- --- Kin
Paw --- ---
--- --- Paw
Would you like to move a piece? Enter "Y" to move a piece
import java.util.Scanner;
public class Assignment {
/**
* main method for the assignment.
*
* This method takes input from the user to allow testing various scenarios
* for the GameBoard and GamePiece classes.
*/
public static void main(String[] args) {
// declare local variables
int row;
int col;
int destRow;
int destCol;
int rowNum;
int colNum;
GameBoard board;
GamePiece piece;
Scanner input = new Scanner(System.in);
String inputString;
// obtain board size from the user
System.out.println("Please enter the number of rows.");
rowNum = input.nextInt();
System.out.println("Please enter the number of columns.");
colNum = input.nextInt();
board = new GameBoard(rowNum, colNum);
// obtain a label for the next piece from the user
System.out.println("Please enter a label for a new piece. Enter \"Q\" when done.");
inputString = input.next();
// continue until the user indicates they are done
while (!inputString.equalsIgnoreCase("Q")) {
// create a new GamePiece from the user input
piece = new GamePiece(inputString);
// obtain a row and column to place the new piece
System.out.println("Please enter a row for the piece.");
row = input.nextInt();
System.out.println("Please enter a column for the piece.");
col = input.nextInt();
// validate the space is valid
if (board.isSpaceValid(row, col)) {
// add the piece
if (board.addPiece(row, col, piece)) {
System.out.println("New piece \"" + piece.getLabel() + "\" added.");
}
else {
System.out.println("A piece is already at that space.");
System.out.println("New piece \"" + piece.getLabel() + "\" not added.");
}
}
else {
System.out.println("Invalid row or column.");
System.out.println("New piece \"" + piece.getLabel() + "\" not added.");
}
// obtain a label for the next piece from the user
System.out.println("Please enter a label for a new piece. Enter \"Q\" when done.");
inputString = input.next();
}
// check if the user wants to move a piece
System.out.println(board.toString());
System.out.println("Would you like to move a piece? Enter \"Y\" to move a piece");
inputString = input.next();
while (inputString.equalsIgnoreCase("Y")) {
// obtain the piece's location from the user
System.out.println("Please enter the piece's row.");
row = input.nextInt();
System.out.println("Please enter the piece's column.");
col = input.nextInt();
// obtain the piece's destination from the user
System.out.println("Please enter the piece's new row.");
destRow = input.nextInt();
System.out.println("Please enter the piece's new column.");
destCol = input.nextInt();
// validate that both spaces are valid
if (board.isSpaceValid(row, col) && board.isSpaceValid(destRow, destCol)) {
// move the piece
if (board.movePiece(row, col, destRow, destCol)) {
System.out.println("Piece moved to new space.");
}
else {
System.out.println("A piece is already in that space.");
}
}
else {
System.out.println("A row or column is invalid. No piece moved.");
}
// print the current state of the board and prompt the user to continue
System.out.println(board.toString());
System.out.println("Would you like to move a piece? Enter \"Y\" to move a piece");
inputString = input.next();
}
input.close();
}
}
--------------------------------------
public class GamePiece {
// TODO add instance variable
public GamePiece() {
// TODO implement method
}
public GamePiece(String newLabel) {
// TODO implement method
}
public String getLabel() {
// TODO implement method
return "";
}
public String toString() {
// TODO implement method
return "";
}
}
-------------------------------------------------------------
public class GameBoard {
// TODO create instance variables
public GameBoard(int rows, int cols) {
// TODO implement method
}
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 "";
}
}
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