#!/usr/bin/env python """ tic-tac-toe programming design as a course exercise on Oct 20 The user would see something like this: X | O | a | b | c ---+---+--- ---+---+--- | | d | e | f ---+---+--- ---+---+--- | | g | h | i Where would you like to put an X ? The user is first asked whether they want to play X or O. X moves first. Moves are input as the letters The program recognizes when someone has won and says so. The computer plays legal moves. """ board = [ [' ']*3, [' ']*3, [' ']*3 ] help_text = [ ' a | b | c', ' d | e | f', ' g | h | i', ] who = ['X', 'O', 0] # [ user, computer, next_to_move] def print_board(): bar = '---+---+---' for row in range(0,len(board)): print ' ' + ' | '.join(board[row]), print ' '*6 + help_text[row] if row == len(board)-1: break print bar + ' '*6 + bar def change_board(move): """ apply this move to the global board""" # not written yet def get_move(): """ return a move for either the user or computer""" # not yet def is_winner(): """ return true if someone has won. As a side effect, print a message saying so.""" print "We have a winner!" return True def pick_sides(): """ Decide who plays X and who plays O """ def play_game(): print " tic tac toe " while True: # outer loop over multiple game pick_sides() while True: print_board() change_board(get_move()) if is_winner(): break raw_input("Another game? (cntl-C to exit)") print "Good, here we go." print