myfile = "tictactoe_strategy.txt"
FILE = open(myfile,"r")
text_strings=FILE.readlines()  #Read all the lines, put each line into a list.
FILE.close()
i=0
line_number=i+1
move_number=0
moves=[]
move_line_numbers=[]
while i < len(text_strings):
  t=text_strings[i]
  t=t.rstrip()      #Remove trailing eol and white space
  #print line_number," ",t
  if (len(t) > 0) and (t[0] != "-"):
    this_move=[]
    move_line_numbers.append(line_number)
    #move_line_numbers=move_line_numbers+[line_number]
    for j in range(0,6):
      t=text_strings[i]
      t=t.rstrip()      #Remove trailing eol and white space
      #print "j=",j," t=",t
      if (len(t) < 4) or (t[0]=="-"):
        print "Error in tictactoe_strategy line ",line_number," =",t        
        exit()
      t=t.rstrip('.')
      this_move=this_move+[t]
      i=i+1
      line_number=i+1
    #print "this_move=",this_move
    moves=moves+[this_move]
  else:  
    i=i+1
    line_number=i+1
print "Moves============================="
for i in range(0,len(moves)):
  print move_line_numbers[i]," ",moves[i]
#print "Moves in a nicer format ---------"
#for i in range(0,len(moves)):
#  print move_line_numbers[i]," ----------"
#  for j in range(0,6):
#    print moves[i][j]
    
current_board=['   ','   ','   ']
while True:
  print "My move.   I am searching my database..."
  for i in range(0,len(moves)):
    if moves[i][0:3] != current_board:
      # print "Moves database ",i," does not match."
      print i,
    else:  
      print "Found!   Here is my move:"
      break
  if i==(len(moves)-1):
    print "I could not find what to do in the database.  I Failed.  You WIN."
    #print "Next time I want to win.  Where should I have moved next?"
    #teaching_choice=input("(Answer 1,2,3,4,5,6,7,8 or 9) ")    
    exit()
  #print "i=",i, moves[i]
  current_board=moves[i][3:6]
  computer_wins=False
  stalemate=True
  for i in range(0,3):
    if current_board[i].find('X') != -1:
      computer_wins=True
    if current_board[i].find(' ') != -1:
      stalemate=False
  if computer_wins:
    for i in range(0,3):
       print current_board[i]+"."
    print "I won!"
    exit()
  if stalemate:
    for i in range(0,3):
       print current_board[i]+'.'
    print "Stalemate, nobody won"
    exit()
  ok_move=False
  while not ok_move:
    #print "current_board=",current_board
    for i in range(0,3):
        print current_board[i]+'.'
    human_choice=input("Where do you want to put your o ?  (Answer 1,2,3,4,5,6,7,8 or 9) ")
    if human_choice == 1:
      if current_board[0][0:1] == ' ':
        current_board[0]='o'+current_board[0][1:3]
        ok_move=True
    elif human_choice == 2:
      if current_board[0][1:2] == ' ':
        current_board[0]=current_board[0][0:1]+'o'+ current_board[0][2:3]
        ok_move=True
    elif human_choice == 3:
      if current_board[0][2:3] == ' ':
        current_board[0]=current_board[0][0:2]+'o'
        ok_move=True
    elif human_choice == 4:
      if current_board[1][0:1] == ' ':
        current_board[1]='o'+current_board[1][1:3]
        ok_move=True
    elif human_choice == 5:
      if current_board[1][1:2] == ' ':
        current_board[1]=current_board[1][0:1]+'o'+ current_board[1][2:3]
        ok_move=True
    elif human_choice == 6:
      if current_board[1][2:3] == ' ':
        current_board[1]=current_board[1][0:2]+'o'
        ok_move=True
    elif human_choice == 7:
      if current_board[2][0:1] == ' ':
        current_board[2]='o'+ current_board[2][1:3]
        ok_move=True
    elif human_choice == 8:
      if current_board[2][1:2] == ' ':
        current_board[2]=current_board[2][0:1]+'o'+ current_board[2][2:3]
        ok_move=True
    elif human_choice == 9:
      if current_board[2][2:3] == ' ':
        current_board[2]=current_board[2][0:2]+'o'
        ok_move=True
    if ok_move:
      print "OK.  With your move the board now looks like this:"  
      for i in range(0,3):
        print current_board[i]+'.'
      print "-----"
    else:
      print "Illegal move!  Try again"
  




