Recipe card · First-year university project (SAE)

Puissance X

A generalized Connect Four in Java: you choose the grid size and the number of discs to align, then play against a friend, against the computer, or watch two bots face off. Built by a team of five.

Context
SAE · Computer Science BUT, first year
Duration
May → June 2025
Team
5 people
Technologies
Java, JavaFX
A Puissance X game in progress: 7-row, 8-column grid with yellow and red discs, the Logique bot's turn
A MaxScore vs. Logique game
Why this dish?

The need

Develop, in Java, an alignment game inspired by Connect Four but generalized: the grid size and the number of discs to align are chosen before each game. It had to be playable by two people, against the computer, or let two computers play each other.

The challenge: an AI that plays well whatever the grid, from 5 × 5 to 10 × 10, and whatever the number of discs to align.

Preparation

What we built

  1. Custom gamesGrids from 5 to 10 rows and columns, from 3 discs to align up to the grid's shorter side, and three modes: player vs. player, player vs. bot, bot vs. bot.
  2. Win detectionChecking alignments horizontally, vertically and on both diagonals, for any number of discs to align.
  3. The “Logique” botA priority-based strategy: take the center on the opening move, win whenever possible, attack the best position, extend its alignments, and otherwise play randomly while respecting gravity.
  4. The “MaxScore” botIt scores every possible move: immediate win, blocking an opponent's win, closeness to the center, length of the alignments created, and a penalty if the move hands the opponent a win on the next turn.
  5. The interfaceA JavaFX window with the grid, the yellow and red discs and the playable cells highlighted, built with boardifier, the board game framework (model, view, controller) used at the IUT.
Starting a game in the terminal (the prompts are in French)
Choisissez le mode de jeu :
0 : Joueur contre Joueur
1 : Joueur contre Bot
2 : Bot contre Bot
> 1
Choisissez l'algorithme de jeu du bot :
1 : MaxScore
2 : Logique
> 1
Quelle est le nombre de ligne de la grille ? (mini 5 et max 10)
> 7
Quelle est le nombre de colonne de la grille ? (mini 5 et max 10)
> 8
Combien de pions à aligner pour gagner ? (mini 3 et max 7)
> 4
Chef's tips

Technical challenges and solutions

  • Winning with X discs, not just 4

    The problem
    The grid and the number of discs to align change every game, so Connect Four's cases can't be hard-coded.

    My solution
    Checks parameterized by the grid size and the number of discs to align, in all four directions.

  • A bot that doesn't fall into the trap

    The problem
    A bot that only looks at its own move often hands the opponent a win on the next turn.

    My solution
    MaxScore simulates every possible move, then all of the opponent's replies, and heavily penalizes moves that open a win for them.

    PXMaxScore.java (simplified excerpt)
    for (Point coup : coups) {
        // 1. A winning move, or one that blocks an opponent's win: play it
        if (gagne(coup, color) || gagne(coup, adversaire)) return coup;
    
        // 2. Otherwise, score the move
        int score = (board.getNbCols()/2 - distanceCentre) * 100;   // the center is worth more
        for (int[] dir : directions) {
            score += Math.pow(10, compterAlignement(board, coup, dir, color));
        }
        // 3. Look ahead: does this move hand the opponent a win?
        for (Point reponse : coupsSuivants) {
            if (gagne(reponse, adversaire)) score -= 50000;
        }
    }
Tasting

The result

The game runs and the two bots can play each other. The screenshots below come from a bot vs. bot game (MaxScore vs. Logique) on a 7 × 8 grid, with 4 discs to align.

Next time

What I learned

Puissance X was one of my first team projects: splitting up the code, merging our changes with Git and agreeing on a shared architecture. Working with boardifier showed me why it matters to separate the model, the view and the controller. And designing bots taught me to turn a player's intuition into rules and scores a program can apply.

Ideas for improvement: entering the game settings in the window rather than in the terminal, and letting a bot play the first move when both players are computers.