代写C++代码 代做C++程序 C++辅导 C++教学

C++网络家教 远程写代码 在线Debug 讲解答疑 不是中介,本人直接写

微信: ittutor QQ: 14061936 Email: ittutor@qq.com

导航

Suppose you want to develop and play a PC-based, single-user, simplified version of battleship (google “battleship game” if you aren’t familiar with it). Just like with most single-user games, you will not play against a real opponent, but rather against a simulated opponent, whose initial map is stored in a text file containing a description of where your opponent’s ships are located The idea is that when your program starts, it will read the description of the opponents map from the file and store it in a 2-d array. The person playing the game, of course, will not see the file or the 2-d array, except at the end of the game. Throughout the game, the player repeated trys to guess where battleships are located by specifying individual map coordinates (just like in the actual game). For each guess, the program will tell the player if they hit or miss. After each time the player makes a guess, the program will print out an “ASCII art” representation of the playing field (the currently known portion of the map). This will allow the player to see their progress as they play the game. 


Since there is technically no opposing player (nobody is trying to guess where the player’s battleships are), the player will be limited by the number of “shots” they can take. If the player sinks all of the battleships in fewer than the limit, then they win, otherwise they lose. 


Note that your program will use two command-line arguments – the first is the shot-limit, and the second is the name of the file that stores the opponents map.


 >javac BattleShip.java


>java BattleShip 5 map.txt 


Map file format: 


8 7


~~~~~~~


~~~~~~~


B~~~~~~


~~~~~~~


~~~~B~~


~~~~B~~


~~~~~~B


~~~~~~~


 


Notes:


·         ~ symbol dictates empty water.

·         B symbol dictates a battleship.

·         The first number is the number of rows.

·         The second number is the number of columns

·         The row count and column count are always separated by a space. You can assume the first line is always correct.

·         The map always starts on the second line after the row and column numbers


 


Part 1: Reading the map file.


For the first part of the assignment, your goal is to read the file and check for errors in the map file (sometimes configuration files like this get corrupted). Your program should print error messages when appropriate (described below).


AS PART OF THE ASSIGNMENT, YOU WILL NEED TO HAVE AT LEAST TWO METHODS FOR PART 1  (you can name them what you want, but they must have the same variable datatype signatures). If you don't know what datatype signatures are, just copy/paste.


·         public static void printMap(char [][] map)

·         public static char[][] processMap(String filePath, int shots)


 


processMap - Takes a map filePath and the shot-limit. This operation generates a 2 dimensional array of chars based on the contents of the map file.


printMap – Takes a 2d character array representing a map and prints it to the console


 


The first method should also check the map file for errors. If an error is detected then the method should exit the program by calling System.exit(1). In this method, you might also be inclined to use Scanner.nextLine() along with Scanner.nextInt(). DO NOT USE SCANNER.NEXTLINE()! As many of you have figured out, mixing nextLine and nextInt causes some very strange issues.


 


After reading the file your program should print the following initial map (if there are no errors in the file). Remember, B's are hidden because the whole point is for the player to guess where the Bs are:



>javac BattleShip.java


>java BattleShip 5 good1.txt


Map:


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


Captain, please enter coordinates (5 shots remaining):


 


At this point the player will enter coordinates of their guess. This will be addressed below, but for now let’s discuss the error checking that must take place.


 


Part 2: Playing the game.


Just like with the actual battleship game, the player will need to specify coordinates for each guess.  As with arrays, the map is zero indexed. Also note that order for coordinates is row followed by column. Do not reverse this. It will cause you to lose a lot of points (because it is wrong).  For example: 


Map:


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


Captain, please enter coordinates (5 shots remaining):


2 3 


The first number is the row position and the second is the column position. Based on these, the program should tell you if you hit or miss. 


MISS!


Map:


~~~~~~~


~~~~~~~


~~~M~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


Captain, please enter coordinates (4 shots remaining): 


As you see on the displayed map, the program adds the letter 'M', standing for “miss,” on the coordinate. Also note that the number of remaining shots has been reduced. Now for an example of hitting: 


Map:


~~~~~~~


~~~~~~~


~~~M~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


Captain, please enter coordinates (4 shots remaining):


2 0


HIT! 


Map:


~~~~~~~


~~~~~~~


H~~M~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


~~~~~~~


Captain, please enter coordinates (3 shots remaining): 


As you can see, H appears since there was a B in the guessed position. 


There are two outcomes to the game. If the player manages to hit all the Bs before they run out of shots, they win.


·         Captain, we have sunk all the battleships. You win! 


If the player runs out of shots before taking out all the battleships, they lose.


·         Captain, we ran out of ammo. You lose!


 


Error checking for part 2:


Just like with inputting the map, you will have to error check while the game is being played. However, these error checks will not cause the program to terminate. Furthermore, if the player enters something erroneous, it will not deduct from the remaining number of shots.


 


·         ERROR: Not a valid set of coordinates

·         ERROR: Illegal row

·         ERROR: Illegal column

·         ERROR: Out of bounds

·         ERROR: Already shot in that position


 


Hints:


*Not a valid set of coordinates: coordinates must include both a row and column (of some type; just check the size here. If size != 2, throw error).


*Use exception handling (i.e., try/catch). Example coordinates: (a, 1) is Illegal row since the first position is a character and not a number.


 


Notes on part 2:


·         Make a new method called playingTheGame() or something similar. This method will call printMap to print the map in between turns.

·         Once again, skip error checking for this section initially, and focus directly on making the game function correctly.

·         Hint: make sure to check for the special case where the player’s last shot hits the last B.

·         Clean up output, make sure the game works.

·         Add error checking. Make sure errors do not deduct from shots.

·         Make sure the game works correctly, including all error checking.    


General notes on tackling the assignment:


·         This assignment is worth 200 points and given over the course of several weeks. Spend your time wisely.

·         You should use Scanner for reading in the file. Use anything else at your own peril.


相关推荐