Battleship - Cartesian Coordinates

Author:
BHNmath

Introduction

Based on the classic Battleship board game, this applet allows you to practice using Cartesian coordinates while playing against a computerized opponent! The goal is to sink all of your opponent's ships before your fleet is destroyed.

Instructions

  1. Your fleet is shown on the left grid. Position your ships by dragging the yellow dots. Clicking/tapping on a ship's yellow dot will change its orientation (horizontal or vertical). All ships must be entirely visible on the grid and cannot overlap each other. The Randomize and Reset buttons may be used to randomly position your fleet or restore the initial ship positions.
  2. After you've positioned your fleet, press the START button and wait for the game to load (typically a few seconds). During this time, your opponent's fleet is being randomly positioned.
  3. Once the game has loaded, an input box will appear above the right grid. Enter the coordinates for your first shot and press the Enter key (or click/tap outside of the input box). Note that the point must be entered in the form (#,#) with no spaces. Don't forget the brackets!
  4. The right grid is used for keeping track of your shots. Your most recent shot is shown with a yellow cross and its status (hit, miss, etc.) is displayed above the right grid. Misses appear as white points and hits are displayed as red points.
  5. Immediately after firing your shot, your opponent (computer) will fire a shot. The results of your opponent's shots are shown on the left grid. The most recent opponent shot is shown with a yellow cross and its status (hit, miss, etc.) is displayed above the left grid.
  6. Continue to fire shots with the goal of sinking your opponent's entire fleet. If you sink all of your opponent's ships before all of your ships are sunk, you win!

A Little Bit About the Opponent Algorithm

Although the computerized opponent never "looks up" the position of your ships, it does use an algorithm to seek and destroy your fleet. Knowledge of this algorithm can help you determine how to best position your fleet! Here's how it works:
  1. Initially, the opponent makes random shots from a list of all the open (unused) points on the grid. Each time a shot is made, that point is removed from the list.
  2. When a ship is hit, a list of all other possible hit points (horizontal and vertical) for that ship is created based on the ship's size and previously fired surrounding shots. The opponent now switches from search mode to target mode for that ship.
  3. The opponent begins shooting using points from the created target list, working randomly outward from the initial hit point.
  4. The target list is updated after each shot. Once the ship has been hit a second time, the orientation (horizontal or vertical) of the ship is known and all points from the opposite orientation are removed from the target list. Furthermore, if at any time the number of available target points in the horizontal or vertical direction is insufficient to sink the ship, points from that orientation are removed from the target list.
  5. If neighbouring ships are hit while a ship is being targeted, target mode is also activated for those ships. When multiple ships are being targeted simultaneously, the target algorithms are executed in alphabetical order by ship name.
  6. When a ship is sunk, target mode is deactivated for that ship. When no ships are being targeted, the opponent returns to the random search mode.
  • NOTE: While in search mode, the opponent algorithm does not consider the number of open neighbouring points. For example, if an open point is surrounded by misses/hits on all for sides, that point is not excluded from the list of available shots, even though there is no way it could be a hit. As a result, the player may have a slight advantage, especially in the final stages of a game!