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:
Things you may not assume (and must check for - PRIORITY LISTED AS BELOW):
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. )
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.
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:
**********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:
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.