1/4/2024 0 Comments Best battleship layoutInt endrow = startrow, endcolumn = startcolumn while the first parameter is inclusive. Next() has the second parameter be exclusive, If none of the proposed panels are occupied, place the ship Select a random row/column combination, Random rand = new Random(Guid.NewGuid().GetHashCode()) We'll create a new method PlaceShips in the Player class and define it like so, and use a random number generator that I stole from StackOverflow: public void PlaceShips() Let's start coding up that algorithm, using the Player class we defined in Part 2. Given that the total number of panels (100) is much greater than the space we need to occupy (2 + 3 + 3 + 4 + 5 = 16), this is actually relatively efficient, but not perfect. If any of those panels are already occupied, or are outside the boundaries of the game board, start over from 1. Attempt to place the ship on the proposed panels.Select an orientation (horizontal or vertical) at random.Pick a random panel which is currently unoccupied.Therefore we must implement a placement algorithm which places each ship on the board but ensures that each ship does not occupy the same Panel as any other ship. In short: our ship placement will be effectively random.īut it cannot be truly random, since two ships cannot occupy the same panel. There are a lot of articles out there that purport to help you win a game of Battleship each time you play (and many of them correspond with the release of that god-awful movie), but for this practice we're not going to bother with more advanced strategies since our goal is not to win games, but to understand the game itself better by modeling it. However, there is some, so in this section we're going to implement the code which places a Player's ships, as well as output what their boards look like. Okay, so there's not a whole lot of setup involved in a game of Battleship. To start, let's think about what a Player would need to do, once s/he has all their pieces, to set up a game of Battleship. Now, let's work though how to set up a game. Our first objective is achieved: we've created the classes necessary to play a game of Battleship. This is because, in the final part of this series, we're going to run a bunch of games to see if this system gives any inherent bias to one of the Player objects. The Player class also has a ton of methods which we define in Part 3. I've taken to calling this space a Panel. The next object we need represents a single space on the game boards. We'll see a lot of those characters when we show how to play a game in the next part of this series. The Description attribute records the display character used for each of these statuses. To represent all of these statuses, I created an enumeration called OccupationType: public enum OccupationType
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |