Question
Java Please!! //--------- PlayGame.java --------- // To test the game. public class PlayGame { public static void main(String[] args) { Queen q1 = new Queen(1,2);
Java Please!!
//--------- 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(); } } }
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