Click here to Skip to main content
15,513,571 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W6-Sep-22 16:51
Otto_W6-Sep-22 16:51 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff6-Sep-22 20:19
mveOriginalGriff6-Sep-22 20:19 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W16-Sep-22 22:54
Otto_W16-Sep-22 22:54 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff16-Sep-22 23:13
mveOriginalGriff16-Sep-22 23:13 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W24-Sep-22 2:09
Otto_W24-Sep-22 2:09 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff24-Sep-22 2:34
mveOriginalGriff24-Sep-22 2:34 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W24-Sep-22 3:45
Otto_W24-Sep-22 3:45 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff24-Sep-22 4:16
mveOriginalGriff24-Sep-22 4:16 
That's a good start. Big Grin | :-D

OK, you have a board class, what do you put on the board?
In the real game, you don't add "single square" ships: you add 4 x Destroyer (two squares), 3 x submarines (three), 2 x Battleships (four) and 1 x Carrier (five).

So why not create an abstract base class Ship, which is inherited by the concrete classes Destroyer, Submarine, Battleship, and Carrier?
The abstract class has properties Length and Name which the concrete classes implement. The Name is so you can say "you sank my Destroyer!" without knowing which concrete class it is, and the Length says how many squares it takes.

Add a Add method to your board which accepts a Ship, a top left coordinate, and an orientation (NSEW) and modify your Hit method to see if it's sunk.

This way, the board doesn't need to know what ship it is, and it enforces rules like "you can't put it there because it hang over the side" and such like.

Have a think about it - I know it sounds complicated, but it's really pretty easy when you get the hang of it and it really does make your code easier to write and modify!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!

GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W25-Sep-22 22:20
Otto_W25-Sep-22 22:20 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff25-Sep-22 23:01
mveOriginalGriff25-Sep-22 23:01 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W29-Sep-22 17:23
Otto_W29-Sep-22 17:23 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff29-Sep-22 20:10
mveOriginalGriff29-Sep-22 20:10 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W4-Oct-22 1:29
Otto_W4-Oct-22 1:29 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff4-Oct-22 2:09
mveOriginalGriff4-Oct-22 2:09 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W13-Oct-22 18:45
Otto_W13-Oct-22 18:45 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W13-Oct-22 18:46
Otto_W13-Oct-22 18:46 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W25-Oct-22 18:16
Otto_W25-Oct-22 18:16 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff25-Oct-22 20:34
mveOriginalGriff25-Oct-22 20:34 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W30-Oct-22 20:30
Otto_W30-Oct-22 20:30 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W15-Nov-22 15:46
Otto_W15-Nov-22 15:46 
AnswerRe: C# Battleship gameboard & random assignment Pin
Gerry Schmitz6-Sep-22 6:06
mveGerry Schmitz6-Sep-22 6:06 
QuestionChanging Windows registry with C# Pin
Ismael_199931-Aug-22 13:50
Ismael_199931-Aug-22 13:50 
AnswerRe: Changing Windows registry with C# Pin
Dave Kreskowiak31-Aug-22 15:37
mveDave Kreskowiak31-Aug-22 15:37 
GeneralRe: Changing Windows registry with C# Pin
Ismael_19993-Sep-22 5:18
Ismael_19993-Sep-22 5:18 
GeneralRe: Changing Windows registry with C# Pin
Dave Kreskowiak3-Sep-22 10:51
mveDave Kreskowiak3-Sep-22 10:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.