Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//--------- PlayGame.java --------- // To test the game. public class PlayGame { public static void main(String[] args) { Queen q1 = new Queen(1,2); Queen q2

image text in transcribed

//--------- PlayGame.java ---------

// To test the game.

public class PlayGame {

public static void main(String[] args) {

Queen q1 = new Queen(1,2);

Queen q2 = new Queen(3,4);

// q3 to test same location placement.

Queen q3 = new Queen(1,2);

// q4 to test out of bound location.

Queen q4 = new Queen(5,6);

GameBoard gameBoard = new GameBoard(5, 5);

gameBoard.setQueen(q1);

gameBoard.setQueen(q2);

gameBoard.setQueen(q3);

gameBoard.setQueen(q4);

gameBoard.printBoardState();

}

}

//--------- GameBoard.java-----------

// This is the GameBoard on which queens will be placed

public class GameBoard {

private static char INITIAL_STATE = '-';

private static char PLACED_STATE = '*';

private char[][] board;

public GameBoard(int rowSize, int colSize) {

this.board = new char[rowSize][colSize];

initBoard();

}

private void initBoard() {

if(board != null) {

for(int i=0;i

for(int j=0;j

board[i][j] = INITIAL_STATE;

}

}

}

}

public int setQueen(Queen q) {

int status = -1;

int row = q.getRow();

int col = q.getCol();

// check out of bound location first

if(row >= board.length || col >= board[row].length) {

System.out.println("Location ["+row+","+col+"] is out of board.");

status = 1;

} else if(board[row-1][col-1] == PLACED_STATE) {

// then check current state of location

System.out.println("Location ["+row+","+col+"] is occupied.");

status = 2;

} else {

// if everything is fine place the queen

board[row-1][col-1] = PLACED_STATE;

System.out.println("Queen placed properly at location ["+row+","+col+"]");

status = 0;

}

return status;

}

// show the status of board

public void printBoardState() {

System.out.println("======= Board State =======");

for(int i=0;i

for(int j=0;j

System.out.print(" "+board[i][j]+" ");

}

System.out.println();

}

}

}

Exercise 2 .Within PlayGame.java, add methods getNO and getM(). These methods will handle fetching user input for the number of Queens (N) and the dimension of the board (M). Test to make sure the user can't provide bad input .Within GameBoard, add a method called validSpace) that should take a row and a column and return a boolean value if the space is not attacked by another queen. If another queen could move to that location (given the rules of chess), then validSpace() should return false. There are several ways this method can be implemented, you can use anyway you can think of. Be sure to test this well, we want to make sure it works before introducing recursion

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

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions

Question

A primary key relates one table to another. True False

Answered: 1 week ago