# My next 'dos console' game...

This is a discussion on My next 'dos console' game... within the C++ Programming forums, part of the General Programming Boards category; I was thinking of battleship, would that be cool or what? That shouldn't be TOO hard, right?...

1. ## My next 'dos console' game...

I was thinking of battleship, would that be cool or what? That shouldn't be TOO hard, right?

2. Battle ship being not TOO hard???are you on something?

3. Battleship's reasonably easy.

Connect-4 would be easy. Go is easy enough to program, rules-wise, but the AI would be trickier.

4. ## Ok

Battleship wouldnt be that hard. The only hard part is the AI. It would just take a long time to write out all the code for it. Thinking of how to do it is an easy task so far. Just to prove it, now I'm definitely going to do it. Muahahahaha!

5. Well, you have some options for AI:

* If no hits on unsunk ships are present, you can try a few things:

1) Randomly fire
2) Fire only on areas that could conceivably have a ship -- you need to figure out the size(s) of the remaining ship(s), figure which elements could contain one or more of these ships, and fire on any random one of them.
3) Fire in an area that is "most likely" to contain a ship -- i.e. given the size of the remaining ship(s), figure how many ways each grid point could contain a ship, and of all of those with the highest probability, one is randomly chosen.

The AI will improve as you move from 1-3, but so will complexity. 2) is probably a good choice, 3) would take a little more effort, but there would be ways to get around having to recalculate the probability of each square on the board -- you only need to recalculate the squares whose probablility changes after each move.

In fact, 3 could be implemented with a little careful thought. If you set up classes and methods intelligently, it wouldn't be THAT tricky.

* If you have previously hit an unsunk ship, the algorithm is simpler -- try to figure out which direction the ship lies on, and continue to fire on that line until sunk.

6. If you want to do battleships then try to take an oop approach and use virtual functions and inheritance properly.

make a class like BoardItem that contains a pure virtual function called Shot(). Then derive some classes from BoardItem like AirCarrier,Cruiser,OpenSea. Each of these can override the virtual function shot. A possible implementation of OpenSea version could be :-

cout<<"Splosh"<<flush;

implementation in aircraft carrier would be ...

cout<<"Bang !! You hit my aircraft carrier"<<flush;

the beauty of all this is when you make the board this is really an array of pointers to objects of these classes.You then call shot on whatever object in the array because all array items are derived from BoardItem and the correct thing happens. Read about polymorphism. You can tie all the ai into this sort of structure too.