anyone made a battleship game with C?

Omegachi

Diamond Member
Mar 27, 2001
3,922
0
76
i need some help for an assignment, can you give me some tips on how to place the enemy ships randomly on the grid? I think i need to use a 2d array. Not sure how to do it though.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
You know the size of each ship.
Run a random number generator from 1 to # of squares of your grid.
When you get point, check if you have the required number of open squares that include your target point.

That is where your ship will be placed.

If you have multiple choices involving the target square, set up some random algorithm to reduce your choices.
 

Omegachi

Diamond Member
Mar 27, 2001
3,922
0
76
actually, i will have 3 ships. 1,2,and 3 segment ships. the problem is that i might have the ship segments placed outside of the ocean grid or overlapin other ships.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0


<< When you get point, check if you have the required number of open squares that include your target point. >>



Once you place a ship you need to indicate that the squares that it covers are occupied. This will then be detected by your next search and invalidate that option.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Then if you really want a challenging game, allow either one or all ships to move 1 square every # turns.

Adds a lot more realism into the game. Just like when you would cheat in the real game by moving your ships out of danger when a searching pattern was being used by your opponent.

When I built the game, the advanced option allowed 1 movement per ship every turn unless a ship was hit more than 30%.