def tictactoe(): #check for existing session if 'key' in session: # Clear the session session.clear() cache.clear() #simple caching for demo purposes only key = get_session_key() session['key'] = key board = Board() rules = Rules(board) player = ArtificialIntelligence(board) #initialize rules.next_play = 'O' state = { 'board': board, 'rules': rules, 'player': player, 'next': rules.get_current_player() } cache.set(key, state) return render_template('index.html')
board = Board() rules = Rules(board) player = ArtificialIntelligence(board) 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))