import random

a1 = [ " " ]

a2 = [ " " ]

a3 = [ " " ]

b1 = [ " " ]

b2 = [ " " ]

b3 = [ " " ]

c1 = [ " " ]

c2 = [ " " ]

c3 = [ " " ]

def help_menu ( ) :

print ( "=============================================================" )

print ( "This is a tic-tac-toe game for two players. The board is" )

print ( "labelled in a Cartesian coordinate system; rows are labelled" )

print ( "a, b, and c, while columns are labelled 1, 2, and 3. Below is" )

print ( "a map of the tic-tac-toe board with coordinates labelled." )

print ( " | |" )

print ( " a1 | a2 | a3" )

print ( "-----+----+-----" )

print ( " b1 | b2 | b3" )

print ( "-----+----+-----" )

print ( " c1 | c2 | c3" )

print ( " | |" )

print ( "=============================================================" )

print ( "X always goes first. When it is your turn, type the" )

print ( "coordinates of the space you wish to place a piece at and" )

print ( "press \" enter \" ." )

print ( "=============================================================" )

print ( "Key commands - press one of these and then press enter" )

print ( "1 - pulls up this Help menu" )

print ( "2 - prints the current game map" )

print ( "3 - skip your turn" )

print ( "=============================================================" )

def print_map ( ) :

print ( " 1 2 3" )

print ( " | |" )

if a1 and a2 and a3:

for x in a1:

for y in a2:

for z in a3:

print ( "a " + x + " | " + y + " | " + z )

print ( " ---+---+---" )

if b1 and b2 and b3:

for x in b1:

for y in b2:

for z in b3:

print ( "b " + x + " | " + y + " | " + z )

print ( " ---+---+---" )

if c1 and c2 and c3:

for x in c1:

for y in c2:

for z in c3:

print ( "c " + x + " | " + y + " | " + z )

print ( " | |" )

def start ( ) :

help_menu ( )

print ( "X goes first." )

print_map ( )

x_turn ( )

input ( )

def check_draw_x ( ) :

if " " not in a1 and " " not in a2 and " " not in a3 and " " not in b1 and " " not in b2 and " " not in b3:

if " " not in c1:

if " " not in c2:

if " " not in c3:

print ( "It's a draw." )

input ( )

sys . exit ( )

else :

print ( "It is O's turn now." )

o_turn ( )

else :

print ( "It is O's turn now." )

o_turn ( )

else :

print ( "It is O's turn now." )

o_turn ( )

else :

print ( "It is O's turn now." )

o_turn ( )

def check_draw_o ( ) :

if " " not in a1 and " " not in a2 and " " not in a3 and " " not in b1 and " " not in b2 and " " not in b3:

if " " not in c1:

if " " not in c2:

if " " not in c3:

print ( "It's a draw." )

input ( )

sys . exit ( )

else :

x_turn ( )

else :

x_turn ( )

else :

x_turn ( )

else :

x_turn ( )

def x_turn ( ) :

print ( "It is X's turn now." )

move = input ( )

if move == "a1" : # Lines 112-174 allow the player to put an X on any unoccupied space on the board. I'm sure

if " " in a1: # there's an easier, much more Pythonic way to do this, but I don't know what that might be.

a1. clear ( )

a1. append ( "X" )

else :

print ( "That space is occupied. Pick a different one." )

x_turn ( )

if move == "a2" :

if " " in a2:

a2. clear ( )

a2. append ( "X" )

else :

print ( "That space is occupied. Pick a different one." )

x_turn ( )

if move == "a3" :

if " " in a3:

a3. clear ( )

a3. append ( "X" )

else :

print ( "That space is occupied. Pick a different one." )

x_turn ( )

if move == "b1" :

if " " in b1:

b1. clear ( )

b1. append ( "X" )

else :

print ( "That space is occupied. Pick a different one." )

x_turn ( )

if move == "b2" :

if " " in b2:

b2. clear ( )

b2. append ( "X" )

else :

print ( "That space is occupied. Pick a different one." )

x_turn ( )

if move == "b3" :

if " " in b3:

b3. clear ( )

b3. append ( "X" )

else :

print ( "That space is occupied. Pick a different one." )

x_turn ( )

if move == "c1" :

if " " in c1:

c1. clear ( )

c1. append ( "X" )

else :

print ( "That space is occupied. Pick a different one." )

x_turn ( )

if move == "c2" :

if " " in c2:

c2. clear ( )

c2. append ( "X" )

else :

print ( "That space is occupied. Pick a different one." )

x_turn ( )

if move == "c3" :

if " " in c3:

c3. clear ( )

c3. append ( "X" )

else :

print ( "That space is occupied. Pick a different one." )

x_turn ( )

if move == "1" :

help_menu ( )

x_turn ( )

if move == "2" :

print_map ( )

x_turn ( )

if move == "3" :

print ( "It is O's turn now." )

o_turn ( )

print_map ( )

if "X" in a1 and "X" in a2 and "X" in a3: # Lines 185-216 determine if X makes a full row or if a draw occurs. If

print ( "X wins!" ) # neither event happens, it proceeds to O's turn.

input ( )

sys . exit ( )

if "X" in a1 and "X" in b2 and "X" in c3:

print ( "X wins!" )

input ( )

sys . exit ( )

if "X" in a1 and "X" in b1 and "X" in c1:

print ( "X wins!" )

input ( )

sys . exit ( )

if "X" in a2 and "X" in b2 and "X" in c2:

print ( "X wins!" )

input ( )

sys . exit ( )

if "X" in a3 and "X" in b3 and "X" in c3:

print ( "X wins!" )

input ( )

sys . exit ( )

if "X" in b1 and "X" in b2 and "X" in b3:

print ( "X wins!" )

input ( )

sys . exit ( )

if "X" in c1 and "X" in c2 and "X" in c3:

print ( "X wins!" )

input ( )

sys . exit ( )

if "X" in c1 and "X" in b2 and "X" in a3:

print ( "X wins!" )

input ( )

sys . exit ( )

check_draw_x ( )

def o_win_move ( ) :

if "O" in a1 and "O" in a2 and " " in a3: # Lines 221-247 make horizontal rows.

o_fill_a3 ( )

o_wins_endgame ( )

if "O" in a1 and " " in a2 and "O" in a3:

o_fill_a2 ( )

o_wins_endgame ( )

if " " in a1 and "O" in a2 and "O" in a3:

o_fill_a1 ( )

o_wins_endgame ( )

if "O" in b1 and "O" in b2 and " " in b3:

o_fill_b3 ( )

o_wins_endgame ( )

if "O" in b1 and " " in b2 and "O" in b3:

o_fill_b2 ( )

o_wins_endgame ( )

if " " in b1 and "O" in b2 and "O" in b3:

o_fill_b1 ( )

o_wins_endgame ( )

if "O" in c1 and "O" in c2 and " " in c3:

o_fill_c3 ( )

o_wins_endgame ( )

if "O" in c1 and " " in c2 and "O" in c3:

o_fill_c2 ( )

o_wins_endgame ( )

if " " in c1 and "O" in c2 and "O" in c3:

o_fill_c1 ( )

o_wins_endgame ( )

if "O" in a1 and "O" in b1 and " " in c1: # Lines 248-274 make vertical rows.

o_fill_c1 ( )

o_wins_endgame ( )

if "O" in a1 and " " in b1 and "O" in c1:

o_fill_b1 ( )

o_wins_endgame ( )

if " " in a1 and "O" in b1 and "O" in c1:

o_fill_a1 ( )

o_wins_endgame ( )

if "O" in a2 and "O" in b2 and " " in c2:

o_fill_c2 ( )

o_wins_endgame ( )

if "O" in a2 and " " in b2 and "O" in c2:

o_fill_b2 ( )

o_wins_endgame ( )

if " " in a2 and "O" in b2 and "O" in c2:

o_fill_a2 ( )

o_wins_endgame ( )

if "O" in a3 and "O" in b3 and " " in c3:

o_fill_c3 ( )

o_wins_endgame ( )

if "O" in a3 and " " in b3 and "O" in c3:

o_fill_b3 ( )

o_wins_endgame ( )

if " " in a3 and "O" in b3 and "O" in c3:

o_fill_a3 ( )

o_wins_endgame ( )

if " " in a1 and "O" in b2 and "O" in c3: # Lines 275-283 make diagonal rows from top left to bottom right.

o_fill_a1 ( )

o_wins_endgame ( )

if "O" in a1 and " " in b2 and "O" in c3:

o_fill_b2 ( )

o_wins_endgame ( )

if "O" in a1 and "O" in b2 and " " in c3:

o_fill_c3 ( )

o_wins_endgame ( )

if " " in a3 and "O" in b2 and "O" in c1: # Lines 284-292 make diagonal rows from bottom left to top right.

o_fill_a3 ( )

o_wins_endgame ( )

if "O" in a3 and " " in b2 and "O" in c1:

o_fill_b2 ( )

o_wins_endgame ( )

if "O" in a3 and "O" in b2 and " " in c1:

o_fill_c1 ( )

o_wins_endgame ( )

else :

o_block_move ( )

def o_turn ( ) :

o_win_move ( )

def o_wins_endgame ( ) :

print ( "O wins!" )

input ( )

sys . exit ( )

def o_fill_a1 ( ) :

a1. clear ( )

a1. append ( "O" )

print_map ( )

def o_fill_a2 ( ) :

a2. clear ( )

a2. append ( "O" )

print_map ( )

def o_fill_a3 ( ) :

a3. clear ( )

a3. append ( "O" )

print_map ( )

def o_fill_b1 ( ) :

b1. clear ( )

b1. append ( "O" )

print_map ( )

def o_fill_b2 ( ) :

b2. clear ( )

b2. append ( "O" )

print_map ( )

def o_fill_b3 ( ) :

b3. clear ( )

b3. append ( "O" )

print_map ( )

def o_fill_c1 ( ) :

c1. clear ( )

c1. append ( "O" )

print_map ( )

def o_fill_c2 ( ) :

c2. clear ( )

c2. append ( "O" )

print_map ( )

def o_fill_c3 ( ) :

c3. clear ( )

c3. append ( "O" )

print_map ( )

def o_block_move ( ) :

if "X" in a1 and "X" in a2 and " " in a3: # Lines 362-388 block X from making horizontal rows.

o_fill_a3 ( )

x_turn ( )

if "X" in a1 and " " in a2 and "X" in a3:

o_fill_a2 ( )

x_turn ( )

if " " in a1 and "X" in a2 and "X" in a3:

o_fill_a1 ( )

x_turn ( )

if "X" in b1 and "X" in b2 and " " in b3:

o_fill_b3 ( )

x_turn ( )

if "X" in b1 and " " in b2 and "X" in b3:

o_fill_b2 ( )

x_turn ( )

if " " in b1 and "X" in b2 and "X" in b3:

o_fill_b1 ( )

x_turn ( )

if "X" in c1 and "X" in c2 and " " in c3:

o_fill_c3 ( )

x_turn ( )

if "X" in c1 and " " in c2 and "X" in c3:

o_fill_c2 ( )

x_turn ( )

if " " in c1 and "X" in c2 and "X" in c3:

o_fill_c1 ( )

x_turn ( )

if "X" in a1 and "X" in b1 and " " in c1: # Lines 389-415 block X from making vertical rows.

o_fill_c1 ( )

x_turn ( )

if "X" in a1 and " " in b1 and "X" in c1:

o_fill_b1 ( )

x_turn ( )

if " " in a1 and "X" in b1 and "X" in c1:

o_fill_a1 ( )

x_turn ( )

if "X" in a2 and "X" in b2 and " " in c2:

o_fill_c2 ( )

x_turn ( )

if "X" in a2 and " " in b2 and "X" in c2:

o_fill_b2 ( )

x_turn ( )

if " " in a2 and "X" in b2 and "X" in c2:

o_fill_a2 ( )

x_turn ( )

if "X" in a3 and "X" in b3 and " " in c3:

o_fill_c3 ( )

x_turn ( )

if "X" in a3 and " " in b3 and "X" in c3:

o_fill_b3 ( )

x_turn ( )

if " " in a3 and "X" in b3 and "X" in c3:

o_fill_a3 ( )

x_turn ( )

if " " in a1 and "X" in b2 and "X" in c3: # Lines 416-424 block X from making rows top left to bottom right.

o_fill_a1 ( )

x_turn ( )

if "X" in a1 and " " in b2 and "X" in c3:

o_fill_b2 ( )

x_turn ( )

if "X" in a1 and "X" in b2 and " " in c3:

o_fill_c3 ( )

x_turn ( )

if " " in a3 and "X" in b2 and "X" in c1: # Lines 425-433 block X from making rows bottom left to top right.

o_fill_a3 ( )

x_turn ( )

if "X" in a3 and " " in b2 and "X" in c1:

o_fill_b2 ( )

x_turn ( )

if "X" in a3 and "X" in b2 and " " in c1:

o_fill_c1 ( )

x_turn ( )

else :

fork ( )

def fork ( ) :

if "O" in a2 and "O" in b1 and " " in a1:

o_fill_a1 ( )

x_turn ( )

if "O" in a2 and "O" in b3 and " " in a3:

o_fill_a3 ( )

x_turn ( )

if "O" in b3 and "O" in c2 and " " in c3:

o_fill_c3 ( )

x_turn ( )

if "O" in c2 and "O" in b1 and " " in c1:

o_fill_c1 ( )

x_turn ( )

if "O" in b2 and "O" in a1 and " " in b1:

o_fill_b1 ( )

x_turn ( )

if "O" in b2 and "O" in a3 and " " in b3:

o_fill_b3 ( )

x_turn ( )

if "O" in b2 and "O" in c3 and " " in b3:

o_fill_b3 ( )

x_turn ( )

if "O" in b2 and "O" in c1 and " " in b1:

o_fill_b1 ( )

x_turn ( )

if "O" in b2 and "O" in a1 and " " in a2:

o_fill_a2 ( )

x_turn ( )

if "O" in b2 and "O" in a3 and " " in a2:

o_fill_a2 ( )

x_turn ( )

if "O" in b2 and "O" in c3 and " " in c2:

o_fill_c2 ( )

x_turn ( )

if "O" in b2 and "O" in c1 and " " in c2:

o_fill_c2 ( )

x_turn ( )

if "O" in b1 and "O" in a2 and " " in b2:

o_fill_b2 ( )

x_turn ( )

if "O" in a2 and "O" in b3 and " " in b2:

o_fill_b2 ( )

x_turn ( )

if "O" in b3 and "O" in c2 and " " in b2:

o_fill_b2 ( )

x_turn ( )

if "O" in c2 and "O" in b1 and " " in b2:

o_fill_b2 ( )

x_turn ( )

if "O" in a1 and "O" in c3 and " " in a2 and " " in a3 and " " in b3:

o_fill_a3 ( )

x_turn ( )

if "O" in a1 and "O" in c3 and " " in b1 and " " in c1 and " " in c2:

o_fill_c1 ( )

x_turn ( )

if "O" in a3 and "O" in c1 and " " in b3 and " " in c3 and " " in c2:

o_fill_c3 ( )

x_turn ( )

if "O" in a3 and "O" in c1 and " " in a2 and " " in a1 and " " in b1:

o_fill_a1 ( )

x_turn ( )

else :

prevent_x_fork ( )

def prevent_x_fork ( ) :

if "X" in a2 and "X" in b1 and " " in a1:

o_fill_a1 ( )

x_turn ( )

if "X" in a2 and "X" in b3 and " " in a3:

o_fill_a3 ( )

x_turn ( )

if "X" in b3 and "X" in c2 and " " in c3:

o_fill_c3 ( )

x_turn ( )

if "X" in c2 and "X" in b1 and " " in c1:

o_fill_c1 ( )

x_turn ( )

if "X" in b2 and "X" in a1 and " " in b1:

o_fill_b1 ( )

x_turn ( )

if "X" in b2 and "X" in a3 and " " in b3:

o_fill_b3 ( )

x_turn ( )

if "X" in b2 and "X" in c3 and " " in b3:

o_fill_b3 ( )

x_turn ( )

if "X" in b2 and "X" in c1 and " " in b1:

o_fill_b1 ( )

x_turn ( )

if "X" in b2 and "X" in a1 and " " in a2:

o_fill_a2 ( )

x_turn ( )

if "X" in b2 and "X" in a3 and " " in a2:

o_fill_a2 ( )

x_turn ( )

if "X" in b2 and "X" in c3 and " " in c2:

o_fill_c2 ( )

x_turn ( )

if "X" in b2 and "X" in c1 and " " in c2:

o_fill_c2 ( )

x_turn ( )

if "X" in b1 and "X" in a2 and " " in b2:

o_fill_b2 ( )

x_turn ( )

if "X" in a2 and "X" in b3 and " " in b2:

o_fill_b2 ( )

x_turn ( )

if "X" in b3 and "X" in c2 and " " in b2:

o_fill_b2 ( )

x_turn ( )

if "X" in c2 and "X" in b1 and " " in b2:

o_fill_b2 ( )

x_turn ( )

if "X" in a1 and "X" in c3 and " " in a2 and " " in a3 and " " in b3:

o_fill_a3 ( )

x_turn ( )

if "X" in a1 and "X" in c3 and " " in b1 and " " in c1 and " " in c2:

o_fill_c1 ( )

x_turn ( )

if "X" in a3 and "X" in c1 and " " in b3 and " " in c3 and " " in c2:

o_fill_c3 ( )

x_turn ( )

if "X" in a3 and "X" in c1 and " " in a2 and " " in a1 and " " in b1:

o_fill_a1 ( )

x_turn ( )

else :

take_center ( )

def take_center ( ) :

if " " in b2:

o_fill_b2 ( )

x_turn ( )

else :

take_opposite_corner ( )

def take_opposite_corner ( ) :

if "X" in a1:

o_fill_c3 ( )

x_turn ( )

if "X" in a3:

o_fill_c1 ( )

x_turn ( )

if "X" in c1:

o_fill_a3 ( )

x_turn ( )

if "X" in c3:

o_fill_a1 ( )

x_turn ( )

else :

take_corner ( )

def take_corner ( ) :

corner = random . randint ( 0 , 3 )

if corner == 0 and " " in a1:

o_fill_a1 ( )

x_turn ( )

if corner == 1 and " " in a3:

o_fill_a3 ( )

x_turn ( )

if corner == 2 and " " in c3:

o_fill_c3 ( )

x_turn ( )

if corner == 3 and " " in c1:

o_fill_c1 ( )

x_turn ( )

else :

take_corner ( )