Battleship!
To learn a little more about JavaFX and to code a little AI, I wrote a Battleship game. I am most interested in the code, and not the interface, so don't expect slick graphics with sound and explosions.
But you will find a rather difficult to beat game of Battleship. Download the jar and run it. Enjoy!
But you will find a rather difficult to beat game of Battleship. Download the jar and run it. Enjoy!
![]()
|
Note: Requires Java on your PC to run.
|
The AI uses two types of maps when deciding where to shoot. When no ships have been hit, it calculates the possiblities for every square on the map. The starting possition with the calculated likelyhood of a ship is shown below on the left. On the right is what the possibilties 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!