Bryce B.

asked • 04/28/22

Computer Science Code

I need the code and some explanations to the following prompt for coding because I am struggling with arrays and loops(1301 computer science)

How could I create a game for combat ships? I have to allow users to pick a board size between 6x6 and 10x10, then generate placements for 2 of each of the following ships: scouts(size 2), combat ships (size 3), and carriers (size 4). Ships should be placed either horizontally or vertically, but cannot cover the area of another ship. Once the board is generated and the ships are placed, allow the user to target the board without knowing the ship placement. If the user selects a location with a ship, display "hit", otherwise "miss". Once all slots containing a ship are hit, "You sunk my shipName" where shipName is the type of ship sunk. Once all ships are sunk, display "You Win!"

I need to create a combat ships class with one attribute, a 2D array of integers for my game board. My constructor should also call a method to place my initial ship positions. I believe the ship position should take one argument which could either be a ship id number or ship length. The position method should be placed at one ship at a time and should be called by the instructor 6 times. The method should first select an empty location on the game board, then attempt to place the rest of the ship in adjacent indices either horizontally or vertically. If one of those indices is already occupied, check in other directions. If all other directions is occupied, pick a new start state and try again.

The program should include a method for targeting a ship, and returning if its a hit or a miss.

The program should also include telling the user when they sink a ship and when they have won the game.

Also include a TOstring method that displays the current state of the board.

In the main method:

-Prompt the user to provide a board size, and use that to instantiate a Combat Ships object.

-In a loop ask the user to select a position to strike

-Tell the user if they hit or miss, inform the type of ship that they have sunk, and that they have won once all the ships have been sunk

-When the user has won use the Tostring method to display the state of the board

Thanks so much!

Donald W.

This problem is too big to be answered in a single answer. If you still need help with this, you should seek out a tutor to go through this in detail. There's a lot here.
Report

04/29/22

Bryce B.

Hi Donald, could you at least provide any code for this prompt?
Report

04/29/22

Donald W.

The description is essentially asking you to build the entire Battleship board game from scratch. I would start with defining what data structures you would use to represent the board game and the pieces. This is also going to depend on what language you're coding in. Using C is going to limit your options. C++/Java would be better. Python and Javascript would give you different options. Like I said, this is not a simple question.
Report

04/29/22

Bryce B.

The language that has to be used is C#, so with that being said where would you start from there @Donald W.
Report

04/29/22

Donald W.

Like I said, start with the data structures. Define your classes first and decide how you're going to represent the board, the pieces, the moves, etc. Then you can start adding logic.
Report

04/30/22

Bryce B.

thank you so much
Report

04/30/22

1 Expert Answer

By:

Emily C. answered • 06/11/25

Tutor
New to Wyzant

Computer Science Tutor & University Senior

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.