Lab: Automating The Game

Entertain me, computer

Part I. The Problem

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:

  1. Choose who goes first
  2. Assign X and O
  3. Ask the next player which square
  4. Make sure it's a legal move
  5. Update the board with an X or O at that location
  6. Check if they won
  7. Check if the game is a tie

Data:

  • The board (9 squares that are X, O or blank)
  • Who is X and who is O
  • Whose turn is next

Data Representation

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

  • Nine variables, one for each space? A list? Multiple lists?
  • What values will you use? Numbers? Characters?

Discuss how the different choices impact:

  • How you will change the board
  • How will you show the board to the players
  • How you will tell if a move is valid or not
  • How you will tell when the game is over

Who is X and who is O, who goes first and whose turn it is

  • These are all connected. What variables and values could you use to keep them straight?

Discuss how the different choices impact:

  • How you will know who goes next
  • How you will know whether they are X or O

Part II. The Algorithm

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


 

Part III. Version 1

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.

  • Represent the board as a list with nine entries
  • Use 'X', 'O' and ' ' as the values in the list
  • Assume the first player is always X
  • Have the players specify their move with a single number 0-8, where 0 is the upper left corner, 2 is the upper right corner, all the way to 8 as the lower right corner

Once that is done, work on two increasingly harder extensions:

  1. Identify illegal moves
  2. When someone makes an illegal move, tell them they can't do that and have them try again

Save this version of the game and submit as part of your lab.

Part IV. Looking Forward

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.