Episode 132: Creating Tic-Tac-Toe With an AI Player & Shortcuts for Python Decorators
The Real Python Podcast
Nov 11, 2022 54m
How do you create a computer opponent for a simple game within Python? Would you also like to learn how to adapt the game to run in a web browser or graphical user interface (GUI)? This week on the show, Christopher Trudeau is here, bringing another batch of PyCoder’s Weekly articles and projects.
Episode Sponsor:
Christopher shares a recent Real Python step-by-step project for creating a tic-tac-toe game engine. He talks about how to build the game engine and adapt it for different front ends. The tutorial also shows how to implement an unbeatable computer player using the minimax algorithm.
We discuss an article about how to avoid repeating yourself when creating decorators with multiple parameters. We talk about how you can stop copying and pasting code several times by assigning the decorator to a new variable.
We share several other articles and projects from the Python community, including a news roundup, a deep dive into Python’s doctest, several Python command line tricks, type annotations via automated refactoring, a new way to draw boxes in the terminal, a collection of projects for beginners with source code, a minimalist PDF creation library, and a tool for sensible logging in Python.
Course Spotlight: Python Decorators 101
In this course on Python decorators, you’ll learn what they are and how to create and use them. Decorators provide a simple syntax for calling higher-order functions in Python. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.
Topics:
- 00:00:00 – Introduction
- 00:02:25 – Python 3.12.0 Alpha 1 Released
- 00:02:45 – PyCon US 2023 Call for Proposals
- 00:03:27 – Python’s doctest: Document and Test Your Code at Once
- 00:13:59 – Build a Tic-Tac-Toe Game Engine With an AI Player in Python
- 00:22:12 – Sponsor: InfluxDB
- 00:22:59 – Python Command Line Tricks
- 00:30:50 – Type Annotation via Automated Refactoring
- 00:34:29 – A New (?) Way of Drawing Boxes in the Terminal
- 00:37:10 – Decorator Shortcuts
- 00:39:36 – Video Course Spotlight
- 00:41:02 – 190 Python Projects With Source Code
- 00:47:58 – fpdf2: Minimalist PDF Creation Library
- 00:50:04 – Simple, Sane, and Sensible Logging in Python
- 00:53:05 – Thanks and goodbye
News:
Topics:
- Python’s doctest: Document and Test Your Code at Once – In this tutorial, you’ll learn how to add usage examples to your code’s documentation and docstrings and how to use these examples to test your code. To run your usage examples as automated tests, you’ll use Python’s doctest module from the standard library.
- Build a Tic-Tac-Toe Game Engine With an AI Player in Python – In this step-by-step tutorial, you’ll build a universal game engine in Python with tic-tac-toe rules and two computer players, including an unbeatable AI player using the minimax algorithm. You’ll also create a text-based graphical front end for your library and explore two alternative front ends.
- Python Command Line Tricks – Using
python -m
you can do all sorts of things from the command line, including starting a webserver, opening a browser, parsing JSON, compressing files, and much more. - Type Annotation via Automated Refactoring – Jimmy’s team at Carta decided they wanted to add type annotations to their large codebase, but doing so manually would’ve taken a very long time. This post shows you how they built automated refactoring tools to add type annotations to their code.
- A New (?) Way of Drawing Boxes in the Terminal – With clever use of some of the Unicode border characters, you can build a better box around your text, without any color bleeding.
- Decorator Shortcuts – “When using many decorators in code, there’s a shortcut you can use if you find yourself repeating them. They can be assigned to a variable just like any other Python expression.”
Discussion:
Projects:
- fpdf2: Minimalist PDF Creation Library
- Simple, Sane, and Sensible Logging in Python – Get started with logging in Python or deploy advanced, flexible loggers without the boilerplate code. Learn all about
log2d
, a third-party wrapper for the Python logging library.
Additional Links:
- Build Your Python Project Documentation With MkDocs – Real Python
- Episode #97: Improving Your Django and Python Developer Experience – The Real Python Podcast
- Python’s zipapp: Build Executable Zip Applications – Real Python
- Episode #80: Make Your Python App Interactive With a Text User Interface (TUI) – The Real Python Podcast
- Episode #20: Building PDFs in Python with ReportLab – The Real Python Podcast
- Links for Documentation FPDF and Other Ports
- PFython/log2d - GitHub