Entertain me, computer
Today, you're going to write a simple, command line version of Tic-Tac-Toe. We'll be creating and testing increasingly feature-complete versions of the game, and be learning some new language constructs along the way.
We already analyzed the algorithm and data for this game in class. Here's a brief summary of what you came up with:
Data:
With a partner, discuss how you are going to represent the necessary pieces of data as variables that the computer can work with. Type up your analysis below (one writeup per pair) and include that with your lab submission. Not more than a page.
The Board
Discuss how the different choices impact:
Who is X and who is O, who goes first and whose turn it is
Discuss how the different choices impact:
Open up a new code file and complete the algorithm below using comments. Make sure all the steps needed to play the game are listed. Save the comments-only file and submit that as part of the lab.
# declare and initialize variables for the board and player information # establish who is X and O and who goes first # loop for the number of possible turns for turn in range(9): # (everything in this block is a single turn) # indicate whose turn it is, and ask for their move # ...you finish the rest # the game has ended, finish anything else here
Rather than try to do the whole game at once, we're going to start with a simpler version. Each comment you put in the code file is one problem to be solved, as part of a bigger problem. So we'll focus on a subset of those problems first.
In this case, we're going with the minimum pieces to have an interactive game. That means allowing the players to take turns putting X and O on the board. Don't worry about legal moves or winning or losing.
If you are still unsure of your data representation, I recommend going with the following choices. They are not the best, but they are straightforward and can work.
'X'
, 'O'
and ' '
as the values in the listOnce that is done, work on two increasingly harder extensions:
Save this version of the game and submit as part of your lab.
During class next week, we'll be looking at several alternative ways to write this game, using additional language features. We'll also be breaking down the more complex problem of how to tell when someone wins or loses. If you want to get a jump on that, I encourage you to finish the game, but it is not part of this lab.