Tic Tac Toe Lab Part 2

Make sure you've got the first tic tac toe lab complete before starting this part.

In the previous lab, we worked on the graphical side of tic tac toe. But in order for the computer to play a smart game of tic tac toe, we're going to need it to keep track internally of what's going on. We're going to call the work we do today the internal stuff (even though we'll do some very simple displays)

Make a copy of your file from the previous lab. You should add this code that copy.

In order to have to have the computer play tic tac toe, we're going to need to have it keep track of the moves that it makes, along with the human's moves. Since the computer can't remember anything unless it writes it down in a "box" we're going to need to keep track of the 9 spaces on the board in a box somehow, and in our case, the somehow will be a list of strings. We'll use the string "." to represent an empty space, the string "O" to represent a space with an O in it, and the string "X" to represent a space with an X in it.

For example:

['.', '.', '.', '.', '.', '.', '.', '.', '.']
 A blank tic tac toe board
['X', 'O', '.', '.', '.', '.', '.', '.', '.']
X in top left, O in top center
['X', 'O', '.', '.', 'X', '.', 'O', '.', '.']
X in top left and center, O in top middle and bottom left


Part 1:

Add the following code to the top of your file. We're making another global variable to represent the tic tac toe board. If this doesn't make sense, talk to me.


EMPTY = "."
internal_board = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]

Part 2:

Write a function called compute_internal_location that takes a row and column number, and figures out what the location number is in the board list and returns that answer. For example
# ---------------------- compute_internal_location -----------------------
# row and col both go between 0 and 2
# given a row and column number, this returns the
# location in our board list where that space is represented

def compute_location(row,col):
# you fill in the code

Part 3:

Now add this code into your program. It will add letters to your board, and allow you to erase the board. Make sure you understand how it works. Play with it for a little while.


# ---------------------- insert_internal_letter ---------------------------
# row and col both go between 0 and 2
# letter should be "X" or "O"
# this inserts the letter into the internal board in the appropriate space
def insert_internal_letter(row, col, letter):
spot = compute_internal_location(row,col)
internal_board[spot] = letter

# ---------------------- clear_internal_board ---------------------------
# resets the board back to all periods

def clear_internal_board():
for i in range(9):
internal_board[i] = EMPTY #remember EMPTY is a global variable that represents "."


Part 4

Write a function called get_internal_spot that returns whatever is in the internal board in space row, col (it should be an "X", an "O" or a ".")


# ---------------------- get_internal_spot -----------------------
# row and col both go between 0 and 2
# given a row and column number, this returns the
# contents of the given location
# on the board which should be "X", "O", or "."
#
def get_internal_spot(row,col):
 # your code goes here

Part 5

If your get_spot function works correctly, you should be able to add this code and print your board. Make sure you understand how it works.
# ----------------------- print_internal_row ---------------------------
# prints out one horizontal row of the board. Rows are numbered
# from 0 to 2
#
def print_internal_row(row_num):
for i in range(3):
item = get_internal_spot(row_num, i)
print item, " ",


# ----------------------- print_internal_board() --------------------------
# prints out the current state of the board in a pretty fashion
# should look like a tic tac toe board
#
def print_internal_board():
for i in range(3):
print_internal_row(i)
print

Part 6

Time to play (as in test). Use the functions to put x's and o's in the spaces and play tic tac toe. Clear out the board and try again. Make sure that all of this code works, and MORE IMPORTANTLY that you understand it!!! REALLY understand it.

Part 7

Now, we're going to hook up the code that we wrote for the graphics with this code. We're going to call the stuff you worked on in this lab the "internal tic tac toe board" and the stuff from the previous lab the "graphical tic tac toe board" Your job is to make the following functions work:

# ----------------------------- clear_graphics_board ------------------------------
# this function clears out the graphical ttt board and redraws the empty board
#
def clear_graphics_board():
# hint: you may want to draw a big empty square over everything to do a fake
# clearing of the board before redrawing the ttt lines

# --------------------------------- reset_game ----------------------------------
#
# This function uses clear_internal_board above to clear out the internal ttt board
# and clear_graphics_board to clear out the graphical ttt board. You should then
# print out the internal board on the screen

def reset_game():


# ------------------------------- play_x ---------------------------------------
#
# This function uses your draw_x graphics function and the insert_internal_letter
# function to put an x on the graphical and internal boards in the specified row
# and column. You should print the internal board on the screen after this
# move happens.

def play_x(row, col):


# ------------------------------- play_o ---------------------------------------
#
# This function uses your draw_o graphics function and the insert_internal_letter
# function to put an o on the graphical and internal boards in the specified row
# and column. You should print the internal board on the screen after this
# move happens.
def play_o(row, col)