Click here to Skip to main content
15,884,388 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W24-Sep-22 2:45
Otto_W24-Sep-22 2:45 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff24-Sep-22 3:16
mveOriginalGriff24-Sep-22 3:16 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W25-Sep-22 21:20
Otto_W25-Sep-22 21:20 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff25-Sep-22 22:01
mveOriginalGriff25-Sep-22 22:01 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W29-Sep-22 16:23
Otto_W29-Sep-22 16:23 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff29-Sep-22 19:10
mveOriginalGriff29-Sep-22 19:10 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W4-Oct-22 0:29
Otto_W4-Oct-22 0:29 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff4-Oct-22 1:09
mveOriginalGriff4-Oct-22 1:09 
You don't need to construct a Point class / struct - it's already in .NET: Point Struct (System.Drawing) | Microsoft Learn[^]

And why would you need two arrays? Does it matter once the ship is in place? Or is a Ship a collection of Point values that need to be hit?

Simplest solution is to use an Orientation enum and pass a value of that to your PlaceShip method:
C#
private Random rand = new random();
...
Point loc = new Point (rand.Next(0, 10), rand.Next(0, 10));
Orientation orient = Enum.GetValues(typeof (Orientation))
                         .OfType<Orientation>()
                         .ElementAt(rand.Next(0, ));
Ship s = PlaceShip (new BattleShip, loc, orient);
The code for a random enum value may be unfamiliar to you: it uses generics (which you may not have met yet) and Linq methods (which you may not have met either) to do the work:
Gets an array of all possible values of the enum
Cast the array to a more useful type - a collection of Orientation values.
Select a random element.
I could have written it like this:
C#
orient = (Orientation)rand.Next(0, 4);
Which gives the same result - but it assumes that the orientation values are sequential: if you want to add diagonals later there are good reasons for making your enum values non-sequential:
C#
public enum Orientation
    {
    Up = 1, 
    Down = 2, 
    Left = 4, 
    Right = 8,
    }
Because now you can combine values to form "Up and Right" without changing working code.
C#
orient = Orientation.Up | Orientation.Right;
Does that make sense?
"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_W13-Oct-22 17:45
Otto_W13-Oct-22 17:45 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W13-Oct-22 17:46
Otto_W13-Oct-22 17:46 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W25-Oct-22 17:16
Otto_W25-Oct-22 17:16 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff25-Oct-22 19:34
mveOriginalGriff25-Oct-22 19:34 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W30-Oct-22 19:30
Otto_W30-Oct-22 19:30 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W15-Nov-22 14:46
Otto_W15-Nov-22 14:46 
AnswerRe: C# Battleship gameboard & random assignment Pin
Gerry Schmitz6-Sep-22 5:06
mveGerry Schmitz6-Sep-22 5:06 
QuestionChanging Windows registry with C# Pin
Ismael_199931-Aug-22 12:50
Ismael_199931-Aug-22 12:50 
AnswerRe: Changing Windows registry with C# Pin
Dave Kreskowiak31-Aug-22 14:37
mveDave Kreskowiak31-Aug-22 14:37 
GeneralRe: Changing Windows registry with C# Pin
Ismael_19993-Sep-22 4:18
Ismael_19993-Sep-22 4:18 
GeneralRe: Changing Windows registry with C# Pin
Dave Kreskowiak3-Sep-22 9:51
mveDave Kreskowiak3-Sep-22 9:51 
AnswerRe: Changing Windows registry with C# Pin
OriginalGriff31-Aug-22 18:51
mveOriginalGriff31-Aug-22 18:51 
GeneralRe: Changing Windows registry with C# Pin
Ismael_19993-Sep-22 4:20
Ismael_19993-Sep-22 4:20 
GeneralRe: Changing Windows registry with C# Pin
OriginalGriff3-Sep-22 4:43
mveOriginalGriff3-Sep-22 4:43 
GeneralRe: Changing Windows registry with C# Pin
Ismael_19994-Sep-22 11:41
Ismael_19994-Sep-22 11:41 
GeneralRe: Changing Windows registry with C# Pin
Dave Kreskowiak4-Sep-22 13:12
mveDave Kreskowiak4-Sep-22 13:12 
GeneralRe: Changing Windows registry with C# Pin
OriginalGriff4-Sep-22 19:19
mveOriginalGriff4-Sep-22 19:19 

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.