Click here to Skip to main content
15,879,535 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 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 
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 
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 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 
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 

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.