Пример #1
0
def index():
    board_seed = int(request.cookies.get('board', 0))
    if not board_seed:
        response.set_cookie('board', "0")

    board = tictactoe.unpack_board(board_seed)

    return get_template().render()
Пример #2
0
def status():
    try:
        seed = int(request.params.seed)
    except ValueError:
        raise bottle.HTTPError(403)

    res = {}
    board = tictactoe.unpack_board(seed)
    if board.is_finished():
        res['finished'] = True
        if not board.is_draw():
            res['winner'] = board.is_win()
            res['winning_move'] = board.get_winning_move()

    return res
Пример #3
0
def handle_move():
    try:
        seed = int(request.params.seed)
        x = int(request.params.x)
        y = int(request.params.y)
    except ValueError:
        print 'invalid arguments: ', dict(request.params)
        raise bottle.HTTPError(403)

    res = {}
    board = tictactoe.unpack_board(seed)
    move_success = board.add(board.current_side, x, y)
    if not move_success:
        res['error'] = 'illegal move'
        # illegal move or taken cell
        return res

    if not board.is_finished():
        new_x, new_y = tictactoe.do_move(board)
        res['next_move'] = {
            'x': new_x,
            'y': new_y,
            'side': tictactoe.opposite_side(board.current_side),
        }

    res['seed'] = board.seed
    if board.is_finished():
        res['finished'] = True
        if not board.is_draw():
            res['winner'] = board.is_win()
            res['winning_move'] = board.get_winning_move()
        return res

    #response.set_cookie('board', str(board.seed))

    return res