Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Create an Interactive Console Player

00:00 Create an Interactive Console Player. At the end of this section, you’ll be able to play a tic-tac-toe match between a human and a computer player or two human players, in addition to the two computer player versions you’ve just seen. A human player will use the keyboard interface to specify their moves.

00:18 You can define a new concrete player class in your console front end, which will implement the abstract .get_move() method specified in the library.

00:28 Create the front end’s players module and fill it with the content seen on-screen.

01:04 You keep asking the player for a valid move until they provide one and make that move.

01:43 Otherwise, the game has finished and you return None to indicate that no moves were possible. Because the human player types cell coordinates such as A1 or C3, you must convert this text to a numeric index with the help of the grid_to_index() function.

02:16 The function uses regular expressions to extract the numeric row and column so that you can calculate the corresponding index in the flat sequence of cells.

02:28 If it doesn’t find an appropriate combination, it returns a ValueError. You can now modify your test script by importing and instantiating ConsolePlayer

03:06 Running this script will allow you to play as X against the computer. Unfortunately, there’s no convenient way of changing the players or stating who should start the game because this information is baked into the code.

03:28 Next up, you’ll add a command-line interface to fix that.

Become a Member to join the conversation.