webdevpuneet.com

Battleship Game vs Computer – Free HTML, CSS & JS Snippet

Battleship Game vs Computer · Games · Plain HTML, CSS & JS · Live preview

CategoryGames

About this UI Snippet

Battleship vs Computer — Fleet Placement, Probability AI, 12 Levels, Sonar and a Daily Fleet

Screenshot of the Battleship Game vs Computer snippet rendered live

Battleship is a game of hidden information: two fleets on two grids, and each player only sees the shots they have fired. This version is a full game against a computer captain, not just a ship-finding puzzle. You place your own fleet, take turns firing with an opponent that really hunts you, and can use special weapons. It has 12 levels, a Daily Fleet with a streak, stars, saved progress and synthesized sound, in plain HTML, CSS and JavaScript with no libraries, images or audio files.

Two fleets, one model

Each side is a small object made by makeSide(): a grid of ship indexes (occ), a grid of shots fired at it (fired: 0 untouched, 1 miss, 2 hit) and a list of ships with their cells, hits and sunk flag. Your side and the enemy side use exactly the same functions, so resolve() can fire at either one, and the win test is the same allSunk() call. The boards themselves are plain CSS grids with an animated water background, and every ship is one absolutely positioned hull element sized in percentages of the board, so the same code draws an 8x8 and a 10x10 grid.

Placing your fleet

Your fleet starts randomly placed, and you can rearrange it: choose a ship in the dock, hover the grid to see a ghost hull that turns red if it would overlap another ship, and click to place it. R or a right click rotates, clicking a placed ship picks it up again, and Randomize and Clear are one click away. Ships are clamped so they always stay on the board. The enemy fleet is placed with the same randomFleet(), and for the Daily Fleet it uses a seeded random generator (mulberry32), so every player gets the same enemy layout on the same day.

An enemy captain with four levels of skill

aiPick(skill) is the opponent. Skill 0 fires at random and follows up a hit. Skill 1 hunts on a checkerboard pattern (every ship is at least two cells long, so half the cells are enough to find it) and extends lines of hits. Skills 2 and 3 use a probability map: for every ship still afloat, the AI counts every position and direction where it could still fit given the misses and the sunk ships, adds that weight to each open cell, and gives a big bonus to placements that cover a known hit. Skill 2 picks randomly among the three best cells, skill 3 always takes the best one. The AI only uses information a real player would have: hits, misses and which ships have been sunk.

Special weapons that cost a turn

Sonar pings a cell and shows how many hidden ship parts are within one square, without firing. The cross strike fires at a cell and its four neighbours in one turn. Both are limited per level, and the later levels give you fewer of them, down to none on the final level. Hovering previews exactly which cells a weapon will touch.

Levels, stars and the daily fleet

The LEVELS array holds 12 levels in four tiers (Recruit, Officer, Captain, Admiral) with the grid size, the enemy skill, the number of sonar pings and strikes, and two par values. Three stars for sinking the enemy fleet within the first par, two within the second, one for any win. Winning a level unlocks the next. Stars, best shot counts and the Daily Fleet streak are saved to localStorage inside try/catch, so the game also runs in a sandboxed iframe.

Feel

Shells splash with a ripple, hits explode into flames and turn into smoke when the ship sinks, your panel shakes when the enemy hits you, and a battle log tells you what just happened. Explosions and splashes are generated as filtered noise with the Web Audio API. The whole game scales to the space it is given, and on phones the boards stack and the page scrolls.

What's included

Features

Full two-fleet game against a computer captain: place your own ships, take turns, win or lose
Placement with a ghost hull preview, rotation by R or right click, pick-up, randomize and clear
Four enemy skill levels: random with follow-up, checkerboard hunting with line extension, and two probability-map AIs
The AI uses only fair information: its own shots, hits, misses and which ships have sunk
12 levels in four tiers on an 8x8 or 10x10 grid, defined as data with sonar, strike and par values
Sonar (counts hidden ship parts nearby) and cross strike (five shells in one turn) with hover previews
Daily Fleet from a seeded generator, a day streak and a copy result button
Stars, best shots and unlocks saved to localStorage inside try/catch
Ship hulls drawn with CSS clip-path in percentages, flames, ripples, smoke on sunk ships, screen shake and a battle log
Web Audio noise and tone synth for cannons, splashes, hits, sunk ships and sonar, with a mute button
Keyboard play with arrow keys, Enter, F, S, X and R, and a layout that scales to its space or stacks on phones

Step by step

How to Use

  1. 1
    Pick a level or the Daily FleetThe menu shows 12 levels in four tiers with your stars. Level 1 is open at first and each win unlocks the next level. The gold Daily Fleet card starts the enemy layout of the day, which is the same for every player.
  2. 2
    Place your fleetYour ships start randomly placed. Click a ship in the dock and then your grid to move it, press R or right-click to rotate, or click a placed ship to pick it up. Randomize and Clear are in the toolbar. Press Start battle (or Enter) when you are happy.
  3. 3
    Fire at the enemy watersClick a cell on the right grid (or move with the arrow keys and press Enter). A red flame is a hit, a ring is a miss. When all the cells of a ship are hit it sinks and its hull appears on the map.
  4. 4
    Use sonar and cross strikesPress S to ping with sonar: it shows how many hidden ship parts are within one square of a cell and costs your turn. Press X for a cross strike that fires at a cell and its four neighbours. Press F to go back to the cannon. Each level gives you a limited number.
  5. 5
    Beat the enemy captainAfter each of your turns the enemy fires at your fleet. Higher levels use smarter captains that hunt on a pattern and calculate where your ships can still be. Sink all their ships before they sink yours.
  6. 6
    Earn stars and keep your streakSink the enemy fleet in few shots for 3 stars, the par is shown on the result screen. Clearing the Daily Fleet on consecutive days builds a streak, and Copy result puts a share line on your clipboard.

Real-world uses

Common Use Cases

Teaching game AI with hidden information
The four skill levels show a full progression: random, pattern-based hunting, and a probability map that counts every possible placement. The probability map is a compact example of how a program can reason about what it cannot see.
A classic game for a portfolio or a games page
A complete Battleship with levels, a daily challenge and sound shows state management, rendering, AI and polish in one self-contained snippet.
Reference for a two-sided board model
Both fleets share one data model and the same fire and win functions. That pattern carries over to Connect Four, Checkers or any game where two players act on two views of a board.
Drawing game pieces in pure CSS
The ship hulls use clip-path, gradients and drop shadows sized in percentages of the board, so they stay sharp and aligned on any grid size without images.
Daily challenge and streak pattern
The date-seeded Daily Fleet and the streak logic are reusable for any daily puzzle, and a seeded generator is the simplest way to give everyone the same challenge.
Related: Checkers Game
See the Checkers Game for another turn-based game against the computer.
Related: Lights Out Puzzle Game
See the Lights Out Puzzle Game for a related games pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

It depends on the level. The easiest captain fires at random and follows up a hit. The next one hunts on a checkerboard pattern and extends lines of hits. The two strongest use a probability map: aiPick() counts every position where each surviving ship could still fit, given the misses and the sunk ships, adds a big bonus for placements that cover a known hit, and fires at the cell with the highest total. It never looks at your real ship positions.

No. It reads only your fired grid (hits and misses) and which of your ships have been sunk, exactly the information a human opponent gets. The strongest captain is simply good at statistics.

Winning gives one star. Sinking the enemy fleet within the second par value gives two, and within the first par value gives three. The par values are in the LEVELS array and are shown on the result screen. Losing gives no stars, and a win unlocks the next level.

Add an object to the LEVELS array with a name, grid (small for 8x8, classic for 10x10), ai (0 to 3), the number of sonar pings and strikes, and the two par values. The menu draws three levels per tier, so add levels in groups of three or extend the TIERS array. A new grid size is one more entry in GRIDS with a size and a fleet list.

todayNumber() counts days since a fixed start date, and the enemy fleet is placed with randomFleet() using a mulberry32 generator seeded from that number. The same seed gives the same layout, so every player faces the same enemy fleet that day. Clearing it on the next day adds one to your streak, and a missed day resets it.

Yes. All localStorage reads and writes are in try/catch, so a blocked storage only means progress is not saved. The AudioContext is created when you start a level and every sound call is wrapped. On wide areas the game scales down to fit, and on narrow ones the boards stack and the page scrolls. Cells respond to pointer events, so it is playable by touch.

Copy & paste

Source Code

<div class="bs-fit" id="bs-fit"><div class="bs-wrap" id="bs-wrap">
  <div class="bs-panel" id="bs-panel">
    <div class="bs-hud">
      <div class="bs-hud-left">
        <span class="bs-kicker" id="bs-kicker">Battleship</span>
        <span class="bs-name" id="bs-name">Pick a level</span>
      </div>
      <div class="bs-turn" id="bs-turn"></div>
      <div class="bs-stat"><span class="bs-label">Shots</span><b id="bs-shots">0</b></div>
      <div class="bs-stat"><span class="bs-label">Accuracy</span><b id="bs-acc">0%</b></div>
      <div class="bs-hud-btns">
        <button class="bs-btn-sm" id="bs-levels-btn" type="button" title="Levels (Esc)">Levels</button>
        <button class="bs-btn-sm" id="bs-mute" type="button" aria-pressed="false">Sound on</button>
      </div>
    </div>

    <div class="bs-boards">
      <section class="bs-side" id="bs-side-me">
        <header class="bs-side-head"><h3>Your fleet</h3><div class="bs-fleet" id="bs-fleet-me"></div></header>
        <div class="bs-gridwrap" id="bs-wrap-me"></div>
      </section>
      <section class="bs-side" id="bs-side-en">
        <header class="bs-side-head"><h3>Enemy waters</h3><div class="bs-fleet" id="bs-fleet-en"></div></header>
        <div class="bs-gridwrap" id="bs-wrap-en"></div>
      </section>
    </div>

    <div class="bs-controls" id="bs-place">
      <div class="bs-dock" id="bs-dock"></div>
      <div class="bs-ctl-btns">
        <button class="bs-btn-sm" id="bs-rotate" type="button">Rotate <em>R</em></button>
        <button class="bs-btn-sm" id="bs-random" type="button">Randomize</button>
        <button class="bs-btn-sm" id="bs-clear" type="button">Clear</button>
        <button class="bs-btn" id="bs-start" type="button">Start battle <em>Enter</em></button>
      </div>
    </div>
    <div class="bs-controls" id="bs-weapons">
      <button class="bs-weapon" id="bs-w-shot" type="button"><b>Cannon</b><span>1 shell <em>F</em></span></button>
      <button class="bs-weapon" id="bs-w-sonar" type="button"><b>Sonar</b><span>count hidden ship parts nearby <i id="bs-sonar-count">0</i> <em>S</em></span></button>
      <button class="bs-weapon" id="bs-w-strike" type="button"><b>Cross strike</b><span>centre + 4 neighbours <i id="bs-strike-count">0</i> <em>X</em></span></button>
    </div>
    <div class="bs-log" id="bs-log"></div>

    <div class="bs-overlay show" id="bs-menu">
      <p class="bs-logo">Battleship</p>
      <p class="bs-tagline">Place your fleet, hunt the enemy fleet and sink every ship before they sink yours.</p>
      <div class="bs-menu-row">
        <button class="bs-daily" id="bs-daily" type="button">
          <span class="bs-daily-title" id="bs-daily-title">Daily Fleet</span>
          <span class="bs-daily-sub" id="bs-daily-sub">The same enemy fleet for everyone</span>
        </button>
        <div class="bs-totals"><span><b id="bs-star-total">0</b> / 36 stars</span><span>Daily streak <b id="bs-streak">0</b></span></div>
      </div>
      <div class="bs-tiers" id="bs-tiers"></div>
    </div>

    <div class="bs-overlay" id="bs-result">
      <p class="bs-result-title" id="bs-result-title">Victory!</p>
      <div class="bs-result-stars" id="bs-result-stars"><span>&#9733;</span><span>&#9733;</span><span>&#9733;</span></div>
      <p class="bs-result-sub" id="bs-result-sub"></p>
      <div class="bs-result-btns">
        <button class="bs-btn" id="bs-next" type="button">Next level</button>
        <button class="bs-btn bs-btn-ghost" id="bs-retry" type="button">Rematch</button>
        <button class="bs-btn bs-btn-ghost" id="bs-share" type="button">Copy result</button>
        <button class="bs-btn bs-btn-ghost" id="bs-menu-back" type="button">Levels</button>
      </div>
    </div>
  </div>
</div></div>

Extend with AI

Take This Snippet Further With AI

Paste this snippet's HTML, CSS and JS into an AI coding assistant like Claude and ask it to explain how aiPick() builds its probability map, why a checkerboard pattern is enough to find every ship, and how the hover preview and the sonar count share the same weaponCells() helper. It is also a good base to extend: ask for a second player on the same screen, ships that can move one square, a "mine" cell the AI must avoid, a salvo mode where you fire one shell per surviving ship, or an online daily leaderboard. You can also ask it to review the turn handling and the runId timer guard for edge cases when you open the menu mid-battle.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a Battleship game against the computer in plain HTML, CSS and JavaScript, with no libraries, images or audio files.

Requirements:
- Two boards side by side ("Your fleet" and "Enemy waters"), drawn as CSS grids with an animated water background and row and column labels. Ships are single absolutely positioned hull elements sized in percentages of the board and drawn with CSS clip-path and gradients. Both fleets use the same data model: an occupancy grid, a grid of shots fired at that side (untouched, miss, hit) and a list of ships with cells, hits and a sunk flag.
- Fleet placement: your fleet starts random, and you can select a ship in a dock, see a ghost hull that turns red when it would overlap, click to place, rotate with R or right-click, click a placed ship to pick it up, and use Randomize, Clear and Start battle.
- Turn-based battle: you fire first by clicking a cell (or arrow keys and Enter). A hit shows flames, a miss a ripple, and a sunk ship reveals its hull on the enemy board. The enemy fires after a short delay. Show a turn indicator, shots and accuracy, per-ship health pips for both fleets and a log of the last three events. Shake the panel when you are hit.
- An enemy captain with four skill levels: random with follow-up shots, checkerboard hunting with line extension, and two probability-map AIs that count every placement where each surviving ship could still fit (given misses and sunk ships) and weight placements covering known hits. The AI must only use information a real player has.
- Two limited special weapons that each cost a turn: sonar (shows how many hidden ship parts are within one square of a cell, without firing) and cross strike (fires at a cell and its four neighbours). Hovering shows which cells the weapon touches.
- 12 levels in four tiers (8x8 grid with 4 ships for the first three levels, then 10x10 with 5 ships) defined as data: enemy skill, sonar and strike counts and two par shot counts. Stars: 3 within the first par, 2 within the second, 1 for any win. A win unlocks the next level.
- A Daily Fleet: the enemy layout comes from a seeded random generator (mulberry32) based on the date, with a day streak and a copy result button. Save stars, best shot counts, the daily result and the streak to localStorage inside try/catch.
- Web Audio synth sounds (filtered noise for cannons, splashes and explosions, tones for sonar and results) with a mute button created on the first level start. Confetti on a win, a result screen with stars, and bump a run id on every new game or menu visit so old timers do nothing.
- Scale the whole game to fit the available area (shrink with transform down to 40 percent), and on narrow screens stack the boards and let the page scroll.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.