AI implementation
Puyo Puyo game
game development
artificial intelligence
puzzle games

How to implement AI for Puyo Puyo game?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

An effective Puyo Puyo AI is mostly a search and evaluation problem. The game is small enough that you can simulate candidate placements directly, but rich enough that a good agent must think beyond the immediate clear and evaluate chain potential, board safety, and sometimes the opponent's threat level.

Represent the board and moves clearly

A standard Puyo Puyo board is 6 columns wide, and pieces arrive as colored pairs. The AI needs a state representation that supports:

  • dropping a pair into any legal column and rotation
  • applying gravity
  • clearing groups of four or more connected colors
  • resolving chain reactions until the board stabilizes

A simple board representation is a 2D array where each cell is empty or stores a color ID.

python
1WIDTH = 6
2HEIGHT = 12
3
4EMPTY = 0
5
6def make_board():
7    return [[EMPTY for _ in range(WIDTH)] for _ in range(HEIGHT)]

The key implementation detail is not the array itself. It is having a deterministic simulator for "apply move, resolve clears, return next board and score."

For each falling pair, enumerate every legal placement:

  • vertical orientation with first puyo on top
  • vertical orientation reversed
  • horizontal left-right
  • horizontal right-left

Some placements are invalid near walls or when a column is full. The AI should generate only reachable placements under the actual game rules. Once you have that move list, the rest becomes a search problem.

python
1def legal_moves(board, pair):
2    moves = []
3    for column in range(WIDTH):
4        for rotation in range(4):
5            if is_legal(board, pair, column, rotation):
6                moves.append((column, rotation))
7    return moves

Even a shallow search becomes effective once move generation and simulation are reliable.

Simulate chains exactly

Puyo Puyo strategy revolves around chain reactions, so the simulator must repeatedly:

  1. find connected groups of size at least four
  2. remove them
  3. apply gravity
  4. count chain depth and score

That loop is what turns a placement into a meaningful evaluation. If chain resolution is inaccurate, the AI will learn the wrong board values and choose bad setups.

A minimal skeleton looks like this:

python
1def resolve_board(board):
2    total_score = 0
3    chain = 0
4
5    while True:
6        groups = find_clearable_groups(board)
7        if not groups:
8            break
9
10        clear_groups(board, groups)
11        apply_gravity(board)
12
13        chain += 1
14        total_score += score_groups(groups, chain)
15
16    return total_score, chain

Use heuristics before using deep learning

A strong hand-built AI can already be good. Start with an evaluation function that rewards:

  • immediate score from clears
  • future chain potential
  • board compactness
  • low column height
  • few isolated single puyos

and penalizes:

  • near-top danger
  • messy color fragmentation
  • moves that block future extensions

A simple evaluator might be:

python
1def evaluate(board, move_score, chain_count):
2    height_penalty = sum(column_height(board, c) for c in range(WIDTH))
3    potential = count_chain_starters(board)
4    return move_score + chain_count * 50 + potential * 10 - height_penalty * 3

This is not perfect, but it gives search something meaningful to optimize.

Add lookahead with beam search or minimax-style logic

For single-player planning, beam search or depth-limited search works well. For versus play, you may also model opponent pressure, garbage generation, and counterplay.

A practical starting point is:

  • simulate all current-piece placements
  • for each resulting board, simulate the next one or two incoming pairs
  • keep only the best few boards at each depth

That beam-search style often gives a better tradeoff than exhaustive search because branching grows quickly in Puyo Puyo.

Common Pitfalls

The biggest mistake is focusing on neural networks too early while the simulator is still inaccurate. If move generation, gravity, or chain resolution are wrong, the AI cannot become good regardless of the learning method.

Another mistake is rewarding only immediate clears. Strong Puyo Puyo play often requires building larger future chains rather than greedily taking a small score now.

Developers also underestimate board safety. An evaluator that ignores stack height can produce flashy but suicidal moves.

Finally, do not start with opponent modeling before your single-board evaluation is solid. A bad heuristic does not improve just because the search tree gets larger.

Summary

  • Puyo Puyo AI starts with reliable move generation and exact chain simulation.
  • A good state evaluator should balance immediate clears, future chain potential, and board safety.
  • Beam search or shallow lookahead is often a strong practical starting point.
  • Accurate simulation matters more than sophisticated learning early on.
  • Build the heuristic agent first, then add opponent modeling or machine learning if needed.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.