#76 Tic-Tac-Toe
Tic-tac-toe is a classic pencil-and-paper game played on a 3 × 3 grid. Players take turns placing their X or O marks, trying to get three in a row. Most games of tic-tac-toe end in a tie, but it is possible to outsmart your opponent if they’re not careful.
The Program in Action
When you run tictactoe.py, the output will look like this:
Welcome to Tic-Tac-Toe!
| | 1 2 3
-+-+-
| | 4 5 6
-+-+-
| | 7 8 9
What is X's move? (1-9)
> 1
X| | 1 2 3
-+-+-
| | 4 5 6
-+-+-
| | 7 8 9
What is O's move? (1-9)
--snip--
X|O|X 1 2 3
-+-+-
X|O|O 4 5 6
-+-+-
O|X|X 7 8 9
The game is a tie!
Thanks for playing!
How It Works
To represent tic-tac-toe ...
Get The Big Book of Small Python Projects now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.