Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.)

image text in transcribed

----------------------------------------------------------------

GameBoard.java

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 "";

}

}

------------------------------------------------------------------------

Assignment.java if needed:

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();

}

}

ethod ublic GameBoard int rows, int cols) Instantiates the 2-dimensional array "board" to the size Description of the Method s x cols. Then instantiates each GamePiece in "board sing the default constructor ublic GamePiece getPiece(int row, int Returns a GamePiece at the indexes row and seat ol) specified by the parameters of this method) of the array board". ublic boolean isSpace Valid(int row, nt col) The method checks if the parameters row and col are alid. If at least one of the parameters "row" or "col" is ess than 0 or larger than the last index of the array (note hat the number of rows and columns can be different), hen it returns false. Otherwise it returns true. ublic boolean addPiece(int row, int ol, GamePiece piece) This method should validate that the space specified by and col is valid and that the space is not occupied by a iece. If the GamePiece at the space has the default label, he space should be considered not occupied. If the space s both valid and not already occupied, then the space ould replaced by the parameter "piece" and the method hould return true. Otherwise, return false ublic boolean movePiece(int srcRow, This method should validate the both the src and dest nt srcCol, int destRow, int destCol) spaces are valid and that the dest space is not occupied. If th conditions pass, then the piece located at (srcRow, Col) should be moved to (destRow, destCol). The spac t (srcRow, srcCol) should be replaced by the default amePiece. If this method moves the piece, return true, therwise return false eturns a String representation of the "board". It should ow the list of pieces placed on the board using the oString method of the class GamePiece (it shows the first characters of each piece). Use the following format ublic String toString he GameBoard -- Kin lease see the sample output listed below

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions