PART 1 BELOW:

You will be creating a program that reads (and checks) a sample play board. The playboard is a text file with the following parameters:

B means boat
* means water
= means boundary

A sample file is as below:

7, 10
==========
=********=
=***B**B*=
=***B**B*=
=***B**B*=
=********=
==========

As above, you have the following:

  • You may assume the first line will always be two numbers separated by a comma (row, column) and that the numbers are correct (numerical and reasonable). In essence, the first line will always be valid. You should use the string.split() operation. If you run into parsing issues on the column, make sure to call trim before converting it to an integer.
  • Boats can be any size, any shape, and any form. And you can have any number of boats, but correct files must have at least 1.
  • Correct files will have boundaries encircling the grid
  • Correct files only have the listed symbols above.
  • You can assume the number of rows are correct.

Things you may not assume (and must check for - PRIORITY LISTED AS BELOW):

  • The file ends with .txt ( ERROR - FILE DOES NOT END WITH .TXT ) and exists ( ERROR - FILE DOES NOT EXIST ) .
  • That the playboard is the correct size for a col ( ERROR - COL MISMATCH ) . You will need to account for under amounts and over amounts.
  • That the file will have correct boundaries (ERROR - MISSING BOUNDARY) or if there is a boundary not on the edge (ERROR - BOUNDARY NOT ON EDGE). Make sure to account for both cases when you have the first line of the map that has a boundary. And the last line has a boundary.
  • That the file will have correct symbols ( ERROR - UNKNOWN SYMBOL )
  • That there is at least one boat ( ERROR - NO BOAT )

Once parsed, the following information between the words PART ONE and the last bullet point above above must be wrapped in a method OUTSIDE OF MAIN (aka you have to make an operation) and return a char 2 dimensional array. For the remainder of the assignment, you must parse the map listed above as a 2D array. Do not convert it back to a string. Do not convert it to another data structure. This is a requirement for your grade. EX:

public static char[][] convertTextToArray(String fileName) {
//stuff going on
}
public static void main(String args[]) {
//Error check command line stuff
char [][] board = convertTextToArray(fileName);
}
Hint: If you want to error and exit within a method, you must call System.exit.
Hint: You can call methods within other methods.
Hint: If you are dealing with char primitive data types, in order to do comparisons or assignment you will need to use the single quotes (not double). Ex: char a = 'a';

Although it is not required at this point, go ahead and write a method designed to print your board. This will give you good feedback to make sure your output matches the file (if correct). You will need this method for the second part, anyways. Below are sample test cases. Make sure your errors match the errors listed EXACTLY.

badCase1.txt - ERROR - COL MISMATCH
badCase2.txt - ERROR - COL MISMATCH
badCase3.txt - ERROR - MISSING BOUNDARY
badCase4.txt - ERROR - MISSING BOUNDARY
badCase5.txt - ERROR - MISSING BOUNDARY
badCase6.txt - ERROR - UNKNOWN SYMBOL
badCase7.txt - ERROR - NO BOAT
badCase8.txt - ERROR - BOUNDARY NOT ON EDGE
badCase9.txt - ERROR - MISSING BOUNDARY

Do not download files directly. Create files locally on your machine and copy paste contents. (Note that badCase9.txt has 8 rows. However, you should catch missing boundary before that becomes an issue. )

PART TWO

Once you have read in the file, you will now play a game. Essentially you will run your application as the following:

Map:

==========
=********=
=********=
=********=
=********=
=********=
==========
You have 10 shots remaining.
Please enter a coordinate:

Now as you see above, you will show the map, hiding the B's as water, and showing the boundary as listed for the example above. Note that if you use a different map file, you should see a different map.

Now you will need to enter a coordinate. Lets say, for example, I enter 3 4 (use the operation nextLine() on Scanner.)

Please enter a coordinate:
3 4
3 is row coordinate and 4 is column coordinate. The map is zero indexed at the top left.
3, 4 was a HIT!
Map:
==========
=********=
=********=
=***H****=
=********=
=********=
==========

You have 9 shots remaining.
Please enter a coordinate:

Now what if they put in a miss? Lets say we pass 1 1

1, 1 was a MISS!
Map:
==========
=.*******=
=********=
=***H****=
=********=
=********=
==========
You have 8 shots remaining.
Please enter a coordinate:

Now what if they attack a boundary? Lets say we pass 0 0

ERROR - OUT OF BOUNDS
Map:
==========
=.*******=
=********=
=***H****=
=********=
=********=
==========

You have 8 shots remaining.
Please enter a coordinate:

What if you are feeling lazy, and want to calculate random?

In this case, you will calculate random by stating rand. You will state this by saying RAND in all caps.

  • If they do not give coordinates, or the word RAND, state ERROR - NEED TWO NUMBERS OR RAND
  • Incorrect coordinates (this includes trying to shoot on the map where '=' symbol exists): ERROR - OUT OF BOUNDS
  • If they hit the same spot twice, state ERROR - ALREADY SHOT THERE
  • Don't worry about any other error check case (Eg 3 numbers, or words as numbers that are not RAND, etc)

In these cases, you will ignore the inputted value and ask the user again and Do not deduct from total shots. Do not crash the program.

HINTS:

  • Check for the word RAND first, then try to see if its a number.
  • To separate by space, I used String.split over the space. This returns a sized 2 string if it splits correctly, 1 otherwise (Google is your friend).

**********Calculate random************** (Important, please read): Call Random rand = new Random(seed); ONCE! At the beginning of program. Not in a loop. Not in a method. Create random row before random column. You will only do this once per RAND call. Do not use a loop to generate random values to see if they are in boundaries. Look at Lab 18 task 3. HINT HINT. Make sure the ranges are from 0 to row size and from 0 to col size. Your random can create values where you attack the same spot that's already been hit before. That is okay. The program will state ERROR - ALREADY SHOT THERE, and will ask for user input again. Note, with the ranges, that means it can attack a boundary as well. Same as before, it will state ERROR - OUT OF BOUNDS.

You have 8 shots remaining.
Please enter a coordinate:
RAND

2, 5 was a MISS!
Map:
==========
=.*******=
=****.***=
=***H****=
=********=
=********=
==========
You have 7 shots remaining.
Please enter a coordinate:

The game continues to play until either the following two conditions are met:

  • You run out of shots and there is at least one B on the field. In this case, you game over.
  • You manage to kill all the Bs on the field. Then you get a You win message. Keep in mind if your last shot kills the last B on the map, then you will still win.

If you lose, you state the following:

"Sir, there are " + boatCount + " ship points remaining. You Lost!"

Where boatCount represents each position on the grid where there is a boat that has NOT been hit.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.