Trouble with Battleship Game

I just finished coding my little Battleship game, and everything works okay for some quick work. Right now ships don't have three sections like the regular battleship game. They are just a standalone 'S' on the display grid. Before I try to make them more like a ship, I need to work on placing the ships. I pseudorandomly place the ships in the 10x10 grid. The problem is they are too close for comfort. Like they are more favored to being in the upper-left corner.

I do this with this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Position ships on grid pseudorandomly. NEEDS WORK!!!
//Ships group too close together for comfort.
void positionShips() {
	int x;
	int y;
	//enemy ships, MAX_SHIPS
	for (int i = 0; i < MAX_SHIPS; i++) {
		do {
		x = getRandInt(0, MAX_LENGTH-1);
		y = getRandInt(0, MAX_LENGTH-1);
		} while (eGrid[x][y] != 0);
		eGrid[x][y] = 1;
	}
	//player ships, MAX_SHIPS
	for (int i = 0; i < MAX_SHIPS; i++) {
		do {
		x = getRandInt(0, MAX_LENGTH-1);
		y = getRandInt(0, MAX_LENGTH-1);
		} while (pGrid[x][y] != 0);
		pGrid[x][y] = 1;
	}
}

int getRandInt(int min, int max) {
	return rand() % max + min;
}


Placing them this way is not really a good way to do this. I guess what I'm trying to do is to create a better random number generator. Then maybe some ideas on how I could implement ships that have more than one part.
Here's a sample of how the ships are being placed:

1
2
3
4
5
6
7
8
9
10
11
12
13
 Your Grid:
 A  S  S  .  .  .  S  .  .  S  .
 B  .  .  .  .  .  .  .  .  .  .
 C  .  .  .  .  .  .  .  .  .  .
 D  S  .  .  .  .  .  .  .  .  .
 E  .  .  .  .  .  .  .  .  .  .
 F  .  .  .  .  .  .  .  .  .  .
 G  S  .  .  S  .  .  .  .  .  .
 H  .  .  .  .  .  .  .  .  .  .
 I  .  .  .  .  .  .  .  .  .  .
 J  .  .  .  .  .  .  .  .  .  .

    1  2  3  4  5  6  7  8  9  10


BTW, each point on the grid has a number. 0 = empty or unknown, 1 = ship inside, 2 = miss, 3 = hit so if dGrid[x][y] = 1 then there is a ship there.
Do a desk test with extreme values.
Suppose getRandInt(10, 15); (a random number between 10 and 15), you will find out that goes out of range. Also could you ever get 15?

enum state{empty, unknow, ship_inside, miss, hit};
Yeah, I now see I'm going to have to rethink my random number generator. Also, I don't know why I didn't think of using an enumerator. That was a 'DUH!' moment.
Topic archived. No new replies allowed.