Click here to Skip to main content
15,509,982 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Service: Error 1053: The service did not respond to the start or control request in a timely fashion Pin
temuco8-Sep-22 23:34
professionaltemuco8-Sep-22 23: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 2:38
professionaltemuco9-Sep-22 2:38 
QuestionC# Battleship gameboard & random assignment Pin
Otto_W5-Sep-22 18:50
Otto_W5-Sep-22 18:50 
AnswerRe: C# Battleship gameboard & random assignment Pin
OriginalGriff5-Sep-22 20:11
mveOriginalGriff5-Sep-22 20:11 
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 
OK ... first off, to play battleships you need at least two boards: one with your ships on, one with his.
So write your methods with that in mind! If you don't, when you come to add the second board, you have to mess around with the existing, tested, working code to get it to work with extra boards. And remember, on the "hardware" version of the game, you have a total of four boards: Your ships / his hits, your hits his ships; and the same pair for him.
Guilty Gadgets Battleships Sea Battle Traditional Family Fun Combat Strategy Board Game : Amazon.co.uk: Toys & Games[^]
While you can do it with just the one, it's a clumsy solution that only saves about 10 * 10 * 4 * 3 bytes and makes your code a lot clumsier.

And don't declare methods inside other methods: it makes the code clumsy and hard to read.

I'd start with a class: give it a board area, and add the CheckGuess method to that. Then create four instances of the class in your main method.

C#
public class BattleshipsBoard
   {
   private Square[,] TheBoard = new Square[10, 10];
   public Square CheckGuess(int x, int y)
      {
      ...
      return whatTheSquareContained;
      }
   }
...
   public static void Main(string[] args)
      {
      BattleshipsBoard myShips = new BattleshipsBoard();
      BattleshipsBoard hisShips = new BattleshipsBoard();
      BattleshipsBoard myGuesses = new BattleshipsBoard();
      BattleshipsBoard hisGuesses = new BattleshipsBoard();
      ...
Now all the boards are independant and you can query any of them the same way:
C#
if (myShips.CheckGuess(hisX, hisY) == Square.Ship)
   {
   ...
Or
C#
if (hisShips.CheckGuess(myX, myY) == Square.Ship)
   {
   ...
Your code becomes more readable, and easier to work with.

Give it a try!
"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_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 
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 

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.