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

Need help with this JAVA project Please make sure, that we are not suppose to ma

ID: 3736672 • Letter: N

Question

Need help with this JAVA project Please make sure, that we are not suppose to make classes of use fuctions. Everything needs to be done in the main method usings loops and arrays! CS1180 Project 2 Purpose: This program will provide experience using decision statements, loops, and processing 2 dimensional arrays. The Angry Queens Game The goal of this game is to have the user place some number of angry queens onto a gam other. The game board consists of N rows and N columns. An angry queen can attack horizontally (anything in her row), vertically (anything in her column), or diagonally from her position. e board so that the angry queens cannot attack each The game starts by asking how many angry queens the user wants place (N). This determines the size of the game board (NxN). Then the user is repeatedly asked where the next angry queen should be placed. The user specifies the queen's position by entering a row and column number. Valid row and column numbers range from 0 up to N-1 Based on the user specified position, there are 3 possible outcomes for placing that angry queen: 1. If there is an angry queen at the position specified by the user, that angry queen is removed from the game board. 2 If the position specified by the user is not safe, then the angry queen is not placed on the game board and the user is informed that the position specified is not safe. . If the position specified is safe, then the angry queen is placed on the game board at that position. A safe position on the board cannot be attack already placed on the game board. This means there are no angry queens in the row specified, in the column specified, or in any of the diagonals which contain the specified position. ed by any angry queen After reaching a disposition regarding the user's specified position, the current state of the game board is displayed to the user, and the process is repeated. Once all N angry queens have been successfully placed on the game board, the game loop terminates, the user is congratulated, and asked if they wish to play again. If they wish to play on, they are once again prompted for the number of angry queens they wish to have in game To help you understand how the program should work, a sample program run is shown on the next several pages. algorithm is described below The basic

Explanation / Answer

package com.chegg;

import java.util.Scanner;

/**

* Angry Queen Game

* Place Angry queens in such a way that they are no

* where near to them by horizontally, vertically or diagonally

* If you can place all queens in proper place then you will win the game

*/

public class AngryQueenGame {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String welcomeMsg = "Welcome to the angry Queens Game! " +

"The goal of this game is to place N angry Queens on " +

"an N X N game board so the Queens cannot attack each other. " +

"Two Angry Queens cannot occupy the same row,column, or diagonal on the " +

"game board.";

System.out.println(welcomeMsg);

String[][] board; //declaring an empty board

String choice = "y";

while (choice.equalsIgnoreCase("y")) {

//Ask how many Queens

System.out.println(" Specify the number of Angry Queens, must be > 3 : ");

int boardSize = scanner.nextInt(); // Input Number of Queens from user

if (boardSize <= 3) {

//If number of Queens is less than or equal to 3 restart the while loop

System.out.println("Number of Queens must be greater than 3");

continue;

} else {

//if number of Queens is greater than 3, then initialize the board with "*"

board = new String[boardSize][boardSize];

for (int i = 0; i < boardSize; i++) {

for (int j = 0; j < boardSize; j++) {

board[i][j] = "*";

}

}

}

System.out.println("To remove an Angry Queen from the board, just specify its row and column position.");

int queen = boardSize; //Total no of Queens in the board

// Angry Queens are placed in the below section

while (queen >= 1) {

boolean rowSafe = true;

boolean columnSafe = true;

boolean diagonalSafe = true;

// Input of row & column in one line is not a best practice, as user may place some extra inputs which

// will produce exception and harder to run this program smoothly

System.out.println("Enter row position for the next Angry Queen. Must be >=0 and <" + boardSize);

int row = scanner.nextInt();

System.out.println("Enter column position for the next Angry Queen. Must be >=0 and <" + boardSize);

int column = scanner.nextInt();

//Check if user input of row and column is less than the board size, restart loop for invalid inputs

if (row >= boardSize || column >= boardSize) {

System.out.println("Your entry was invalid, try again");

continue;

}

String currentPos = board[row][column];

//Check character in the user specified position and determine action

// If the position has a queen, then remove it and increment remaining Queen count

// If row and column across the specified position has queen, then warn the player

// If Diagonal of the specified position has queen, then warn the player

// If all above conditions are false then place the queen in specified position

// and decrement remaining Queen count

if (currentPos.equalsIgnoreCase("Q")) {

System.out.println("Removed Angry Queen");

board[row][column] = "*";

queen++;

} else if (currentPos.equalsIgnoreCase("*")) { //Check whether row and column across the position is safe

for (int i = 0; i < boardSize; i++) {

if (board[i][column].equalsIgnoreCase("Q")) {

rowSafe = false;

break;

}

if (board[row][i].equalsIgnoreCase("Q")) {

columnSafe = false;

break;

}

}

//Check Diagonal to current position in forward manner from current position

for (int i = 1; i < boardSize; i++) {

int newRow = row + i;

int newColumn = column + i;

if (newRow < boardSize && newColumn < boardSize) {

if (board[newRow][newColumn].equalsIgnoreCase("Q")) {

diagonalSafe = false;

break;

}

} else break;

}

if (diagonalSafe) {

// If no Queen found in forward diagonal then check backward to current position

for (int i = 1; i < boardSize; i++) {

int newRow = row - i;

int newColumn = column - i;

if (newRow >= 0 && newColumn >= 0) {

if (board[newRow][newColumn].equalsIgnoreCase("Q")) {

diagonalSafe = false;

break;

}

} else break;

}

}

//If not safe to place Queen, then warn player

if (!rowSafe || !columnSafe || !diagonalSafe) {

System.out.println("It is not safe to place a queen there!");

for (int i = 0; i < boardSize; i++) {

for (int j = 0; j < boardSize; j++) {

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

}

System.out.println(" ");

}

continue;

} else {

//place a Queen and decrement remaining Queen count

board[row][column] = "Q";

queen--;

for (int i = 0; i < boardSize; i++) {

for (int j = 0; j < boardSize; j++) {

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

}

System.out.println(" ");

}

}

}

} //End of Inner While

//If no Queens remains the player wins the game, can also play again if he wishes to

if (queen <= 0) {

System.out.println("Awesome! You won! Do you want to paly again y or n :");

choice = scanner.next();

}

} //End of Outer While

} //End of Main Method

}

====================== Output ==================================

Welcome to the angry Queens Game!

The goal of this game is to place N angry Queens on an N X N game board so the Queens cannot attack each other.

Two Angry Queens cannot occupy the same row,column, or diagonal on the game board.

Specify the number of Angry Queens, must be > 3 :

3

Number of Queens must be greater than 3

Specify the number of Angry Queens, must be > 3 :

4

To remove an Angry Queen from the board, just specify its row and column position.

Enter row position for the next Angry Queen. Must be >=0 and <4

3

Enter column position for the next Angry Queen. Must be >=0 and <4

1

* * * *

* * * *

* * * *

* Q * *

Enter row position for the next Angry Queen. Must be >=0 and <4

1

Enter column position for the next Angry Queen. Must be >=0 and <4

0

* * * *

Q * * *

* * * *

* Q * *

Enter row position for the next Angry Queen. Must be >=0 and <4

0

Enter column position for the next Angry Queen. Must be >=0 and <4

2

* * Q *

Q * * *

* * * *

* Q * *

Enter row position for the next Angry Queen. Must be >=0 and <4

2

Enter column position for the next Angry Queen. Must be >=0 and <4

3

* * Q *

Q * * *

* * * Q

* Q * *

Awesome! You won!

Do you want to paly again y or n :

n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote