Skip to content
tmon.ch
Go back

Rediscovering the Connect Four AI I wrote at 12

connectfour.jar4,049 bytes2 classes, no source

I was scouring some old hard drives and found a folder called conenctfour. Typo included. Inside was a single connectfour.jar and a .bat file to start it. No source code.

I made this when I was 12. I remembered it had some kind of AI, so I wanted to take a look. I ran it, played a game, and lost in like 10 moves. lol.

Getting the code back

A jar is just a zip of compiled .class files. This one had two: Board.class and Main.class. The .java files were gone.

So I gave the jar to Opus 5.5 (medium effort) to decompile and reconstruct. Turns out 12-year-old me compiled with debug info on, so the jar still had every variable name and every line number. That’s enough to rebuild the source almost exactly. To check, it compiled the rebuilt source again and compared: the bytecode, line numbers and variable names all come out identical to the original jar.

The one thing compiled code doesn’t keep is comments. Whatever notes I left myself are gone forever. Judging by the rest of the code, there probably weren’t many.

The game

It runs in the terminal. You type a column, it drops your piece, then the AI thinks and drops its piece:

Du bist Spieler #1 (der Rote)!

--1---2---3---4---5---6---7--
|   |   |   |   |   |   |   |
-----------------------------
|   |   |   |   |   |   |   |
-----------------------------
|   |   |   |   |   |   |   |
-----------------------------
|   |   |   |   |   |   |   |
-----------------------------
|   |   |   | 2 |   |   |   |
-----------------------------
|   | 2 | 1 | 1 | 1 | 2 |   |
-----------------------------

Gib eine Spalte ein (1-7):

That’s a real game, three moves in. I went 4, 3, 5 for a cheap three in a row, and it blocked both ends.

The board is a plain int[7][6]: 0 is empty, 1 is you, 2 is the AI. Dropping a piece walks up a column from the bottom until it finds a 0. Checking for a win is four loops, one per direction: vertical, horizontal and the two diagonals.

That’s the whole game. And that’s the important part, because it’s also all the AI needs.

Monte Carlo: just play random games

I thought this was the coolest algorithm ever as a kid, and honestly I still do.

You don’t teach the AI any strategy. No “take the center”, no “block three in a row”. You only need the rules: which moves are legal, and who won. Then:

  1. Pick a column and drop your piece there.
  2. From there, play the rest of the game with completely random moves for both sides until someone wins or the board is full.
  3. Write down who won.
  4. Do that thousands of times for every column.
  5. Play the column that won the most.

A random game tells you almost nothing. Thousands of them do. If dropping in column 4 leads to a win in 60% of random games and column 1 only in 30%, column 4 is probably the better move, even though no single game was played well.

My version does this 15,000 times per column. With 7 columns that’s 105,000 random games before every single move. Here’s the actual code:

public void doMonteCarlo(int iterationen, int spieler) {
    double[] winsprospalte = new double[7];
    double[] lossesProSpalte = new double[7];
    double[] ratioprospalte = new double[7];

    int curspieler = -1;

    Board curBoard = new Board(null);

    for (int a = 0; a < iterationen; a++) {
        for (int i = 0; i < 7; i++) {
            for (int x = 0; x < board.length; x++) {
                for (int y = 0; y < board[x].length; y++) {
                    curBoard.board[x][y] = board[x][y];
                }
            }
            int c = 0;
            while (true) {
                c++;
                if (curBoard.listLeereSpalten().size() == 0) {
                    break;
                }

                if (c == 1) {
                    if (curBoard.isSpalteVoll(i)) {
                        winsprospalte[i] = 0;
                        lossesProSpalte[i] = 1;
                        break;
                    }
                    curBoard.dropStein(i, spieler);
                    curspieler = spieler ^ 3;
                }

                curBoard.dropStein(curBoard.listLeereSpalten().get(ThreadLocalRandom.current().nextInt(0, curBoard.listLeereSpalten().size())), curspieler);

                if (curBoard.hatSpielerGewonnen(spieler)) {
                    winsprospalte[i]++;
                    break;
                }
                if (curBoard.hatSpielerGewonnen(spieler ^ 3)) {
                    lossesProSpalte[i]++;
                    break;
                }

                curspieler ^= 3;
            }
        }
    }

    for (int j = 0; j < 7; j++) {
        ratioprospalte[j] = winsprospalte[j] / lossesProSpalte[j];
    }

    int besteWahl = 0;
    for (int i = 0; i < ratioprospalte.length; i++) {
        besteWahl = ratioprospalte[i] > ratioprospalte[besteWahl] ? i : besteWahl;
    }

    dropStein(besteWahl, spieler);
}

What I love about this is how little it needs. It’s the same idea for Tic-tac-toe, Othello, or any game where you can write “make a move” and “who won”. Implement the game, add one loop of random games, and you get an AI for free. A smarter version of this, Monte Carlo Tree Search, was what Go programs were built on for years.

Play it yourself

Here’s the same algorithm ported to JavaScript, quirks included. You’re red and go first. After each move you can see what the AI saw: the win/loss ratio for every column from its random games.

Your move. Pick a column.

Try turning it down to 100 games. It gets noticeably dumber, because a few random games just aren’t enough to tell a good column from a bad one. At 15,000 it beat me again.

I made Opus 5.5 play it

Since Opus 5.5 rebuilt the code anyway, I asked it to play a round against the AI on this page and give it its best shot. Same model, same medium effort setting, played on 11 November 2026.

It played red, went first, and started in the middle. That matters, because Connect Four is solved: if the first player starts in the middle and plays perfectly, they always win. So Opus started from a guaranteed win, against an AI that doesn’t know any strategy and just plays random games.

Here’s the game, move by move. Press play.

Opus 5.5 mediumvsmy AI (15,000)

It lost.

19 moves each. By the endgame, yellow had a threat in column 7, one row below red’s own threat in the same column. Whoever played the bottom of that column first would lose. Then the AI forced red to use up every other safe move, until the only one left was the bottom of column 7.

An AI that plays random games beat a model that knows Connect Four is solved. 12-year-old me is 2 for 2.

Roasting my code

Now that I can read it again, 12-year-old me deserves some feedback.

int zuegevommensch = -1; It’s declared at the top of main and never used. Not once.

randomFillBoard() fills the board with random moves. It’s never called. A leftover experiment, I guess, and nobody cleaned up.

Pick a language. Any language. The variable names are half German, half English, with no casing rules: winsprospalte next to lossesProSpalte, curspieler in one method and curSpieler in the next, dropStein and printBrett on a class called Board.

The first move lives inside the loop. Instead of dropping the AI’s piece once before the random game starts, there’s a counter c and an if (c == 1) that runs on every single step to check whether this is the first one.

listLeereSpalten() twice per random move. To pick a random column it builds a fresh list of free columns, then builds the exact same list again to get its size. That’s two new ArrayLists for every move of every random game, millions per turn.

It divides wins by losses. Not wins by games played, wins by losses. A column that never loses scores infinity. A column with zero wins and zero losses scores 0 / 0, which is NaN. It happens to work, because a column that never loses really is a good pick, but I’m pretty sure that wasn’t the plan.

Type 0 and you skip your turn. The input check is n > 7 || n < 0, so 0 gets through. Then it looks up column -1, crashes, catches its own crash with e.printStackTrace(), prints a stack trace at you, and gives the AI a free move.

It thinks you play randomly. In all 105,000 imagined games, both sides move at random, including you. So it rates every column as if you’ll never block it on purpose or set up a trap. That’s a huge blind spot, and it still beat me and Opus.

To be fair, some of it holds up. Switching players with spieler ^ 3 is neat: 1 XOR 3 is 2, and 2 XOR 3 is 1. And the win check is four clean loops.

What I got right back then

The code is messy, slow and full of leftovers. But the idea is exactly right, and it’s still the part I’m proudest of: you don’t need to know how to play a game well to write something that plays it well. You just need the rules and a lot of random games.


Share this post:

Previous Post
My balls were the wrong color in Firefox
Next Post
tinycnn: A Convolutional Neural Network From Scratch in Rust