Battleship!
Just as an artist creates quick sketches to try out ideas or just to practice, this BattleShip game is a demonstration program I coded to learn a little more about JavaFX and to code a little AI. I am most interested in the code, and not the interface, so don't expect slick graphics with sound and explosions. It does not have a splash screen, copyright notices, high scores, personalization or anything else a full-fledged game would have.
But it does work quite well. You may find it rather difficult to beat this little game of Battleship. Download the jar file and run it. Enjoy!
But it does work quite well. You may find it rather difficult to beat this little game of Battleship. Download the jar file and run it. Enjoy!
|
Note: Requires Java on your PC to run.
|
The AI has two states that uses a different map to decide where to shoot. When no ships have been hit, it calculates the possibilities for every square on the map. The starting position with the calculated likelihood of a ship is shown below on the left. On the right is what the possibilities map looks like after the first shot is a miss. The AI will now randomly choose between the highest values on the map (which is 14).
A Map of Possibilities before the first shot:
|
After the 1st shot is a miss:
|
After a hit has been made the AI switches to a probabilities map showing which locations will most likely have a ship. Below on the left a hit has been made on E5 so the most likely locations with a ship are the highest numbered locations, in this case the 4s. On the right is the probabilties map after a miss in F5. Now G5, H5 and I5 are 0 and the ship is most likely in E4, D5, or E6.
The AI continues like this and plays a pretty good game of Battleship. Give it a try!