Пример #1
0
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')
Пример #2
0
	while starter not in ['1', '2']:
		starter = raw_input("Invalid: Pick either 1 or 2: ")

	opponent = raw_input("Do you want to play X(X) or O(O): ")
	while opponent not in ['X', 'O']:
		opponent = raw_input("Invalid: Pick either X or O: ")

	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: ")