Connect Four in Python: a six-chapter terminal game
Build Connect Four in the terminal: a 6×7 grid, gravity, turns, four-in-a-row, a draw, then a full game against a random opponent.
Getting started
Home Assistant
Buying
Build Connect Four in the terminal: a 6×7 grid, gravity, turns, four-in-a-row, a draw, then a full game against a random opponent.

The downloadable script is the same file as on the French page, so its messages stay in French.
Two players drop tokens down columns. The first to line up four wins. You will write that game in Python 3, in the terminal, with no Pygame and no extra library.
You need to be comfortable with lists, for and while loops, and if. The series has six chapters. Each one ends with a script you can run, and the next chapter starts from that script.
The messages printed by the downloadable files are in French (Grille, Colonne, Ton choix). The Python is the same whatever language you read this in. The screenshots on chapters 1 and 6 show that French output, so you can compare your screen with the file.
Allow about 20 to 40 minutes per chapter. At the end of chapter 6 you can play two humans, or one human against an opponent that picks a legal column at random. That opponent does not look ahead.
q, and a random opponent.Run python puissance4_chapitre_01.py. You should see a 6 by 7 board created, the bottom row [0, 0, 0, 1, 0, 0, 0], and False for a full board. If that line is different, the demo did not place the token in column index 3.
Was this page helpful?
Thank you!
What should we improve?
Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.
Cookies and analytics
We use Google Analytics 4 to understand how the site is used (page views, navigation). It is only switched on if you accept. Declining does not stop you using the site. More: cookie settings and privacy policy (in French).