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 | |
-
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. -
Re: Text based battleship game AI in JavaI'll say it in pseudocode (or whatever makes sense XD)(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.
( > 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 italicLast edited by TheGrinningSkull; 29-05-2012 at 17:31. -
Re: Text based battleship game AI in JavaNo problem, I've edited the first post to give it more context and hopefully make more sense.(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
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. -
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 italicSmall 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?(Original post by djshine88)
X
Obviously just outside the scope of forum psuedocode but I thought it might be worth throwing my thoughts onto paper anyway.
-
Re: Text based battleship game AI in JavaThat's a good thought, nice one.(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.
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!
)