Thread: Battle Ship AI

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    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;
           }
        }
        }
    }
    Thanks in advance!
    David
    Last edited by IdioticCreation; 01-05-2007 at 12:11 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    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)

    So I just had to add parentheses

    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 )
    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.
    -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.
    Last edited by IdioticCreation; 01-05-2007 at 01:03 PM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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. #5
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Quote 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. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote 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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you interested on more details regarding rand - maybe you should read http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    That sort of defeats the point.

  10. #10
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Quote 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. #11
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    Quote Originally Posted by IdioticCreation
    Haha, isnt that cheating? In battle ship, you put down all the ships before attacking.
    The "hard" level ai.
    Illusion and reality become impartiality and confidence.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple space combat AI
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 01-06-2009, 12:54 AM
  2. chess ai contest
    By Raven Arkadon in forum Contests Board
    Replies: 7
    Last Post: 07-09-2005, 06:38 AM
  3. AI Contest Proposal
    By MadCow257 in forum Contests Board
    Replies: 4
    Last Post: 03-13-2005, 03:27 PM
  4. Game Design Topic #1 - AI Behavior
    By TechWins in forum Game Programming
    Replies: 13
    Last Post: 10-11-2002, 10:35 AM