ABOUT

Battleship is a classic board game where two players try to sink each other's fleet of ships. Each player has a grid on which they secretly place their ships. Players take turns calling out grid coordinates to guess where the opponent's ships are located. It's a game of strategy and luck.

HOW TO PLAY

BOT'S COORDINATE GUESSER LOGIC

An intelligent coordinate guesser algorithm has been designed for the robot to predict coordinates that leverages on the previous HIT coordinates along with predicted direction of ship alignment (vertical or horizontal) where the ship was HIT but not WRECKED.

Data Structure of the HIT Stack:
{
  row: number;
  col: number;
  shipId: string;
  direction: "vertical" | "horizontal" | null;
}[]

The Algorithm is designed based on the following ideology:

  • First guess is random, if it is a HIT, then push it into HIT Stack with direction set to null.
  • If there is data on HIT Stack, the next guess is around the most recent hit, i.e. the top element of stack.
  • While dropping torpedo around valid coordinates of most recent HIT, if we miss the guess then we would swap predicted direction from vertical to horizontal or vice versa.
  • In case if there are no valid coordinates around the most recent HIT, there again are 2 cases:
    1. Case 1: If there are successive hits on my HIT Stack, then the next guess is along the direction of Successive hits, this way we reach the other end of the ship.
    2. Case 2: Else we just swap our predicted direction from horizontal to vertical or vertical to horizontal.
  • When a ship is wrecked completely, remove it's coordinates from the HIT Stack.
  • If a ship is wrecked and no more coordinates are present inside HIT Stack, just go with random guess.

You can find the code of the Coordinate guesser here.
Learn more about this project on github and find me on twitter.

FEEDBACK

CREDITS