AI Glossary / Graphs and symbolic AI / Beginner
Move generation
Definition
Move generation is the process of producing the candidate or legal actions available from a game position or search state. In game-playing AI, it supplies the branches that a search algorithm can examine. Correct generation must reflect the current state, the rules, and whose turn it is, rather than merely listing physically imaginable movements.
What it means
Before a game engine can decide which move is best, it needs to know what moves are available. Generating moves and evaluating them are separate tasks. A move can be legal but strategically poor, while an attractive-looking action can be illegal.
Some systems first generate pseudo-legal moves that satisfy basic movement rules, then filter those violating additional conditions. Chess provides a common example: a geometrically valid move may leave the moving side's king in check. Other implementations generate legal moves directly. The choice affects engineering, but the search must ultimately respect the intended game rules.
How it works
Start from a complete state representation, including information that may not be visible from piece locations alone. Apply the relevant movement and special-action rules, create candidate move descriptions, and check any remaining legality conditions. Applying a move produces a successor state for search.
Generation can be performed eagerly, producing a full list, or lazily, producing moves when requested. Engines may also generate selected classes, such as captures, during a particular search phase. Such a restricted generator is not a complete legal-move generator. Move ordering can change the order in which candidates are searched without changing which moves are legal.
Three practical examples
- A chess engine can supply all legal continuations for minimax, alpha-beta, or Monte Carlo tree search.
- A board-game interface can highlight valid destinations and reject a submitted action that is not allowed in the current position.
- A puzzle solver can enumerate permitted state transitions before comparing their likely progress toward a goal.
Think of it this way
Think of preparing the valid options on a menu before deciding which one to choose. The menu determines what can be considered, while the evaluation determines what is attractive. The analogy is limited because game moves change the state and future options, and eligibility may depend on hidden state details or the history required by the rules.
Worked example
Consider an invented tic-tac-toe position with squares numbered 1 through 9 from left to right, top to bottom. X occupies squares 1, 3, and 5. O occupies squares 2, 4, and 9. It is X's turn, and neither side currently has three in a row.
The empty squares are 6, 7, and 8, so a complete legal generator returns exactly three actions: place X at 6, at 7, or at 8. It must not include occupied square 2, and it must not omit square 7 because an ordering heuristic happens to consider it unlikely.
An evaluator can then notice that placing X at 7 completes the diagonal 3-5-7 and wins immediately. That strategic fact does not make the other two actions illegal. After the winning move, under the usual rule that play ends upon a win, the successor is terminal and no further ordinary move should be generated. Generation and terminal-state handling therefore need to agree.
Illustrative case study
In a fictional chess-learning app, developers discover that the AI sometimes proposes moving a pinned piece in a way that exposes its king. The position evaluator gives the resulting board a favorable score, but the proposed move should never have been treated as a legal option.
The team separates candidate generation from final legality checking and adds focused examples involving checks and special moves. It also verifies that undoing a searched move restores the full state. The case illustrates a rules-engine defect rather than weak strategic evaluation; it does not describe a specific released app or claim a measured increase in playing strength.
Common misconceptions and mistakes
Do not confuse generating an action with validating an externally supplied action. Both may use shared logic, but listing moves and checking one move answer different operational questions. Do not assume a move-application function automatically checks legality; software interfaces may require the caller to provide an already valid move.
Omitting a legal move can make search miss the best continuation, while adding an illegal move can make its result invalid. Repeatedly generating the same move wastes work and can distort counts. Finally, remember that history-dependent rules may require more state than the visible board, such as castling rights or an en-passant opportunity in chess.
Business use
For game and simulation products, move generation is foundational correctness infrastructure. A strong evaluator cannot compensate for an incorrect action set. The testing effort should cover ordinary positions, special rules, terminal states, and state restoration after exploring a branch.
Useful checks include comparing generated moves with an independent reference implementation and counting reachable positions at small depths for well-defined test positions. Keep completeness claims specific: a fast generator for captures only may be correct for its purpose while incomplete as a general move generator. Also document whether a public interface returns candidates, pseudo-legal moves, or fully legal actions.
People also ask
Does move generation choose the best move?
No. It provides options; evaluation and search decide which option to prefer.
Are pseudo-legal moves always safe to play?
No. They may violate conditions checked later, depending on the game's rules and the implementation's definition.
Can move order matter if the legal set stays the same?
Yes. It can strongly affect search efficiency and, under a fixed computation budget, which useful lines receive attention.
Check your understanding
In the worked tic-tac-toe position, should a complete legal generator return only square 7 because it wins immediately?
Answer: No. Squares 6 and 8 are legal too. Returning only the winning move would be a selective decision or pruning choice, not complete enumeration of the legal action set.
Related terms
- Branching factor
- Minimax
- Alpha-beta pruning
- Terminal states
- Monte Carlo tree search
- legal move
- pseudo-legal move
- successor function
- game state
- move ordering
- terminal state
Learn next
Write out the three successor boards in the worked example and mark which are terminal. Then define a capture-only or otherwise restricted generator for a different game and state exactly which completeness claim it can support. This exercise separates legal-action coverage from strategic selectivity.
More in Graphs and symbolic AI
- Most likely state - The most likely state is a state assigned the greatest probability by an agent's current probability distribution over possible states.
- Most probable explanation - A most probable explanation, or MPE, is a joint assignment to all unobserved variables that has the highest probability given the observed evidence in a...
- Motion blur - Motion blur is the smearing of image detail caused by movement during the time an image sensor collects light.
- MTD(f) - MTD(f) is a minimax search algorithm that repeatedly calls a memory-enhanced alpha-beta search with a narrow, zero-window test until lower and upper bounds...
- Multiattribute utility theory - Multiattribute utility theory is a framework for representing preferences over outcomes described by several attributes, such as delivery speed...
- Multibody planning - Multibody planning is planning for several physically separate bodies, such as a fleet of robots, whose actions must be coordinated to achieve an overall...
Sources
Sources: python-chess official documentation, legal and pseudo-legal move generation, state information, and move-application responsibilities: https://python-chess.readthedocs.io/en/stable/core.html . The tic-tac-toe board and fictional learning-app case are original teaching illustrations. Library-specific pseudo-legality details should not be assumed identical in every chess engine.
Previous: Motion vector Next: Move-to-front transform