Answered step by step
Verified Expert Solution
Question
1 Approved Answer
java please Write an interactive, text-based program that allows the user to play a one-player simplified Battleship game. This game will use a 1-dimensional array.
java please
Write an interactive, text-based program that allows the user to play a one-player simplified Battleship game. This game will use a 1-dimensional array. The user will play against the computer. You can read more about the general game on Wikipedia Links to an external site. In this game of Battleship, the computer will randomly place ships somewhere in a 1-dimensional area. The user will guess positions. If the user guesses all positions of a ship, they sink that ship. If they sink all ships, they win. Game Requirements - (25 points) The ships: - The computer must create at least 3 ships. - The ships cannot all be the same size. - The ships must be randomly placed within the array. - (25 points) User communication: - Provide information to the user about the number of hits and misses. Provide a message about whether each guess is a hit or miss. - If the user guesses a number outside of the acceptable range, provide an error message. - Your program should continue- not crash- when this happens. - Note: if the user enters a non-number, it's okay if your program crashes. We'll learn how to prevent this later in the semester! - (10 points) Ending the game: - If the user hits all positions on all ships, the user wins. - If the user gets more than the maximum number of allowed misses, the user looses. - After the user wins or loses, ask them if they want to play again. Programming Requirements - Place all of your code inside of one class: Battleship.java. - Note: because we have not yet covered classes and objects, you are not required to create separate game/interaction classes. - Use at least one 1-dimensional array to represent the location of the ships. - Note: you may choose to use more than one array! - (20 points) Break your code up into logical methods. - Do not put all of your code inside of main. Write static methods and invoke those methods from main. - Carefully consider the best logical breakdown for your methods. - (20 points) For full credit, follow good principles of object-oriented programming and method design. - Follow naming conventions. - Think carefully about what variables you need. - Use constants where appropriate. - Avoid duplicating code. Reuse code whenever possible. - Think carefully about method headers (e.g., what is the best return type). - You will receive no credit for files that do not compile. tes and Suggestions - To avoid problems with switching between reading numeric input and text input from the user, use this code to read in number input: - int number = Integer.parselnt(scan.nextLine()) - For variables that you want to use across methods (for example, the array that represents the ship locations, the number of guesses, etc.), you can declare the outside of any method and make it static. - For example: private static int numberOfGuesses; - We'll learn more about exactly why you'd use this type of declaration later in the semester! - It's often a good idea to write a program like this in "layers." - This means get one part working correctly and then add another part. Test that part and get it working before adding the next part. - Before you ever write code, sketch out all the components you need and make a plan for the order in which you'll write them. - For example, you might start by writing a program that works only for a single boat. - Perhaps you first write the code that randomly places that boat. - Get that working first before writing code that interacts with the user. - Next, add code to read in user guesses. - Next, add code that evaluates those guesses for hits/misses. - You might then modify the program to work with multiple boats. - You might leave the "play another" functionality to be added last, once the rest of the program is working. - It can be very useful to print out information during testing/programming that won't be printed in the final version of the game. - For example, the random locations of the ships (e.g., System.out.println(Arrays.toString(shipLocations)); - It can be helpful to start with a small array size for testing purposes (e.g., 20) and then only increase the size once the game is workingStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started