Games · Guided
Build Tic-Tac-Toe in Python
A two-player Tic-Tac-Toe you can play in the page: turns, a win in any line, and a draw.
You write: The click handler that makes a move and looks for a winner, and the test for a full board. The drawing and the rules are given.
Loading the game…
What to build
Everything is here except two functions. state holds the whole game. play, winner_for, square_at and draw are written and tested. Write the last two, where the comment is.
board_is_full(board)returns True when no square is empty, and False otherwise.drawuses it.clicked(x, y)handles one click:- if the game already has a winner, do nothing at all;
- ask
square_atwhich position was clicked (it gives -1 for a click outside the board); - ask
playto make the move onstate["board"], and keep the turn it returns instate["turn"]; - ask
winner_forabout the board, and keep the answer instate["winner"]; - call
draw().
play already refuses a move that is not allowed, so clicked does not need to check the square itself. Then run the program and play a whole game.
Not sure where to start?
This game is part of “Build a Game”, a lesson of Build with Python. The lesson builds up to it, and the course before it starts from zero. Both are free.
Read the lessonSee the courseMore games
- Hit the Target: A first click game: every click lands a dot, green inside the target and red outside.
- Lights: A puzzle on a five-by-five grid: a click switches a light and its neighbours, and the goal is to turn every light off.
- Four in a Row: A game for two: counters drop down the columns of a board, and four in a line wins.
- Pairs: A memory game: sixteen face-down cards hide eight pairs, and a turn flips two.