2025-09-09

Using Mini-Max Algorithm to Play Tetris 🎮

fe
#algorithm
#portfolio

Using Mini-Max Algorithm to Play Tetris 🎮

J.Gong

2025-09-09

3.08min

Using Mini-Max Algorithm to Play Tetris 🎮

So here’s a funny story - when my teacher first mentioned the “Mini-Max Algorithm,” I immediately thought of that AI company 😅. Had no clue it was actually a classic game theory algorithm!

But then he showed us this simple example: two players take turns moving the same piece either 1 or 2 steps forward. First one to reach the end wins. That’s when it clicked! 💡

You basically build a binary tree and find leaves that equal 0 or less. Count the branches - if it’s odd, player wins; if even, PC wins. Pretty neat, right?

This is actually super close to the real Mini-Max algorithm! The core idea is simple: calculate the worst-case scenario for your opponent while maximizing your own benefit.

Let’s think about it this way - at my turn, which move gives me the best advantage? 🤔

Let’s define our win conditions:

{1 win1 lose\begin{cases} 1 \text{ win} \\ -1 \text{ lose} \end{cases}

Mini-Max Decision Tree Structure 🌳

Here’s how the decision tree should look:

graph LR
  Root(("Root"))
  Root --> L(("Player 1"))
  Root --> R(("Player 2"))
  L --> LL(("AI 1"))
  L --> LR(("AI 2"))
  R --> RL(("AI 1"))
  R --> RR(("AI 2"))
  LL --> LLL(("..."))
  LR --> LRL(("..."))
  RL --> RLL(("..."))
  RR --> RLR(("..."))
  LLL --> LLLL(("1"))
  LRL --> LRLL(("-1"))
  RLL --> RLLL(("-1"))
  RLR --> RLRL(("1"))

Every time the AI moves, it calculates all possible outcomes. For example: if Player1 → AI1 leads to +1 but Player1 → AI2 leads to -1, then max("Player1 → AI1", "Player1 → AI2") is obviously Player1 → AI1. So the AI should take 1 step.

The cool part? The AI can also predict player moves and choose the branch where even if the player gets minimum benefit, the AI can still win! 🧠

Building a 1v1 Tetris Game 🎯

Now here’s where it gets interesting - let’s create a competitive Tetris game: Player VS AI!

Game start screen showing split view

Game Start: Player and AI split the screen - you’re on the bottom playing regular Tetris, AI gets the top half 🎮

Reward system in action

The Twist: When either side clears a row, they get rewarded with +1 row of space, while their opponent loses one row! Talk about pressure! 😤

The game continues until someone runs out of space to place pieces. It’s like Tetris meets territorial warfare! ⚔️

Scoring System - The Math Behind the Magic 🧮

To make Mini-Max work, we need a solid scoring system. Let me break down how we evaluate each possible move:

Tetris board analysis example

Tetris Board Evaluation Example: Here’s what happens when we drop this “Z” piece without rotation

Let’s look at our key metrics:

🏗️ Height: 28=4+0+0+5+5+4+3+2+1+428 = 4 + 0 + 0 + 5 + 5 + 4 + 3 + 2 + 1 + 4
Lower is better - we don’t want towering stacks!

🧹 Cleared Rows: 00
Higher is better - more cleared rows = more points

🕳️ Holes: 44
Lower is better - holes are the enemy of Tetris

📈 Gradient: 16=40+00+05+55+54+43+32+21+1416 = |4-0| + |0-0| + |0-5| + |5-5| + |5-4| + |4-3| + |3-2| + |2-1| + |1-4|
Lower is better - we want smooth, even surfaces

Finding the Perfect Coefficients 🎯

Now we need to weight these factors. Our scoring formula looks like this:

Score=(a,b,c,d)(HeightRowsHolesGradient)Score = (a, b, c, d) \begin{pmatrix} Height \\ Rows \\ Holes \\ Gradient \\ \end{pmatrix}

Instead of reinventing the wheel, I found that someone already solved this using Genetic Algorithms 🧬. Here are the magic numbers:

a=0.510066b=0.760666c=0.35663d=0.184483a = -0.510066 \\ b = 0.760666 \\ c = -0.35663 \\ d = -0.184483

Implementing Mini-Max in Tetris, First Try 🤖

Here’s where it gets tricky - Tetris pieces are random! So our decision tree height is usually just 1. But if we know the next few pieces in the sequence, we can go deeper.

Move Analysis for Each Piece:

Horizontal Movement (9 positions) 🔄Rotation Options (4 rotations) 🌀
Horizontal movement optionsRotation options

For each piece, we typically have around 36 possible moves (9 positions × 4 rotations). Consider if we show a sequence of pieces not only one, the calculation will be 36n36^n. Definately need pruning.

Another thing… this game currently only uses the Max part of the Mini-Max algorithm 🤯.
Yeah… I think I need to change my plan.

🎮 Change into Turns Game

Ohh, wait! I might have overcomplicated things 😅.
What if I make it a turn-based game? The player and the AI take turns.
Each turn, I’ll show two pieces:

  • one for the current player,
  • one for the opponent.

That way, the AI can actually use the full Mini-Max algorithm! 🧠✨

📜 Rules

So, what about the rules… 🤔

Classic Tetris scoring looks like this:

Row clearPoints
Single100
Double300
Triple500
Tetris800

There are also some high-level moves like T-Spin (when you rotate the T piece at the very last moment to clear rows).
I probably won’t implement it (too tricky to test 😅), but here are the points anyway:

Row clearPoints
Single800
Double1200
Triple1600

And don’t forget Combos 🔥 — every time you chain clears, you get +50 points extra.


👉 Wanna try it out? Play the game here: pixtris.vercel.app 🎉

unfinished

© 2025 All rights reserved..

This website uses Astro.build, Mantine and React Bits | deployed on Vercel