Click here to Skip to main content
15,881,172 members
Home / Discussions / C#
   

C#

 
GeneralRe: reading memory stream in byte chunks Pin
mjeeves10-Sep-22 10:04
mjeeves10-Sep-22 10:04 
GeneralRe: reading memory stream in byte chunks Pin
harold aptroot10-Sep-22 10:15
harold aptroot10-Sep-22 10:15 
QuestionC# Service: Error 1053: The service did not respond to the start or control request in a timely fashion Pin
temuco8-Sep-22 20:41
professionaltemuco8-Sep-22 20:41 
AnswerRe: C# Service: Error 1053: The service did not respond to the start or control request in a timely fashion Pin
Richard Deeming8-Sep-22 21:49
mveRichard Deeming8-Sep-22 21:49 
GeneralRe: C# Service: Error 1053: The service did not respond to the start or control request in a timely fashion Pin
temuco8-Sep-22 22:34
professionaltemuco8-Sep-22 22:34 
General[Solved] Re: C# Service: Error 1053: The service did not respond to the start or control request in a timely fashion Pin
temuco9-Sep-22 1:38
professionaltemuco9-Sep-22 1:38 
QuestionC# Battleship gameboard & random assignment Pin
Otto_W5-Sep-22 17:50
Otto_W5-Sep-22 17:50 
AnswerRe: C# Battleship gameboard & random assignment Pin
OriginalGriff5-Sep-22 19:11
mveOriginalGriff5-Sep-22 19:11 
Why are you using a DataTable? It's a bit of a sledgehammer to crack a nut ...

You need a 10 x 10 area of "squares" which can contain four values: Empty, Ship, Hit, and Miss. So the simplest way to do that is you create an enum that has all of those values:
C#
public enum Square
{
    Empty,
    Ship,
    Hit,
    Miss,
}
You can then create an array of that:
C#
Square[,] MyBoard = new Square[10,10];
Or two:
C#
Square[,] HisBoard = new Square[10,10];
You can now reference any particular Square via indexes:
C#
if (MyBoard[x, y] == Square.Ship)
		{
			MyBoard[x, y] = Square.Hit;
            ...
		}
Create a method to clear a board:
C#
public static Square[,] Clear(Square[,] board)
{
	for (int x = 0; x < board.GetLength(0); x++)
	{
		for (int y = 0; y < board.GetLength(1); y++)
		{
			board[x, y] = Square.Empty;
		}
	}
	return board;
}
And you can start getting ready for a new game:
C#
Square[,] MyBoard = new Square[10,10];
Square[,] HisBoard = new Square[10,10];
Clear(MyBoard);
Clear(HisBoard);
Then just write another method to load your ships in: it accepts a board as a parameter, calls Clear. and then decides where to put the ships. (Initially, I'd try to write it using "single space" ships to make the code easier, then start working on the more complex shapes you really need).

In fact what I'd do is more complex than that: I'd encapsulate the Square array in a class called Board, several ship classes (Carrier, BattleShip, Cruiser, Submarine, Destroyer) derived from a base Ship class) and let it handle the mundane stuff so that most of the time you are manipulating Board objects:
C#
MyBoard.place(myDestroyer, x, y, Orientation.Horizontal);

C#
MyBoard.Bomb(6, 7);
But if you haven't reached classes and instances yet then a basic array will be fine.
"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_W6-Sep-22 15:51
Otto_W6-Sep-22 15:51 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff6-Sep-22 19:19
mveOriginalGriff6-Sep-22 19:19 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W16-Sep-22 21:54
Otto_W16-Sep-22 21:54 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff16-Sep-22 22:13
mveOriginalGriff16-Sep-22 22:13 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W24-Sep-22 1:09
Otto_W24-Sep-22 1:09 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff24-Sep-22 1:34
mveOriginalGriff24-Sep-22 1:34 
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 
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 

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.