Solved by verified expert:java
java1.docx
Unformatted Attachment Preview
Surname 1
Upload to Canvas the source files (.java files) and screen output
(copied and pasted into the end of the main file, commented out),
all zipped in one file
Problem: You will be writing a program to “play” a one-player,
simplified Battleship game. You MUST write your own class
(SEPARATE from HW2_Methods class) that has main. In main,
declare and allocate memory for one 10 X 10 two-dimensional
array of ints (I’m calling it shipGrid) for placed ships the AND also
one 10 X 10 two-dimensional array of ints (I’m calling it hitGrid),
and do the following:
• Call a method to display the Battleship rules: “Welcome to
CIS 35A’s One-person Battleship game! There will be 5
ships placed on a 10 x 10 grid, one of each of size 1, size 2 ,
size 3, etc. The game ends when you sink them all, make
25 guesses, or decide to quit at the end of a turn.” (put n in
good places!)
• **Repeat the following
◦ Call the fillShipGrid method (given in the
HW2_Codefile) passing the shipGrid and final static
constant for 5.
◦ Call a method (that you write) to set all the elements in
hitGrid to 0
◦ Call a method to play Battleship (I’m calling it
playBattleship, see details in 3. below), passing
shipGrid and hitGrid (and you should pass the final
static constant for 5)
◦ Repeat to ** as long as the wantsToContinue method
(in the HW2_Methods class given in the
HW2_CodeFile) returns true (see test runs for what to
pass for the String prompt)
(Details of the above methods below and on the next pages!!!)
1 You’re required to use the wantsToContinue() method (in the
separate HW2_Methods class) that’s in the HW2_Codefile
(see code file for description)
Surname 2
2
3
•
•
•
•
•
•
•
•
•
•
•
•
1
You’re required to use the fillShipGrid() method (in the
separate HW2_Methods class) that’s in the HW2_Codefile
(see code file for description)
In the playBattleship method (that you write), do the
following in a loop:
***Call a method (I’m calling it displayGrid, details in 4.
below) passing hitGrid
Prompt the user for the row letter and column number of
his/her guess, and read into rowLetter and colNum
Calculate the rowNum (int) based on the rowLetter, and
adjust the colNum (decrement by 1)
Call a method (I’m calling it checkGrid, details in 5. below),
passing shipGrid, hitGrid, rowNum and colNum and assign
the return value to gridStatus (int local variable)
If the gridStatus is –1, display “Invalid row or column”
If the gridStatus is –2, display “Spot already guessed”
If the gridStatus is 0, display “Not a hit”
If the gridStatus is > 0, then display “Made a hit”
If the gridStatus is >= 0, increment the guess counter and
call a method (I’m calling it setHitGrid, see details in 6.
below). If the return value of setHitGrid is > 0 and < 10
display "Sunk a ship: ", increment the sunk counter and
display the return value
If the user made < 25 valid guesses and not all the ships
have been sunk (check the ship counter), and the
wantsToContinue method returns true, repeat to *** (see test
runs for what to pass for the String prompt)
After the loop, display a message "Game Over" AND...
◦ if the user sunk all the ships, display a message "You
sank all the ships!"
◦ otherwise, if the made 25 valid guesses, display "You
used up all your allowed guesses"
Call displayResults (details in 8. below)
In the displayGrid method, do the following
Surname 3
•
•
1
•
•
1
•
•
Display headers for columns 1 – 10 (leaving space on the
left for the columns of row letters)
For each row (0 – 9) in the hitGrid:
◦ Display the row letter, starting with 'A' for row 0, 'B' for
row 1, etc.
◦ For each element in the row, display one of the
following based on an element:
▪ '-' (hyphen) if the element has 0 (spot not checked
yet)
▪ 'm' if the element has –1 (means a miss)
▪ Number of the ship, if the element > 0 and < 10
(shows which ship has been sunk)
▪ 'H' if the element >10 (means a hit)
In the checkGrid method, checks if valid rowNum (0 – 9) and
colNum (0 – 9). If not valid, return –1. Otherwise:
If the element in hitGrid at the rowNum & colNum isn’t 0,
return -2 (already chosen)
Return the element in shipGrid at the rowNum & colNum
In the setHitGrid method, you’ll be updating the hitGrid and
possibly the shipGrid if a ship is sunk, at the given rowNum
and colNum (all parameters), and return the hit status (more
details below). Check the following:
if the element in shipGrid at the rowNum & colNum is 0, set
the hitGrid at the same rowNum & colNum to -1 and return 1
If the element in shipGrid at the rowNum & colNum is > 0,
◦ add 10 to the element value and assign it to the hitGrid
element at the same rowNum & colNum. For example,
if the element in shipGrid at the rowNum & colNum 3,
assign 13 to the hitGrid’s corresponding element.
◦ check if there are the same number of elements as the
element value in the same row, or same column (for
example, if you assigned 13, check for 3 elements with
13 in the same row or same column). (you may assume
Surname 4
the same number will always be contiguous in a row or
column). If so,
▪ change the hitGrid elements (for example 13’s) to
the element %10 (for example 3)
▪ change the shipGrid elements at the same
locations to 0 (ship was sunk, remove from
shipGrid)
▪ return the ship number (for example 3) if sunk,
return ship number + 10 if not sunk
1 In the displayResults method, display “Ships not sunk” and
call displayGrid passing the shipGrid . Then display “Your
results” and call displayGrid (for the hitGrid).
Include in your program:
• 2 – two-dimensional array of ints (10 X 10) (declare and
allocate in main) DON’T ALLOCATE the 2-dim. arrays more
than once in the program!
• static final (class-scope) variables for 10 (dimension sizes), 5
(number of ships) and 25 (number of guesses) but use the
ones for 10 ONLY WHEN ALLOCATING MEMORY FOR
THE 2-dim. arrays!
• YOU MUST USE THE length ARRAY VARIABLES TO
CHECK THE SIZES (or use in the for loops) TO MAKE
SURE YOU DON’T GO OUTSIDE THE ARRAY BOUNDS!
• MAKE SURE YOU PASS THE 2-dim. arrays to the
appropriate methods, but NOT the dimension sizes!
Turn in a run with at least 3 games played in which one you quit
early, one you guess all the ships, and one you reach 25 guesses
without sinking all the ships. See test runs on Canvas (separate
link).
SUGGESTION: To test this, display the shipGrid before calling
playBattleship so you know where the ships are, and set a smaller
set of ships (like 4) and smaller maximum guesses (like 12).
Surname 5
NOTE: If you want to change any of the above design, you MUST
get PERMISSION from the instructor OR YOU MAY GET
POINTS DEDUCTED!! Be aware that HW#3 will be an Object
Oriented version of this program.
REMEMBER, efficiency counts (so try not to write code that’s
inefficient or slow)!
…
Purchase answer to see full
attachment
You will get a plagiarism-free paper and you can get an originality report upon request.
All the personal information is confidential and we have 100% safe payment methods. We also guarantee good grades
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more