Freitag Software
The Joy of Writing Software
  • Home
    • Links
  • My Software
    • JavaScript Programs >
      • Dots
      • The Code Cracker
      • The Deserted Ship
      • Flying Text
    • Java Programs >
      • Dot Animation
      • Operation Rescue >
        • Mazes for Programmers
      • Battleship!
      • Blackjack
      • Draw Poker
      • State Pattern Demo
      • Falling Blocks
    • Android Programs >
      • The Oracle
      • RPSLS
      • Gemini Falcon: Asteroid Miner
      • Gemini Falcon: All Boxed In >
        • Gemini Falcon >
          • Privacy Policy
          • A Game Oddity
      • Ay Caramba
      • Dots vs Dots
      • Ants vs Ants
    • Twine Stories
  • Random Thoughts
  • Book Reviews
  • Teaching
    • Real-Life Stories
    • Flying Text
  • About
    • Contact

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! 
​

freitags_battleship.jar
File Size: 15 kb
File Type: jar
Download File

Note: Requires Java on your PC to run.
Picture
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:
Picture
Picture
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. 
Picture
Picture
The AI continues like this and plays a pretty good game of Battleship. Give it a try! 
Powered by Create your own unique website with customizable templates.