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

I need help with a Java project using arrays. A correct code to use as a referen

ID: 3752928 • Letter: I

Question

I need help with a Java project using arrays. A correct code to use as a reference for this so I can see how to structure something like this would really help.

Instructions:

Output Examples:

Create two separate programs for this project. The first program randomly creates a map that the second program uses for the game. In the game, the player searches the map looking for five treasures. After each guess, the map either shows they found a treasure ('X marks the spot), or a clue telling them if they are close to a treasure (hot 'H or cold C'). The player wins when they find all five treasures without getting captured by the pirate that patrols his treasures (P") ogram 1 - Map Generator 1. Create a 9x9 array of ''s 2. Randomly distribute 5 'X's (treasures) throughout the grid 3. Make sure that the randomizer cannot choose the same location twice and overwrite an 'X' (ie. make sure you always have 5 'X's) 4. Write the array to a file named "treasures.txt" in 9x9 format. Program 2 - Treasure Hunt Game 1. Main - create two 9x9 arrays, one for storing the map, and the other for displaying the board to the user. Also create variables to store information about the pirate 2. Load Map - create a function to read in the map from the file "treasures.txt" and stores it in a 2D array, return the array 3. Clear Board - create a function to reset the board grid with all '~s 4. Input Checks - create functions to get and return valid input (you may use the CheckInput class provided on Beachboard and add functions as needed) Take in a letter (A-I) for the row (this is a good place to convert user input from a letter to a integer for array access) Take in a number (1-9) for the column. Take in a menu option (1-2) Replay Level (Y/N) Check that the spot hasn't already been revealed a. b. c. d. e. 5. Display Board - create a function that passes in the board and the pirate's information, and then displays the board. Display the numbers 1-9 above the grid and the letters A-I along the side. Display the pirate's location when necessary 6. Get Clue - create a function that detects the treasure. Pass in the map and the location to check, examine that location and the eight spots surrounding the location the user entered. If that location has one of the treasures, return an X', if there is treasure nearby, return an 'H' for hot, otherwise return a 'C for cold Note: the program will throw an exception if you try to access a location outside the bounds of the arrav, either do a bounds check on the values, or use exception handling a. Allow the user to play the game by prompting them to enter a location, then reveal that location, but watch out, the pirate starts at a random location on the map and then randomly moves one space (up, down, left, or right) after each guess (don't allow the pirate to move off the map). Keep a counter of the number of treasures left to find and display it after every round. If they find a treasure, decrement the counter, if not, show the clue, if the clue is 'H', reveal the pirate's location, otherwise he stays hidden. If the user successfully locates all 5 treasures, then they win. If they are captured by the pirate then they lose the game. Allow them to replay the same map if they choose to

Explanation / Answer

package Game;

import java.util.*;

// Defines a class TreasureHunt

public class TreasureHunt

{

// To store board data

String board[][];

// To store random treasure positions

int treasurePos[][];

// To store number of treasure left

int treasureLeft;

// To store number of treasure found

int treasureFound;

// Scanner class object declared

static Scanner sc;

// Default constructor to initialize instance variables

TreasureHunt()

{

// Dynamically allocates memory to board

board = new String[10][10];

// Dynamically allocates memory to treasure position

treasurePos = new int[10][10];

// Initializes number of treasures to 5

treasureLeft = 5;

// Initializes number of treasures found to zero

treasureFound = 0;

}// End of default constructor

// Method to create a board

void createBoard()

{

// Loops for rows

for(int r = 1; r <= 9; r++)

// Loops for columns

for(int c = 1; c <= 9; c++)

// Assigns "~" symbol to each cell

board[r][c] = "~";

}// End of method

// Method to display the board

void displayBoard()

{

// Displays number of treasure left to hit

System.out.println(" " + treasureLeft + " treasure left.");

// Prints space

System.out.print(" ");

// Loops for first row to display numbers

for(int r = 1; r <= 9; r++)

System.out.print(r + " ");

// Prints new line

System.out.println();

// Loops for rest of the rows to display alphabet and board contents

for(int r = 1; r <= 9; r++)

{

// Displays the alphabet

// 64 is added to row value to get ASCII value of alphabet ('A' as 65)

System.out.print((char)(64+r) + " ");

// Loops through columns to displays board contents

for(int c = 1; c <= 9; c++)

System.out.print(board[r][c]);

System.out.println();

}// End of for loop

}// End of method

// Method to display menu, accept user choice and return user choice

int getMenu()

{

// To store user choice

int ch;

// Creates scanner class object

sc = new Scanner(System.in);

// Displays menu

System.out.println("1. Hunt");

System.out.println("2. Quit.");

// Accepts user choice

System.out.print("Enter your choice: ");

ch = sc.nextInt();

// Returns user choice

return ch;

}// End of method

// Returns true if row letter and column letter is with in the board boundary and it is '~'

// Otherwise returns false

boolean checkInput(char rowLetter, int col)

{

// Initializes valid status to false

boolean valid = false;

// Checks if the row letter is between 'A' to 'I' and column value is between 1 - 9

if(rowLetter >= 'A' && rowLetter <= 'I' && col >= 1 && col <= 9)

// Checks if row position and column position contains "~"

if(board[(int)(rowLetter - 64)][col] == "~")

// Set the valid status to true

valid = true;

// Return valid status

return valid;

}// End of method

// Method to generate random treasure position in the board

void generateTreasure()

{

// Creates a Random class object

Random r = new Random();

// Loops 5 times to generate 5 random treasure position

for(int x = 0; x < 5; x++)

{

// Generates a random number for row position

int row = r.nextInt(9) + 1;

// Generates a random number for column position

int col = r.nextInt(9) + 1;

// Assigns 1 to the randomly generated row and column position

treasurePos[row][col] = 1;

}// End of for loop

}// End of method

// Method to assign a character on the board based on the condition

void getClue(char rowLetter, int col)

{

// Checks if the given row and column of treasurePos array contains 1

// Then hit the treasure

if(treasurePos[(int)(rowLetter - 64)][col] == 1)

{

// Assigns "X" to parameter row and column index position of board

board[(int)(rowLetter - 64)][col] = "X";

// Increase the hit count by one

treasureFound++;

}// End of if condition

// Otherwise checks if the parameter row and column is not the boarder of the board

else if((int)(rowLetter - 64) != 9 && col != 9)

{

// Checks the top, bottom, left and right index position near to the

// parameter row and column value is 1

if(treasurePos[(int)(rowLetter - 64)][col+1] == 1 ||

treasurePos[(int)(rowLetter - 64)][col-1] == 1

|| treasurePos[(int)(rowLetter - 64)+1][col] == 1

|| treasurePos[(int)(rowLetter - 64)-1][col] == 1)

// Assigns "H" to parameter row and column index position of board

board[(int)(rowLetter - 64)][col] = "H";

// Otherwise assigns "C" to parameter row and column index position of board

else

board[(int)(rowLetter - 64)][col] = "C";

}// End of else if condition

// Otherwise assigns "C" to parameter row and column index position of board

else

board[(int)(rowLetter - 64)][col] = "C";

// Decrease the left count by one

treasureLeft--;

}// End of method

// Driver method definition

public static void main(String[] args)

{

// Creates a scanner class object

sc = new Scanner(System.in);

// To store user choice

char choice;

// To store the letter for row

char letter;

// To store column number

int col;

// Creates an object of the class TreasureHunt using default constructor

TreasureHunt t = new TreasureHunt();

// Calls the method to create board

t.createBoard();

// Calls the method to generate 5 random treasure

t.generateTreasure();

// Loops till user choice is 'Y' or 'y'

do

{

// Checks if number of treasure left is 0 (end of current game)

if(t.treasureLeft == 0)

{

// Checks if number of treasure found is equals to 5 (All treasure hit)

if(t.treasureFound == 5)

// Display win

System.out.println(" You Won!" );

// Otherwise Display loss

else

System.out.println(" You Loss!" );

// Accepts the user choice to continue

System.out.println("Try again (Y/N)" );

choice = sc.next().charAt(0);

// Checks if the user choice is 'Y' or 'y' (Play again)

if(choice == 'Y' || choice == 'y')

{

// Calls the method to create board

t.createBoard();

// Calls the method to generate 5 random treasure

t.generateTreasure();

// Reset the treasureLeft and treasureFound to 5 and 0 respectively

t.treasureLeft = 5;

t.treasureFound = 0;

}// End of if inner condition

// Otherwise stop the game

else

break;

}// End of outer if condition

// Calls the method to display current board values

t.displayBoard();

// Calls the method to accept user choice for menu

int ch = t.getMenu();

// Checks if user choice is 1

if(ch == 1)

{

// Accepts the row letter and column value

System.out.print(" Enter row letter: ");

letter = Character.toUpperCase(sc.next().charAt(0));

System.out.print(" Enter column number: ");

col = sc.nextInt();

// Calls the method to check the validity of the position

if(t.checkInput(letter, col))

{

// Calls the method to play game

t.getClue(letter, col);

// Calls the method to display current board values

t.displayBoard();

}// End of if inner condition

// Otherwise display error for invalid position

else

System.out.print(" ERROR: Invalid position");

}// End of outer if condition

// Otherwise quit the game

else

break;

}while(true); // End of do - while loop

}// End of main method

}// End of class

Sample Output:

5 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~~~~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~~~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

1. Hunt

2. Quit.

Enter your choice: 1

Enter row letter: A

Enter column number: 6

4 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~~X~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~~~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

4 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~~X~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~~~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

1. Hunt

2. Quit.

Enter your choice: 1

Enter row letter: A

Enter column number: 4

3 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~X~X~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~~~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

3 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~X~X~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~~~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

1. Hunt

2. Quit.

Enter your choice: 1

Enter row letter: G

Enter column number: 4

2 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~X~X~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

2 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~X~X~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

1. Hunt

2. Quit.

Enter your choice: 1

Enter row letter: B

Enter column number: 2

1 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~X~X~~~

B ~X~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

1 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~X~X~~~

B ~X~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

1. Hunt

2. Quit.

Enter your choice: 1

Enter row letter: F

Enter column number: 4

0 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~X~X~~~

B ~X~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~X~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

You Won!

Try again (Y/N)

y

5 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~~~~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~~~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

1. Hunt

2. Quit.

Enter your choice: 1

Enter row letter: G

Enter column number: 4

4 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~~~~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

4 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~~~~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~~~~~~~~~

1. Hunt

2. Quit.

Enter your choice: 1

Enter row letter: I

Enter column number: 2

3 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~~~~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~C~~~~~~~

3 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~~~~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~C~~~~~~~

1. Hunt

2. Quit.

Enter your choice: 1

Enter row letter: A

Enter column number: 5

2 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~X~~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~C~~~~~~~

2 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~X~~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~~~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~C~~~~~~~

1. Hunt

2. Quit.

Enter your choice: 1

Enter row letter: D

Enter column number: 2

1 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~X~~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~H~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~C~~~~~~~

1 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~X~~~~

B ~~~~~~~~~

C ~~~~~~~~~

D ~H~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~C~~~~~~~

1. Hunt

2. Quit.

Enter your choice: 1

Enter row letter: D

Enter column number: 1

0 treasure left.

1 2 3 4 5 6 7 8 9

A ~~~~X~~~~

B ~~~~~~~~~

C ~~~~~~~~~

D XH~~~~~~~

E ~~~~~~~~~

F ~~~~~~~~~

G ~~~X~~~~~

H ~~~~~~~~~

I ~C~~~~~~~

You Loss!

Try again (Y/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