Пример #1
0
    def new_versus(self):
        # Create a new Human Versus game ID and redirect to it
        game = Game(versus=True)
        game_id = game.id
        Session.add(game)
        Session.commit()

        return redirect('/game/cont/%s/' % game_id)
Пример #2
0
    def long_poll(self, id):
        from datetime import datetime
        from time import sleep

        start = datetime.now()

        if 'my_board' in request.POST:
            my_board = request.POST['my_board']
            my_board = int(my_board, 2)
        else:
            return ''

        game = self.game_q.filter_by(id=id).first()

        while True:
            Session.refresh(game)
            if not game:
                return ''

            board_size = game.size
            x_pos = game.x_pos
            o_pos = game.o_pos
            cur_board = x_pos | o_pos
            data = {}

            if cur_board > my_board:
                x_pos_bin = int_to_bin(x_pos, board_size)
                o_pos_bin = int_to_bin(o_pos, board_size)

                positions = []
                for i in range(board_size):
                    positions.append([])

                    for j in range(board_size):
                        if x_pos_bin[(i*board_size)+j] == '1':
                            positions[i].append('X')
                        elif o_pos_bin[(i*board_size)+j] == '1':
                            positions[i].append('O')
                        else:
                            positions[i].append('')

                data['result'] = positions

            if bit_count(cur_board) == (board_size**2):
                data['message'] = 'It\'s a tie!'

            if game_over(x_pos):
                data['message'] = 'X Wins!'
            elif game_over(o_pos):
                data['message'] = 'O Wins!'

            if len(data) > 0:
                return data
            
            sleep(1)

            delta = datetime.now() - start
            if delta.seconds >= 30:
                return {'again': True}

        return ''
Пример #3
0
    def cont_game(self, id):
        # Continue an existing Human Versus game
        game = self.game_q.filter_by(id=id).first()
        if not game:
            return redirect('/')

        message = ''
        user_side = ''
        finished = False
        board_size = game.size
        x_pos = game.x_pos
        o_pos = game.o_pos
        x_pos_bin = int_to_bin(x_pos, board_size)
        o_pos_bin = int_to_bin(o_pos, board_size)

        if 'move' in request.POST:
            if 'user_side' in session:
                user_side = session['user_side']
            elif user_side == '':
                # Determine your side by the person whose turn it should be
                # Note: I have no idea what security issues this raises
                # should one user maliciously and purposfully clear their
                # cookies.
                if bit_count(x_pos) <= bit_count(o_pos):
                    user_side = 'X'
                else:
                    user_side = 'O'
                session['user_side'] = user_side
                session.save()

            if (not game_over(x_pos)) and (not game_over(o_pos)) and is_legal_move(x_pos, o_pos, request.POST['move'], user_side):
                if user_side == 'X':
                    x_pos, x_pos_bin = add_move(x_pos, request.POST['move'], board_size)
                    game.x_pos = x_pos
                    Session.commit()
                else:
                    o_pos, o_pos_bin = add_move(o_pos, request.POST['move'], board_size)
                    game.o_pos = o_pos
                    Session.commit()

        if game_over(x_pos):
            finished = True
            if user_side == 'X':
                message = 'You win!'
            elif user_side == 'O':
                message = 'You lose!'
        elif game_over(o_pos):
            finished = True
            if user_side == 'O':
                message = 'You win!'
            elif user_side == 'X':
                message = 'You lose!'
        elif bit_count(x_pos | o_pos) == (board_size**2):
            finished = True
            message = 'It\'s a tie!'

        positions = []
        for i in range(board_size):
            positions.append([])

            for j in range(board_size):
                if x_pos_bin[(i*board_size)+j] == '1':
                    positions[i].append('X')
                elif o_pos_bin[(i*board_size)+j] == '1':
                    positions[i].append('O')
                else:
                    positions[i].append('')

        c.board_size = board_size
        c.positions = positions
        c.user_side = user_side
        c.finished = finished
        c.message = message
        c.this_url = request.environ.get('PATH_INFO')
        # c.this_url = url.current(qualified=False)

        if request.is_xhr:
            return self.json(positions)
        
        return render('/game/continue.mako')
Пример #4
0
 def __before__(self):
     self.game_q = Session.query(Game)