• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

C# Battleship gameboard & random assignment

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Repo: https://github.com/Charles-CarM/Battleship


I am currently building my battleship console application in C#. I have prior experience
building projects with Vanilla JS and in React. I went through a tutorial on C# and could
grasp most of the concepts pretty well. I have jumped into project building because I know
this is where the maximum amount of growth will be as a developer. With that being said I
will lay out the issues arising in my application.

requirements
   *  the ship has a length of 5
   * ship will be randomly assigned on a 10X10 grid
   * when the ship is hit five times it is sunk and game is over

I am having trouble choosing the best way to implement the 10x10 gameboard grid. With
some searching on the web I have came across the DataTable class, which allows for rows
and columns to be set in a table(I believe this to be the better approach), and I have attempted
to get them displayed in the Console below. Also I know that I can create a 10x10 grid with
a 2D array using int[,] grid = [10,10]; however I have realized that I can't assign the random values for the ship with an already populated array. I would like to get some feedback on how
I can move forward with creation of the gameboard, and random assignment of the ship on the board.

I know this post and my code are a little messy so please excuse me on that part. Any solutions or suggestions you recommend for me moving forward are welcomed and greatly appreciated. Hope you are very well wherever you may be.

 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The usual approach to a problem like this is to start by thinking about a representation of the board which can work with the operations you need.

Fairly obviously, a Battleships board is a two-dimensional structure, which suits a two -dimensional array, matrix or table. We agree up to that point, but now I get puzzled. I don't understand why you have created a two-dimensional structure filled with the numbers 1..100. At this point, I would be thinking about the possible states of each "cell" in the structure.

Off the top of my head, the possibilities are:
  • empty cell (i.e. the water)
  • empty cell which has been fired at (i.e. a miss)
  • cell with something in it (i.e. a ship)
  • cell with something in it which has been fired at (i.e. a hit)

  • So we have four possibilities which can be represented in many ways: as numbers (e.g 1,2,3,4), as charactrers (e.g. W,M,S,H), or binary values (e.g. 00,01,10,11), or strings or enumerations, etc.

    Once you have decided on a representation, the next step would seem to be to fill your board with the representation of water. Now you have somewhere that you can place your ship(s).

    This is slightly tricky, as you need to make sure you don't accidentally position a ship hanging off the edge of the board, but the essence is to randomly choose an orientation (up/down or left-right) and a starting position, then loop through the number of cells in the ship filling the appropriate locations on the board with the symbol for ship.

    Does that make any sense? Have I addressed any of your problems?
     
    Charles CarMichael
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Frank for taking the time to read my post and provide meaningful feedback.

    The thought process of having a 2D array filled with numbers 1-100 was for me
    to see the representation of a 10x10 grid, so I have it in there as I was playing in the
    sandbox for understanding.

    I see what you are saying about the "state" of each cell having four options
    that you've listed(W,M,S,H). I think I'll shoot for representing them as strings
    on the game board.

    Yeah so the ship(s) need to be bounded within the parameters of the game board like you said.
    I'll have to think further of how I am going to set up those boundaries.
    So the orientation will have two options, then a starting position followed by a loop, which
    will fill in the cells with a string value of "ship".

    This gives me a good understanding of how to move forward and you've made a ton of
    sense addressing my problems with the battleship application.

    I'm going to follow you on LinkedIn if that's okay?

    Thanks again Frank!
     
    Sheriff
    Posts: 17734
    302
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    EDIT: I forgot this was C# - please translate whatever I wrote to appropriate C#.    The general approach/idea should still be viable in C# though.

    Another approach, although it requires more imagination perhaps.

    Instead of representing the board as a two-dimensional array or table or whatever, you can just note the maximum coordinates and assume the minimum would be (0,0). Then your ship will just keep track of which points that it occupies. Orienting the ship vertically or horizontally is a matter of randomly choosing a starting point and iterating upwards in whatever direction it's going. The math isn't that complicated to work out.

    Then, you can query each ship if it was hit or not at a specific point.


    You'd have to add more methods to that class of course, to manage and query any hits it has taken or check if it's still alive or sunk.

    The way I like to think of this is "If the ship had some intelligence in it, what kind of things could I ask it to do, and what kind of questions could ask of it?"

    The answer might be along the lines of:
    - I should be able to tell a ship to orient itself randomly (horizontal, vertical, or diagonal) starting from a given point that would put it within the bounds of a grid that has a maximum coordinate of (MAX_X, MAX_Y).
    - I should be able to check if the ship is hit at a particular coordinate
    - I should be able to check if the ship is still afloat or sunk

    Then I choose a method that would most appropriately represent that kind of interaction. For example, to check if a ship is hit at a particular coordinate, I might decide to have this interaction with it:

    Deciding on how I interact with an object like Ship is called "designing the object's API" or Application Programming Interface. A well-designed API makes the code easier to understand and easier to work with and fits will with an intuitive mental model of the problem and solution.
     
    Bartender
    Posts: 15737
    368
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't have anything new to contribute to the topic, I just wanted to convert Junilu's code to C#:


     
    Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic