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.
What we built
- 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.
- Win detectionChecking alignments horizontally, vertically and on both diagonals, for any number of discs to align.
- 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.
- 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.
- 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.
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
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; } }
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.


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.
