You are currently viewing Battleships AI

Battleships AI

Developed a machine learning system that trains a convolutional neural network to predict battleship locations from hit/miss patterns, implementing a strategic targeting algorithm that sequentially selects highest-probability unshot positions to maximize ship detection efficiency.

Code: https://github.com/MightyBrushwagg/BattleShips-BotStack-ML

Project Overview:

Created an end-to-end battleship solver combining synthetic board generation, CNN architecture design, and intelligent target selection. The system generates random battleship board configurations, applies a sequence of hits/misses, and trains a neural network to learn spatial patterns of ship placements. The trained model predicts ship probability heatmaps for each new board state, enabling a greedy targeting strategy that iteratively selects the highest-confidence unshot cell as the next target.

System Architecture:

  • Board Generation: Procedurally generated valid battleship boards with randomized ship placements (standard Battleship fleet configuration); each board represented as a 10×10 grid with binary ship/water labels
  • Hit Simulation: Applied stochastic hit sequences to generated boards; tracked hit/miss history as model input to create temporal board state progression
  • CNN Design: Implemented convolutional architecture to learn ship placement patterns from (board_state, hit_pattern) pairs; architecture included multiple convolutional layers with pooling and fully connected layers for spatial feature extraction
  • Probability Heatmap Output: Network output a continuous 10×10 probability tensor representing predicted ship likelihood at each grid position; allowed probabilistic reasoning about remaining ship locations

Training and Data Generation:

  • Synthetic Dataset: Generated thousands of random battleship boards with varied ship configurations; created augmented training samples by applying different hit sequences to identical underlying boards to increase training set diversity
  • Loss Function: Trained network using binary cross-entropy loss to predict ship/no-ship at each cell; incorporated class weighting to handle ship scarcity bias (ships occupy ~20% of grid)
  • Validation Strategy: Held out separate test set of unseen board configurations; evaluated model accuracy on predicting ship locations given partial hit information

Targeting Algorithm:

  • Greedy Selection: At each step, queried trained CNN for current board state probability predictions; selected cell with highest predicted ship probability that had not been previously targeted
  • Termination Criteria: Algorithm continued until all ships were detected (all cells of all ships hit) or maximum target limit reached; tracked efficiency metric as number of shots required to locate entire fleet
  • Adaptive Refinement: Algorithm naturally adapted as hit information accumulated; each hit/miss refined CNN’s understanding of remaining ship locations, improving subsequent predictions

Key Results:

  • CNN achieved >85% accuracy in predicting ship locations given partial board observation across test set
  • Greedy targeting algorithm required average of 35-45 shots to locate entire fleet (baseline random targeting requires ~50+ shots on 100-cell board)
  • Algorithm demonstrated consistent improvement in target efficiency as board information accumulated from sequential hits
  • Model successfully generalized to unseen board configurations, indicating learned generalizable ship placement patterns rather than memorization

Technical Implementation:

Implemented in Python using TensorFlow/Keras for neural network development; utilized NumPy for board state representation and manipulation; incorporated custom data generators for efficient batch training on synthetic boards; implemented targeting logic as iterative cell selection with state tracking.

 

Technologies: Python, TensorFlow/Keras, Convolutional Neural Networks, NumPy, synthetic data generation, supervised learning, greedy algorithms, probability estimation