Jump to content
  • Advertisement
Sign in to follow this  

Console Battleship

This topic is 3995 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I need some tips/advice on some ways of storing the positions of the various Battleship game pieces. I was thinking of trying to use a two dimensional char array, something like "char Battleship [5][5] {A1,A2,A3,A4,A5} ... {E1,E2,E3,E4,E5}". I then was thinking of using an Enum? Would the game be to simple for classes? Thanks for any help

Share this post


Link to post
Share on other sites
Advertisement
i have done some programs using arrays before... it works pretty good for stuff like this...
you probably know this already... but if you don't...
lets say your array is battleship[5][5]...
now that would be...
{0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0}
lets say you want some ships in there...
{0,0,0,0,0,
1,0,0,0,0,
1,0,0,0,0,
0,2,2,2,2,
0,0,0,0,0}
you just have to make the program interpret the different numbers or variables if you want

i dont really think you need to get into anything very complex for storing the locations...

have fun with it :)

Share this post


Link to post
Share on other sites
Creating an array may seem the simple way to do it, but there is really no need for one. Also determining if a ship is sunk is a little complicated.

I think a better way would be to store each ship's position and orientation. The locations of the points it occupies can easily be deduced from those values.

Since each ship has data associated with it, it is natural to create a class to contain that data -- so no, the game is not too simple for classes.

Share this post


Link to post
Share on other sites
Yeah, this isn't like Tetris where you can add pieces to the board. They stay in one spot and that's it. That said you can do it that way which has some advantages or you could try having just one Ship class as JohnBolton said. All ships are the same, basically, they only differ on position, length, direction, and name. However, you may try a different approach than that which may make calculations easier overall. Have a class Ship that stores a std::vector of piece locations where a piece contains an X and Y coordinate as well as a status variable that tells if the piece was hit. Then you just keep a list of pieces of the ship and when the opponent strikes you look at each ship and see if the attack location matches any of the piece location.

However, you still need to keep track of where you attacked. This is the drawback of using a ship class. You'll need another two vectors filled with attack locations for each player that you check before attacking every time. If you used a game board then you just mark where you attacked and every attack check if there was an attack there. Each position can have about 8 different states. Five for each type of ship, an empty state, an attacked and empty state, and an attacked and hit state.

This isn't to say "don't use classes" just that a 'ship' class may or may not be appropriate in this case. Have a Board class that contains the some type of array (2D array, or Boost::MultiArray, or 2D std::vector, or 1D array that you treat as a 2D array, or a 1D std::vector you treat as a 2D std::vector, etc) and each player gets two boards that the main program interacts with. One board will be used to store attack locations, the other for pieces and where you were hit. Actually, you really only need one board for each player and then you just cheat and have the computer look into the opponents board for information on what was hit or not.

The sky's the limit. In fact, it may be a good exercise for you to try writing the code both ways over two projects so you can see how vastly different your code will turn out by using a Board class vs. a Ship class.

Share this post


Link to post
Share on other sites
Thanks for all the great advice.

I forgot to mention that this would be a human vs computer game, at least for now.

I had been thinking about using a ship class. In it, it would have some private members for the ship's direction, ie horizontal or vertical as well as the ship's start position, such as A1 and then, based on the direction and the ships "size" it would determine the end point.

Is this a game that has been done before as a text based game, and if so, is there an example of such a game? If not, is there a game that I can use that is similiar to this one?

Thank

Share this post


Link to post
Share on other sites
Just a question: how are you planning to impelement the portion of the game when the player decides his own ship positions. I'm thinking it's not very easy to insert the positions with text.

Share this post


Link to post
Share on other sites
For now, the game would just involve the player guessing where the computer ships are located.

I agree, using text for the player to place his ships would not be very easy to implement.

Share this post


Link to post
Share on other sites
I don't see why reading a ship position from text would be hard. Print "Where do you want to place your battleship?", read in two characters, verify that they make sense, ask whether they want it to be facing horizontally or vertically. That should be exactly what you need to initialize your ship class right there. Repeat for all the other ships.

Share this post


Link to post
Share on other sites
I'd make a class like others said of ship data and store it in a vector.

For the AI you'll need a simple structure that holds shot information so the computer doesn't keep hitting places it's already shot at. We humans in the board game get those white pegs to mark a miss, the computer needs similar info. Along with hit info so it keeps firing in the same area it already scored a hit in.


struct ShotInfo
{
int x, y;
bool hit;
}

std::vector<ShotInfo> AIShots;



I just woke up so please forgive me if the code doesn't work perfectly, I will blame it on the sleep in my eyes! :)

Good luck!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!