ai_mark = 'O' if opponent == 'X' else 'X' next_play = opponent if starter == '1' else ai_mark #initialize rules.next_play = next_play player.set_mark(ai_mark) #computer's mark (i.e., X) end_game = False while not end_game: print_board(board.grid) mark = rules.get_current_player() if mark == ai_mark: position = player.next_move() print 'Robot plays position %d' % position board.add_mark(mark, int(position)) else: position = raw_input("Pick a position to play between 1 and 9: ") while position not in [str(x) for x in xrange(1,10)]: position = raw_input("Invalid: Pick a number between 1 and 9: ") valid = board.add_mark(mark, int(position)) while not valid: mark = raw_input("Already taken - pick an open position: ") valid = board.add_mark(mark, int(position)) end_game, result = rules.end_game(position) print result