Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//--------- PlayGame.java --------- // To test the game. public class PlayGame {

ID: 3710993 • Letter: #

Question

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

Explanation / Answer

The functions are as follows:

public static int getN(){
   int n = 0;
   Scanner sc = new Scanner(System.in);
   while(true){
      System.out.println("Enter number of queens:");
      n = Integer.parseInt(sc.nextLine());
      if (n <= 0 ){
         System.out.println("Invalid input: Please enetr a positive number > 0:");
      }
      else
        break;
   }
}

public static int getN(){
   int n = 0;
   Scanner sc = new Scanner(System.in);
   while(true){
      System.out.println("Enter size of board:");
      n = Integer.parseInt(sc.nextLine());
      if (n <= 0 ){
         System.out.println("Invalid input: Please enetr a positive number > 0:");
      }
      else
        break;
   }
}

public static boolean validSpace(int row, int col){

   for (int i = 0; i < board.length; i++){
       if (i != col){
          if (board[row][i] == PLACED_STATE)
             return false;
       }
   }
   for (int i = 0; i < board.length; i++){
       if (i != row){
          if (board[i][col] == PLACED_STATE)
             return false;
       }
   }
   int i = row;
   int j = col;
  
   while(i <= 0 && j <= board.length){
          if (board[i][j] == PLACED_STATE)
             return false;
          i = i-1;
          j = j + 1;
   }

   i = row;
   j = col;
  
   while(i <= 0 && j <= 0){
          if (board[i][j] == PLACED_STATE)
             return false;
          i = i-1;
          j = j - 1;
   }

   i = row;
   j = col;
  
   while(i < board.length && j < board.length){
          if (board[i][j] == PLACED_STATE)
             return false;
          i = i+1;
          j = j + 1;
   }

   i = row;
   j = col;
  
   while(i < board.length && j <= 0){
          if (board[i][j] == PLACED_STATE)
             return false;
          i = i+1;
          j = j - 1;
   }

   return true;
}