1. ## Battle Ship AI

Hey,

I have been working on a battle ship game for a while, and I'm trying to add in a player vs. computer option. I have two functions that are used to generate a ships coordinate. The first one just generates a random coordinate (the first coord for the ship).

Also In case you didn't know some ships are bigger then others.

Then the second function second function gets a random direction that the ship should face, it also checks if the ship (depending on the length) has enough room to face the randomly chosen direction. If it can't fit, then it picks a different direction. Ok, but the part I'm having trouble with is checking if there is room to place a ship horizontally (left or right).

This is my board:
[11] [12] [13] [14] [15] [16] [17] [18]
[21] [22] [23] [24] [25] [26] [27] [28]
[31] [32] [33] [34] [35] [36] [37] [38]
[41] [42] [43] [44] [45] [46] [47] [48]
[51] [52] [53] [54] [55] [56] [57] [58]
[61] [62] [63] [64] [65] [66] [67] [68]
[71] [72] [73] [74] [75] [76] [77] [78]
[81] [82] [83] [84] [85] [86] [87] [88]

Ok, it is easy to check if there is room to place a ship up and down. Say the first random coord of a 3 space ship is 26. Then to check if it can face up (which it cant) you could use this:

Code:
`if (cord_one - 20 < 11)`
This would evaluate to 6 < 11, so you know the ship can't go there, but it gets much more complicated when I try to check left and right. I have been trying to use modules, but It's getting too confusing for me.

These are the my two statements for left and right:
(for a ship with three coords)
Code:
```if (cord_one + 2 % 10 > 8 || cord_one +2 % 10 < 3)
and
if(cord_one - 2 % 10 < 1 || cord_one - 2 % 10 > 6 )```
Here is the entire function:
Code:
```int generate_cord2 (int cord_one, int spaces) {
wait(5);
int imp = spaces - 1;
int x, y, i, f;
int go = 1;
i = r_rand(4);
while (go == 1) {
if (i == 1) {
if (cord_one - imp * 10 < 11) {
i == 2;
}
else {
f = cord_one - 10;
return 1;
}
}
else if (i == 2) {
if (cord_one + imp * 10 > 88) {
i == 3;
}
else {
f = cord_one + 10;
return 2;
}
}
else if (i == 3){
if (cord_one + imp % 10 > 8 || cord_one + imp % 10 < spaces) {
i == 4;
}
else {
return 3;
}
}
else if (i == 4) {
if(cord_one - imp % 10 < 1 || cord_one - imp % 10 > 8 - imp) {
i == 1;
}
else {
return 4;
}
}
}
}```
David

2. This is one weird function.

Code:
```int generate_cord2 (int cord_one, int spaces) {
wait(5); //why? it's not related to the function's task
int imp = spaces - 1;
int x, y, i, f;
int go = 1;
i = r_rand(4);
while (go == 1) { //meaning while (true) ? = forever?
if (i == 1) {
if (cord_one - imp * 10 < 11) {
i == 2; //You mean i = 2; ?
}
else {
f = cord_one - 10; //What for, if f gets thrown away on next line?
return 1;
}
}
else if (i == 2) {
if (cord_one + imp * 10 > 88) {
i == 3; //???
}
else {
f = cord_one + 10; //why?
return 2;
}
}
else if (i == 3){
if (cord_one + imp % 10 > 8 || cord_one + imp % 10 < spaces) {
i == 4; //???
}
else {
return 3;
}
}
else if (i == 4) {
if(cord_one - imp % 10 < 1 || cord_one - imp % 10 > 8 - imp) {
i == 1; //???
}
else {
return 4;
}
}
}
}```
Apart from that, you'll probably only need 2 directions - one vertical and one horizontal (for example right and down): a 3-square ship located at [13] and going left == a ship at [11] going right.

I hope that the names cord_one and generate_cord2 don't mean that you are going to write separate code for each ship... Oh, and does this function really generate a coordinate? It seems to return a direction.

3. I figured it out, if anyone is interested. The problem was with my if statements.
Code:
```if (cord_one + 2 % 10 > 8 || cord_one +2 % 10 < 3)
and
if(cord_one - 2 % 10 < 1 || cord_one - 2 % 10 > 6 )```
The % operator is evaluated before the + operator. (I guess that is because modulus is considered division)

It now looks like this:
Code:
```if ((cord_one + 2) % 10 > 8 || (cord_one +2) % 10 < 3)
and
if((cord_one - 2) % 10 < 1 || (cord_one - 2) % 10 > 6 )```
-The wait, is because the random function, If I don't have wait it will random the same number every time.
-The while ( go ==1 ) was supposed to be an infinite loop ( kinda improvised )
-Thanks, it was supposed to be i = 2 not i == 2. That tells it to try a different direction if the current one did not work.
-The f = cord_one - 10, was left over from how I had planed to do the function at first.

Sorry it was so messy, and thanks for your help.

4. EDIT: Just saw your post.
-The wait, is because the random function, If I don't have wait it will random the same number every time.
You won't get the same number if you srand() just once in the beginning of the program. Once the generator is seeded, when you actually call rand() is absolutely irrelevant. (I suppose you are using time with srand() inside r_rand()? Don't.)
-The while ( go ==1 ) was supposed to be an infinite loop ( kinda improvised )
while (true) or while (1) expresses this intent more clearly.

-Thanks, it was supposed to be i = 2 not i == 2. That tells it to try a different direction if the current one did not work.
Yes I realized that. Using just two directions would still make the programming a lot easier.

5. Originally Posted by anon
You won't get the same number if you srand() just once in the beginning of the program. Once the generator is seeded, when you actually call rand() is absolutely irrelevant. (I suppose you are using time with srand() inside r_rand()? Don't.)
Ok, so I call srand() at the beginning of my program, then I can use rand() when ever, and it will be completely random?

6. Originally Posted by IdioticCreation
Ok, so I call srand() at the beginning of my program, then I can use rand() when ever, and it will be completely random?
not completely, just pseudorandom...
use something like srand(time()) to use new seed on each run of your program

7. if you interested on more details regarding rand - maybe you should read http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx

8. If you really want tough battle ship AI, make it move the ships to spaces they'll fit in, if they haven't been hit yet. Or better yet, only put the ships on the board once there are just enough spaces for them all to fit left.

Quzah.

9. That sort of defeats the point.

10. Originally Posted by quzah
If you really want tough battle ship AI, make it move the ships to spaces they'll fit in, if they haven't been hit yet. Or better yet, only put the ships on the board once there are just enough spaces for them all to fit left.
Haha, isnt that cheating? In battle ship, you put down all the ships before attacking.

11. Originally Posted by IdioticCreation
Haha, isnt that cheating? In battle ship, you put down all the ships before attacking.
The "hard" level ai.