Question
Help with some C# console application for a classic game of battleship. I have been planning and prototyping builds for this game. From this I
Help with some C# console application for a classic game of battleship. I have been planning and prototyping builds for this game. From this I have thought about the best way to do this will be to have a couple of different classes to help protect the script from becoming too long and doing too many different things. I am also trying to learn how classes work within C# so I figure having multiple classes will help me iron out exactly how classes interact in C#.
I know to have a class be usable in another class you make it a public method and then do this:
ExampleClass bringOverInfo = new ExampleClass();
bringOverInfo.MethodName();
For the Board.cs I want it to:
Display a 10x10 board (which I did code below)
Use an 'X' for miss, 'O' for hit, and when cheat mode is on 'S' to see the locations of the ships (not sure how to feed this information in)
With a display method (I have a display method for the board but not sure how to store the information for the X, O, and S)
For the Ship.cs I want it to:
Have a length property for the ships that ranges from 2-5 (already did this, code below)
Variables for the xStern and yStern to contain the location of the stern of the ship (not sure how to do this)
Variables for the xSBow and yBow to contain the location of the bow of the ship (same as above)
For RunGame.cs I want it to:
Hold the game logic
Place the ships at random following the same rules of no overlapping boats, no boats hanging off the edge, and no diagonal placements
Hold the ship information such as the type of ship they are and size they are, except I do not want a patrol boat and have added a ship for a total of 6
a. 2 Destroyers of length 2
b. 2 Submarines of length 3
c. 1 Carrier of length 5
d. 1 Battleship of length 4
When the player sinks a ship display a message (Simple message I wrote is Console.WriteLine("Ship hit and sunk");)
Finally when all ships are sunk display a message asking the player if they want to play again (Simple message I wrote is Console.WriteLine("Do you want to play again? (y/n)");)
Finally, I want a simple Main.cs with it having no more than 1-2 commands in it.
Code I have written
Board.cs
class Board { private char[,] board = new char[10, 10]; public void Display() { // to move through the size of the board for (int row = 0; row < board.GetLength(0); row++) { // create the sections Console.Write("|"); for (int column = 0; column < board.GetLength(1); column++) { // set the current slot getting filled Console.Write(board[row, column] + "|"); } Console.WriteLine(); } }
Ship.cs
class Ship { private int shipLength;
public int Length { get { return shipLength; } set { if (value >= 2 && value <= 5) { shipLength = value; } else { Console.WriteLine("Invalid ship size"); } } } }
Thank you for any help you can give me!!
Step 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