webdevpuneet.com

Checkers Game vs Computer with 12 Levels – HTML, CSS & JS

Checkers Game · Games · Plain HTML, CSS & JS · Live preview

CategoryGames

About this UI Snippet

Checkers vs Computer — Forced Jumps, Multi-Jumps, a Minimax AI with 12 Levels and a Daily Match

Screenshot of the Checkers Game snippet rendered live

Checkers (draughts) is a game of forced captures, and that rule is what makes it a good programming exercise. This version is a full game: you play red against a computer with twelve difficulty levels, or play a friend on the same screen. It enforces the real rules (forced jumps, multi-jumps that must be completed, kings, a draw rule), animates every move, gives hints, and has stars, a daily match and saved progress. It is plain HTML, CSS and JavaScript with no libraries, images or audio files.

The board as 64 numbers

The position is a flat array of 64 numbers: 0 for empty, 1 and 2 for a red man and king, and -1 and -2 for black. genMoves() returns every legal move as a complete sequence, such as { path: [41, 32, 23], caps: [36, 27] }. Because a whole multi-jump is one move, the interface, the AI and the move log all use the same objects. If any jump exists, only jumps are returned, which is the forced-capture rule. While generating a chain, a jumped piece is marked as a ghost so it cannot be jumped twice, and a man that reaches the far row becomes a king and its move ends there.

Multi-jumps that cannot be abandoned

Many simple checkers programs let you switch pieces halfway through a multi-jump. Here, nextSquares() looks at the squares chosen so far and shows only the squares that continue a legal sequence. Once you have started a chain, clicking another piece is refused with a message, and the move is applied only when a complete sequence has been chosen. Each jump is drawn as soon as you click it, and the captured piece shrinks away.

A minimax opponent with a dial for strength

The computer uses iterative deepening negamax with alpha-beta pruning. evaluate() scores material (a king is worth 1.7 men), how far each man has advanced, a guarded home row and control of the centre. At the edge of the search it keeps going while captures are pending, so it never stops in the middle of an exchange. Each level in the LEVELS array sets the search depth (1 to 9 moves), a time limit and a noise value. Noise adds a random amount to each candidate move's score, which makes the lower levels play like a distracted human instead of a perfect player with a shallow horizon. Level 12 searches nine plies, within a time cap that keeps the page responsive.

Hints, undo, stars and the daily match

The Hint button runs the same search for you at depth 6 and highlights the start and end squares. Undo takes back your last move together with the computer's reply. Winning gives one star, a second star if you used no hints or undos, and a third if you lost no more than the level's par number of pieces. A win unlocks the next level. The Daily Match plays four seeded random moves from the start position, so everyone faces the same opening each day, and winning on consecutive days builds a streak. There is also a pass-and-play mode.

Draws, feel and saving

A game with 40 moves each without a capture or a man move is a draw, which stops endless king shuffling. Moves slide across the board, captured pieces shrink away, kinging crowns a piece with a glow, and a short wooden click and chimes come from a Web Audio synth. Your last move is highlighted, pieces that must jump pulse, and a move list shows the game in notation such as d6xb4. Stars, your record and the streak are saved to localStorage inside try/catch, and the whole game scales to short preview areas.

What's included

Features

Full rules: forced captures, multi-jumps that must be finished with the same piece, kinging that ends a move, and a 40-move draw rule
Moves are generated as complete sequences, so the interface, the AI and the move log share one move format
Minimax with alpha-beta, iterative deepening, a time cap and capture extension at the search edge
12 computer levels, searching 1 to 9 plies, with a noise setting that makes the weaker levels play like imperfect humans
Hints from the same search at depth 6, free undo of your move together with the reply, and stars for clean wins
Daily Match from a seeded random opening with a day streak and a copy result button, plus a two-player pass-and-play mode
Animated sliding moves, shrinking captures, a crowned king with a glow, last-move highlight, pulsing forced pieces and a move list
Web Audio synth sounds for moves, captures, kings and results, with a mute button
Keyboard play with arrow keys, Enter, U for undo and H for hint, and progress saved to localStorage inside try/catch
Scales to its preview area (down to 40 percent), and on phones the side panel drops under the board

Step by step

How to Use

  1. 1
    Pick a level, the Daily Match or two playersThe menu shows 12 levels in four tiers, from the Cadet who plays almost randomly to the Grandmaster who searches nine moves ahead. Level 1 is open at first and each win unlocks the next. The gold card starts the daily match and the green card is a two-player game on one screen.
  2. 2
    Move your piecesYou play red, at the bottom, and move first. Click a piece and then a highlighted square. Men move diagonally forward, and a man that reaches the far row becomes a king (shown with a crown) that can move in all four diagonal directions.
  3. 3
    Jump when you canCaptures are forced: if a jump is available, you must take one, and the pieces that can jump pulse. Red-marked squares are jumps. After a jump, if the same piece can jump again, you have to keep going with that piece.
  4. 4
    Beat the computerCapture all its pieces or leave it with no legal move. The right panel shows how many pieces each side has left and the last moves in notation. If neither side captures or advances a man for 40 moves each, the game is a draw.
  5. 5
    Use hints and undo carefullyHint (H) highlights a strong move and you get three per game. Undo (U) takes back your last move and the computer reply. Both are useful for learning but they cost you the second star.
  6. 6
    Earn stars and keep the streakOne star for a win, one more for no hints or undos, one more for losing no more than the par number of pieces. Winning the Daily Match on consecutive days builds a streak, and Copy result gives a share line.

Real-world uses

Common Use Cases

Teaching minimax and alpha-beta pruning
The whole AI is under a hundred lines: a move generator, an evaluation function, negamax with pruning and iterative deepening. The noise and depth settings show how search depth and randomness change how strong a program feels.
Modelling forced moves and multi-step turns
Forced captures and multi-jumps are a good lesson in representing a turn as a sequence and in showing only the next legal squares while a sequence is in progress. The same idea fits Reversi, Chess captures or any multi-step action.
A classic game with progression for a games page
Twelve levels, stars and a daily match turn a classic into something players return to. Progress is saved in the browser with no account.
Animating a board with persistent elements
Pieces are persistent elements positioned in percentages and moved with CSS transitions, so slides, captures and kinging are animated without redrawing the board on every click.
Daily challenge from a seeded opening
Playing a few seeded random moves from the start position gives everyone the same daily game without storing any puzzles, and a streak counter gives a reason to come back.
Related: Battleship Game vs Computer
See the Battleship Game vs Computer for another turn-based game with a computer opponent whose skill rises with each level.
Related: Connect Four vs Computer
See the Connect Four vs Computer for a smaller game with a similar search-based opponent.

Got questions?

Frequently Asked Questions

chooseMove() runs an iterative deepening negamax search with alpha-beta pruning. It searches one ply, then two, and so on up to the level depth, and keeps the result of the last depth it finished before the time cap. Positions are scored by evaluate(): material (a king is worth 170 and a man 100), how far men have advanced, a guarded home row and the centre. On the weaker levels a random amount (the noise) is added to each move score, so it sometimes picks a worse move.

American checkers: 8x8 board, men move and capture diagonally forward, kings move in all four diagonal directions one square at a time, captures are mandatory, a multi-jump must be finished with the same piece, a man that reaches the far row becomes a king and its move ends there. If no capture or man move happens for 40 moves each, the game is a draw. There are no flying kings.

Because the rules do not allow it. A move is a whole sequence of jumps, and after you have started one, nextSquares() only shows squares that continue a legal sequence. A click elsewhere shows a reminder. The board is only changed when a complete sequence has been chosen.

Beating a level gives one star. You get a second star if you used no hints and no undos, and a third if you lost no more than the level par number of pieces (6 on the first levels, down to 2 on the last ones). A draw or a defeat gives no stars. A level unlocks when the previous one has at least one star.

Edit the LEVELS array: depth is how many plies the computer searches, noise is the random score added to its candidate moves, ms is its thinking time cap and par is the number of pieces you may lose for the third star. Tiers hold three levels each, so add levels in threes or edit TIERS. To change how it plays, tune the numbers in evaluate().

Yes. All localStorage access is in try/catch, so a blocked storage only means progress is not saved, and every sound call is wrapped. Squares respond to pointer events, so touch works, and on narrow screens the side panel moves under the board and the page scrolls. The computer thinks within a time cap, so the page stays responsive.

Copy & paste

Source Code

<div class="ck-fit" id="ck-fit"><div class="ck-wrap" id="ck-wrap">
  <div class="ck-panel" id="ck-panel">
    <div class="ck-hud">
      <div class="ck-hud-left">
        <span class="ck-kicker" id="ck-kicker">Checkers</span>
        <span class="ck-name" id="ck-name">Pick a level</span>
      </div>
      <div class="ck-turn" id="ck-turn"></div>
      <div class="ck-hud-btns">
        <button class="ck-btn-sm" id="ck-levels-btn" type="button" title="Levels (Esc)">Levels</button>
        <button class="ck-btn-sm" id="ck-mute" type="button" aria-pressed="false">Sound on</button>
      </div>
    </div>

    <div class="ck-main">
      <div class="ck-board-col">
        <div class="ck-frame">
          <div class="ck-axis-y" id="ck-axis-y"></div>
          <div class="ck-board" id="ck-board" role="grid" aria-label="Checkers board"></div>
          <div class="ck-axis-x" id="ck-axis-x"></div>
        </div>
      </div>
      <aside class="ck-side">
        <div class="ck-player" id="ck-p-black"><i class="ck-chip black"></i><div><b id="ck-black-name">Computer</b><span id="ck-black-sub"></span></div><em id="ck-black-count">12</em></div>
        <div class="ck-player" id="ck-p-red"><i class="ck-chip red"></i><div><b id="ck-red-name">You</b><span id="ck-red-sub"></span></div><em id="ck-red-count">12</em></div>
        <div class="ck-moves" id="ck-moves"></div>
        <div class="ck-tools">
          <button class="ck-tool" id="ck-undo" type="button"><span class="ck-tool-ico">&#8630;</span>Undo <em>U</em></button>
          <button class="ck-tool" id="ck-hint" type="button"><span class="ck-tool-ico">&#128161;</span>Hint <i id="ck-hint-count">3</i><em>H</em></button>
        </div>
        <p class="ck-msg" id="ck-msg"></p>
      </aside>
    </div>

    <div class="ck-overlay show" id="ck-menu">
      <p class="ck-logo">Checkers</p>
      <p class="ck-tagline">Capture every piece or block the computer. Jumps are forced, multi-jumps must be finished and a piece that reaches the far row becomes a king.</p>
      <div class="ck-menu-row">
        <button class="ck-daily" id="ck-daily" type="button">
          <span class="ck-daily-title" id="ck-daily-title">Daily Match</span>
          <span class="ck-daily-sub" id="ck-daily-sub">The same opening for everyone</span>
        </button>
        <button class="ck-daily ck-pvp" id="ck-pvp" type="button">
          <span class="ck-daily-title">Two players</span>
          <span class="ck-daily-sub">Pass and play on one screen</span>
        </button>
        <div class="ck-totals"><span><b id="ck-star-total">0</b> / 36 stars</span><span>Daily streak <b id="ck-streak">0</b></span><span id="ck-record"></span></div>
      </div>
      <div class="ck-tiers" id="ck-tiers"></div>
    </div>

    <div class="ck-overlay" id="ck-result">
      <p class="ck-result-title" id="ck-result-title">Victory!</p>
      <div class="ck-result-stars" id="ck-result-stars"><span>&#9733;</span><span>&#9733;</span><span>&#9733;</span></div>
      <p class="ck-result-sub" id="ck-result-sub"></p>
      <div class="ck-result-btns">
        <button class="ck-btn" id="ck-next" type="button">Next level</button>
        <button class="ck-btn ck-btn-ghost" id="ck-retry" type="button">Rematch</button>
        <button class="ck-btn ck-btn-ghost" id="ck-share" type="button">Copy result</button>
        <button class="ck-btn ck-btn-ghost" id="ck-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 genMoves() builds complete multi-jump sequences with ghost pieces, how negamax and alpha-beta pruning work together in search(), and why chooseMove() adds noise to the weaker levels. It is also a good base to extend: ask for a transposition table, an opening book, a "flying kings" international draughts variant, a 10x10 board, a stronger evaluation (mobility, tempo, trapped pieces), a move replay slider, or playing as black. You can also ask it to review how the busy flag and the run id protect against clicks and timers during the computer's move.

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 checkers (draughts) game against the computer in plain HTML, CSS and JavaScript, with no libraries, images or audio files.

Requirements:
- An 8x8 board drawn with CSS (green and cream squares, wooden frame, file and rank labels). Pieces are persistent elements positioned in percentages and moved with CSS transitions. The position is stored as a flat array of 64 numbers (0 empty, +1 and +2 red man and king, -1 and -2 black man and king). You play red at the bottom and move first.
- Rules: men move and capture diagonally forward, kings move and capture in all four diagonals, captures are mandatory, a multi-jump must be completed with the same piece (a jumped piece cannot be jumped twice), a man reaching the far row becomes a king and the move ends there. The game is lost when the side to move has no legal move or no pieces, and a draw after 40 moves each without a capture or man move.
- A move generator that returns every legal move as a complete sequence ({ path, caps, promote }), returning only jumps when a jump exists. In the interface, click a piece, show only the squares that continue a legal sequence, draw each jump as soon as it is clicked, refuse clicking other pieces mid-chain and apply the move when the sequence is complete. Highlight the last move and make pieces that must jump pulse.
- The computer: iterative deepening negamax with alpha-beta pruning, a time cap, an evaluation that scores material, advancement, home-row guard and the centre, and a capture extension at the search edge. 12 levels defined as data (depth 1 to 9, noise added to root move scores for weaker levels, a time cap, par pieces lost), in 4 tiers. Winning unlocks the next level.
- Stars: 1 for a win, +1 for no hints or undos, +1 for losing no more than the par number of pieces. Hints (3 per game) use the same search at depth 6 and highlight the start and end squares. Undo takes back your move and the reply.
- A Daily Match that plays four seeded random moves (mulberry32) from the start position, a day streak and a copy result button, plus a two-player pass-and-play mode. Save stars, the win/loss/draw record and the daily result to localStorage inside try/catch.
- Side panel with piece counts for both players, a move list in notation (for example d6xb4) and a turn indicator showing "Computer thinking...". Web Audio synth sounds (wood tocks, capture, king chime, win and lose) with a mute button, confetti on a win, and a result screen with stars.
- Lock input and guard timers with a busy flag and a run id while the computer thinks or a move animates. Keyboard play with arrow keys and Enter. Scale the whole game to fit the available area (down to 40 percent) and stack the layout on narrow screens.

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.