Text based battleship game AI in Java

From C++ to PHP, debugging to webhosting; help and discussion about writing your latest program to running your website. NOT for help when your PC won't work.

Announcements Posted on
Enter our travel-writing competition for the chance to win a Nikon 1 J3 camera 21-05-2013
Sign in to Reply
  1. djshine88's Avatar
    • Full Member
    • Location: Huddersfield
    • Posts: 99
    Text based battleship game AI in Java
    Hi guys.

    I just need help for a bit of a summer project. A while ago for my module assessment, i made a Text based battleship game in Java in which you enter coordinates to take a shot and the computer randomly does it.

    Now i got this all working, but there was one part of the project i never finished - and would really love someone's help or even the coding to make this happen. We were supposed to make an AI that basically - if the computer hit a ship, the next shot the computer took would be one space after the existing shot.

    Dies that make sense?? Also, can anyone please help me?? I just wanna be able to understand it.
  2. TheGrinningSkull's Avatar
    • Overlord in Training
    • Posts: 2,074
    Re: Text based battleship game AI in Java
    (Original post by djshine88)
    Hi guys.

    I just need help for a bit of a summer project. A while ago for my module assessment, i made a Text based battleship game in Java in which you enter coordinates to take a shot and the computer randomly does it.

    Now i got this all working, but there was one part of the project i never finished - and would really love someone's help or even the coding to make this happen. We were supposed to make an AI that basically - if the computer hit a ship, the next shot the computer took would be one space after the existing shot.

    Dies that make sense?? Also, can anyone please help me?? I just wanna be able to understand it.
    I'll say it in pseudocode (or whatever makes sense XD)

    ( > shows indent due to if then statement and repeat loop)

    target is made random (if previous was miss unless you enter the repeat until loop below)
    IF hit=true THEN
    > LET centre=hit position
    > LET position of hit=centre (useless, may as well condense the 2 previous lines to saying hit position as being position of hit, wasn't sure if you needed centre elsewhere so that's why I included it at the start)
    > NEXT player turn
    > REPEAT
    >> <direction> ::= right|left|up|down
    >> target = position of hit +1 (direction)
    >> IF hit=true THEN LET target = position of hit +1 (direction)
    >> IF hit=miss THEN LET <direction> ::= opposite of what direction was
    >> IF hit=miss AND <direction> has been 2 opposites then direction will be one of the remaining 2 (May need to fix a bug if it comes to the 3rd and 4th directions as it'll do this because you've already had 2 opposites and I can't be asked fixing it right now )
    >> NEXT player turn
    > UNTIL ship is sunk (which the computer determines when the ships are placed somehow)
    > NEXT player turn
    ENDIF
    NEXT player turn
    EDIT is underlined and made italic
    Last edited by TheGrinningSkull; 29-05-2012 at 17:31.
  3. djshine88's Avatar
    • Full Member
    • Location: Huddersfield
    • Posts: 99
    Makes good sense... I just have to really try and implement this. Thanks


    This was posted from The Student Room's iPhone/iPad App
  4. TheGrinningSkull's Avatar
    • Overlord in Training
    • Posts: 2,074
    Re: Text based battleship game AI in Java
    (Original post by djshine88)
    Makes good sense... I just have to really try and implement this. Thanks


    This was posted from The Student Room's iPhone/iPad App
    No problem, I've edited the first post to give it more context and hopefully make more sense.

    I've also added a next player turn and an endif at the end so that if hit is not true at the beginning, it'd go onto the next player and start the routine again. (Unless the miss comes when you are in the middle of the repeat loop.
  5. VisualKiddy's Avatar
    • Exalted Member
    • Posts: 348
    Re: Text based battleship game AI in Java
    (Original post by TheGrinningSkull)
    I'll say it in pseudocode (or whatever makes sense XD)

    ( > shows indent due to if then statement and repeat loop)

    target is made random (if previous was miss unless you enter the repeat until loop below)
    IF hit=true THEN
    > LET centre=hit position
    > LET position of hit=centre (useless, may as well condense the 2 previous lines to saying hit position as being position of hit, wasn't sure if you needed centre elsewhere so that's why I included it at the start)
    > NEXT player turn
    > REPEAT
    >> <direction> ::= right|left|up|down
    >> target = position of hit +1 (direction)
    >> IF hit=true THEN LET target = position of hit +1 (direction)
    >> IF hit=miss THEN LET <direction> ::= opposite of what direction was
    >> IF hit=miss AND <direction> has been 2 opposites then direction will be one of the remaining 2 (May need to fix a bug if it comes to the 3rd and 4th directions as it'll do this because you've already had 2 opposites and I can't be asked fixing it right now )
    >> NEXT player turn
    > UNTIL ship is sunk (which the computer determines when the ships are placed somehow)
    > NEXT player turn
    ENDIF
    NEXT player turn
    EDIT is underlined and made italic
    (Original post by djshine88)
    X
    Small note of observation: Later on you might need to think about adding an escape for the loop incase two ships are placed directly side by side, and the AI hits the left one, moves one space right and hits, it would continue to move right, miss, then start moving left again I think?

    Obviously just outside the scope of forum psuedocode but I thought it might be worth throwing my thoughts onto paper anyway.

  6. TheGrinningSkull's Avatar
    • Overlord in Training
    • Posts: 2,074
    Re: Text based battleship game AI in Java
    (Original post by VisualKiddy)
    Small note of observation: Later on you might need to think about adding an escape for the loop incase two ships are placed directly side by side, and the AI hits the left one, moves one space right and hits, it would continue to move right, miss, then start moving left again I think?

    Obviously just outside the scope of forum psuedocode but I thought it might be worth throwing my thoughts onto paper anyway.

    That's a good thought, nice one.

    Is there some sort of rule (Not sure if my memory is right) where you have to leave a space between ships?

    If this is defined when placing the boats in the beginning then the loop should be fine but good shout either way!
  7. djshine88's Avatar
    • Full Member
    • Location: Huddersfield
    • Posts: 99
    Tbh we never really gave "rules"... It just had to have some form of intelligence, it didn't necessarily say it couldn't hit other ships...

    It just kept hitting until it couldn't find anymore ships...


    This was posted from The Student Room's iPhone/iPad App
Sign in to Reply
Share this discussion:  
Useful resources
Article updates
Moderators

We have a brilliant team of more than 60 volunteers looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Reputation gems:
The Reputation gems seen here indicate how well reputed the user is, red gem indicate negative reputation and green indicates a good rep.
Post rating score:
These scores show if a post has been positively or negatively rated by our members.