Lesson goal: Chess Openings, Game Trees & Loops

Previous: The Knight's Tour and vector geometry | Home | Next: Introduction to Coding Music and ABC Notation

In computer science, chess is the quintessential study of Game Trees and combinatorial search spaces.

From the starting position, White has $20$ possible first moves ($16$ pawn moves and $4$ knight moves). Black also has $20$ responses, creating $400$ distinct positions after just one full turn.

After four moves each, there are over 84 billion possible game variations — leading to Claude Shannon's famous estimate that chess has a game-tree complexity of approximately $10^{120}$ possible games (the Shannon Number, which exceeds the number of atoms in the observable universe)!

To study chess, grandmasters categorize games into standardized openings. With CodeByMath, you can store game move sequences in Lua arrays and watch them play out automatically.
chess_board()
chess_speed(400)

chess_move("e2-e4")

chess_move("e7-e5")
Move the mouse over a dotted box for more information.

  • Move List Navigation: Click any move in the notation bar above the board to jump directly to that point in history!
  • Auto-Capture: When a piece moves to an occupied square, the captured piece is automatically removed from the board.

Now you try. Change the 6th move from "g8-f6" to "g7-g6" (pawn blocks the Queen). What happens when Black plays g6 instead?

Type your code here:


See your results here: