gekro
GitHub LinkedIn

Hnefatafl

Play Hnefatafl, the old Scandinavian board game of a king and his guards against a ring of attackers, on a 9 by 9 or 7 by 7 board against the computer or a friend.

Attackers pieces left 16

Defenders and the king 8

Your move (Attackers)

Session You 0, Computer 0, Draws 0

Wins 0

Quiet moves 0 of 80

Attackers (rose triangles) move first and try to capture the king (the amber star). Defenders (sky discs) guard the king, who wins by reaching any of the four corners (the diamond squares). Every piece slides any distance along a row or column over empty squares, never jumping. Only the king may stop on the middle square (the throne, marked with a cross) or on a corner; any piece may slide over the empty throne. You capture an enemy piece by moving so that it sits in a straight line between the piece you moved and another piece of yours. A corner counts as your piece against everybody, the empty throne counts against attackers always and against defenders when it is empty, and the king helps the defenders. Moving between two enemy pieces is safe. On the 9 by 9 board the king is captured only when attackers stand on all four sides of it, or on the three open sides when it is beside the throne, and never on the edge. On the 7 by 7 board, away from the throne, two attackers on opposite sides capture it. A side that cannot move loses. The game is drawn after 80 moves in a row, by both sides together, with no capture. Click a piece, then a marked square. Arrow keys move around the board, Enter or Space selects or moves, Escape lets go of the piece.

How the computer plays

Easy picks any legal move at random, for either side. Medium searches up to 3 moves ahead with alpha-beta pruning, looking at no more than 30,000 positions and stopping after about 0.9 of a second. Hard searches up to 5 moves ahead, one move deeper at a time, and stops deepening after about 0.45 of a second or 60,000 positions. Both score a position from the attackers' side: pieces left (a defender counts more than an attacker), how far the king is from a corner, how many squares it can slide to, a clear line from the king to a corner (a large penalty for the attackers), attackers touching the king, and pieces next to the corners. The defenders play for the opposite score, and a defender to move with a clear line to a corner is counted as a win. It is a fixed search with no learning and no opening book, so a careful player can beat it. Undo takes back your last move and the reply, and a game that used Undo is not counted as a result. Undo is not available once a game is over.

As-is, no warranty. These apps are free under their listed license and run entirely in your browser. Use at your own risk — don't blame me if your PC catches fire, your dog runs away, or the math turns out wrong. Verify anything that actually matters. None of this is professional financial, medical, legal, or engineering advice.

© 2026 Rohit Burani · MIT · Built at gekro.com · View source ↗

Guide

How to play

Two armies face each other on a square board. The attackers (rose triangles) start at the middle of each edge. The defenders (sky discs) stand around their king (an amber star), who starts on the middle square, the throne, which is marked with a cross. The four corner squares, marked with a diamond, are the king’s way out. The attackers move first.

  • Moving: every piece slides any distance along a row or a column over empty squares. Nothing jumps.
  • Special squares: only the king may stop on the throne or on a corner. Any piece may slide across the empty throne.
  • Capturing: you capture an enemy piece by moving a piece so that the enemy piece sits in a straight line between the piece you moved and another piece of yours. A corner counts as your piece against everybody. The throne counts as an attacker’s enemy at all times, and as a defender’s enemy while it is empty. The king helps the defenders capture. Moving between two enemy pieces is safe, and only the side that moves captures. One move can capture several pieces.
  • Capturing the king: on the 9 by 9 board the attackers need to stand on all four sides of the king, or on the three open sides when the king is beside the throne. A king on the edge cannot be captured. On the 7 by 7 board, a king away from the throne is captured like any other piece, by two attackers on opposite sides; on or beside the throne it needs the same ring as on the large board.
  • Winning: the defenders win when the king reaches a corner. The attackers win by capturing the king. A side that cannot move loses. The game is drawn after 80 moves in a row, counting both sides together, with no capture. The page shows that count.

The page warns you when the king has a clear line to a corner. As the defender, that is the move that wins. As the attacker, you have one move to block the line or capture the king.

Controls:

  • Opponent: the computer, or two players on the same screen.
  • Difficulty: Easy, Medium or Hard. It applies when you play the computer, and the menu is switched off in two-player games.
  • You play: the attackers, who move first, or the defenders with the king. When you are the defenders the computer opens.
  • Board: the full 9 by 9 game or the small 7 by 7 game, which is quicker.
  • Undo: takes back your last move and the reply. Against a person it takes back one move. Once a game is over, undo is off.

Click one of your pieces (pieces that can move have a dashed outline) and then a square with a green dot. On the keyboard, arrow keys move around the board, Home and End jump to the ends of a row, Enter or Space selects a piece or moves it, and Escape puts the piece down. Every square is announced with its row, column, contents and whether it is the throne or a corner, and the status line says what each move did and what it captured. Pieces differ by shape, not just by colour: triangle, disc and star. The page address carries the opponent, difficulty, side and board size.

How it works

The engine keeps, for every square and each of the four directions, the list of squares met going outward. A piece’s moves are those lists cut off at the first piece, with the throne and corners skipped for everyone but the king. After a move it looks at the four squares around the landing square for enemy pieces that are now sandwiched, and it asks a separate question about the king.

The test suite pins the rules in several ways:

  • The openings: 16 attackers, 8 defenders and the king on 9 by 9, and 8, 4 and the king on 7 by 7, both unchanged by turning the board a quarter turn or flipping it. The opening has 72 moves for the attackers and 56 for the defenders on 9 by 9, and 40 and 24 on 7 by 7.
  • Each rule, by hand-built positions: sliding blocked by pieces, the throne and corners kept for the king, sandwiches in a row and a column, safe stepping between two enemies, capture against a corner, against the empty throne and not against the throne when the king stands on it, the armed king, three pieces taken in one move, each way of capturing the king on both boards, a king that steps into a ring being safe, the king reaching a corner, a side with no move, and the draw.
  • A second implementation: a separately written model, with coordinates and loops instead of lookup lists, lists every legal move, works out what each move captures and decides every ending. It agrees with the engine in 1,600 random positions on both sizes, including every capture of every legal move, and move by move through 200 random games, with the king, corners and throne rules holding all the way.
  • Illegal input: moves onto pieces, through pieces, diagonally, to corners or the throne, of the wrong side, and junk values are all refused without changing the position.

The computer comes in three levels and plays either side:

  • Easy picks any legal move at random.
  • Medium runs an alpha-beta search up to 3 moves ahead, looking at no more than 30,000 positions and stopping after about 0.9 of a second.
  • Hard searches up to 5 moves ahead, one move deeper at a time (iterative deepening), and stops deepening after about 0.45 of a second or 60,000 positions, whichever comes first, so a move never ties up the page for long even on a slow device. The deepest finished search decides the move.

Both searches score a position from the attackers’ side and the defenders play for the opposite score. The score counts pieces (a defender is worth more than twice an attacker), how far the king is from a corner, how many squares it can reach, a large penalty for each corner the king has a clear line to, a bonus for attackers touching the king and a small one for pieces on the squares next to the corners. A defender to move with a clear line to a corner is counted as a win at once. Captures and king runs are tried first, and moves that tie for best are chosen between at random.

The alpha-beta search is checked against a plain minimax with no pruning, which gives the same value at depths 0 to 2 on 9 by 9 and 0 to 3 on 7 by 7 in positions from random games. In the test suite, Hard defenders searching 3 moves ahead won at least 5 of 6 small-board games against Easy attackers, and Medium attackers lost at most 1 of 6 against Easy defenders. In the suite’s test positions, the defenders slide the king to a corner when the line is open, the attackers capture the king when they can, and when the king threatens a corner the attackers block it. The suite asserts that Hard answers every position it is asked about in under 5 seconds, a bound loose enough for a slow machine; the design limit is about 0.45 of a second. The computer waits about 0.45 seconds before moving so you can follow along, and moves at once if you have asked for reduced motion.

Privacy and storage

Nothing is uploaded and there is no account. Against the computer, your win count for each difficulty (for both sides and both boards added together) is saved in your browser’s local storage, as small whole numbers inside a small JSON object, under one key. Clearing site data removes it. The session tally lives only on the page and resets when you leave it. Two-player games are not saved.

Only finished games count. A game in which you used Undo is not counted against the computer, so the saved figures are not inflated.

Limits

  • Many rule sets exist for this family of games. These are the page’s own: corner escape, a king that needs four attackers on the large board, no rule that lets the attackers win by surrounding all the defenders, and a draw after 80 quiet moves in a row instead of a repetition rule.
  • Hard is a fixed search of at most 5 moves with no learning and no opening book, and the evaluation is a simple hand-made one. A careful player can beat it.
  • The small board plays quickly but is not balanced by any proof. I have not measured which side wins more often.
  • Two-player mode is for one screen only; there is no online play.
  • Difficulty and side are only used against the computer.
All board games →

Free to play, no ads, nothing uploaded. These games are original and are not affiliated with any other game or brand.