Esempio n. 1
0
def output(maze, cars, logs, colors=True):
    out = ''

    for y, row in enumerate(maze):
        for x, cell in enumerate(row):

            # Get value of cell without car.
            value = controls.display[cell.name].format(value=cell.value)
            color = controls.colors[cell.name]

            # Should only be one car in cell, but if not print last one.
            car = [car for car in cars if car.x == x and car.y == y]
            if car:
                # Replace value of cell with value of car if there is one.
                value = car[-1].value
                color = {
                    'bg': color['bg'],
                    'fg': WHITE if color['bg'] == BLACK else BLACK,
                    'style': None
                }

            # Two characters wide.
            value = funcs.escape(value)
            if len(value) > 2:
                value = value[:2]
            elif len(value) < 2:
                value = ('0' * (2 - len(value))) + value

            out += colorStr(value, **color) if colors else value
        out += CLS_END_LN + '\n'
    out += logs

    print(REDRAW + out + CLS_END)
Esempio n. 2
0
def output(maze, cars, logs, colors=True):
    out = ''

    for y, row in enumerate(maze):
        for x, cell in enumerate(row):

            # Get value of cell without car.
            value = controls.display[cell.name].format(value=cell.value)
            color = controls.colors[cell.name]

            # Should only be one car in cell, but if not print last one.
            car = [car for car in cars if car.x == x and car.y == y]
            if car:
                # Replace value of cell with value of car if there is one.
                value = car[-1].value
                color = {
                    'bg': color['bg'],
                    'fg': WHITE if color['bg'] == BLACK else BLACK,
                    'style': None
                }

            # Two characters wide.
            value = funcs.escape(value)
            if len(value) > 2:
                value = value[:2]
            elif len(value) < 2:
                value = ('0' * (2 - len(value))) + value

            out += colorStr(value, **color) if colors else value
        out += CLS_END_LN + '\n'
    out += logs

    print(REDRAW + out + CLS_END)
Esempio n. 3
0
def get_function(line):
    """
        Parses and returns a function on the given line.
    """

    # Get function name from before the function assignment operator.
    match = re.search(controls.regexes['function'], line.split('->')[0])
    if not match:
        funcs.error('Invalid function, invalid name.')
    name = match.group()

    # Make sure the function has an function assignment operator.
    match = re.search(controls.regexes['function'] + r' *-> *', line)
    if not match:
        funcs.error('Invalid function.')
    function = line[match.end():]

    # Function type: conditional or assignment.
    if function.lower().startswith('if'):  # Conditional

        # Get Condition
        match = re.search(r'([<>]=?)|==|(\*(?=[\*A-Za-z0-9]))', function)
        if not match:
            funcs.error('Invalid function, invalid condition.')
        condition = match.group()

        # Get condition value
        if condition == '*':
            match = re.search(controls.regexes['signal'], function)
            try:
                number = match.group()
            except (ValueError, AttributeError):
                funcs.error('Invalid function, invalid value for signal.')
        else:
            match = re.search(
                r'(-?[0-9]+)|((?<=")[^"]*(?=")|(?<=\')[^\']*(?=\'))', function)
            if match.group(1):
                number = int(match.group(1))
                string = None
            elif isinstance(match.group(2), str) and condition == '==':
                number = None
                string = str(match.group(2))
            else:
                funcs.error('Invalid function, invalid value for condition.')

        # Get 'then' keyword and statement.
        match = re.search(r' *then *', function.lower())
        if not match:
            funcs.error('Invalid function, no THEN keyword.')
        then_keywd = match.group()
        function = function[match.end():]

        match = re.search(controls.regexes['direction'], function)
        if not match:
            funcs.error('Invalid function, no THEN statement.')
        then = match.group()

        # Get 'else' keyword and statement.
        match = re.search(r' *else *', function.lower())
        if not match:
            else_ = None
        else:
            function = function[match.end():]

            match = re.search(controls.regexes['direction'], function)
            else_ = match.group()
            if not else_:
                funcs.error('Invalid function, no ELSE statement.')

        # Create lambda representing the function.
        if condition == '*':
            if number == '*':
                function = lambda value, signal: then if signal else else_
            else:
                function = lambda value, signal: then if '*' in signal or number in signal else else_

        elif condition == '<=':
            function = lambda value: then if int(value) <= number else else_
        elif condition == '==':
            if isinstance(string, str):
                function = lambda value: then if funcs.escape(
                    value) == string else else_
            else:
                function = lambda value: then if int(value
                                                     ) == number else else_
        elif condition == '>=':
            function = lambda value: then if int(value) >= number else else_
        elif condition == '>':
            function = lambda value: then if int(value) > number else else_
        elif condition == '<':
            function = lambda value: then if int(value) < number else else_

    else:  # Assignment

        # Make sure it has an assignment operator.
        match = re.search(r'[-+*/]?=', function)
        if not match:
            funcs.error('Invalid function, invalid operator.')
        operator = match.group()

        # Create lambdas representing the assignment.
        if operator == '=':
            # Find a string or an int
            quotes = r'(?<=")[^"]*(?=")|(?<=\')[^\']*(?=\')'
            str_match = re.search(quotes, function)
            int_match = re.search(r'-?[0-9]+', function)
            if not str_match and not int_match:
                funcs.error('Invalid function, invalid assignment value.')

            assign = (str_match or int_match).group()

            # Fix new lines in string assignment
            assign = '\n'.join(assign.split('\\n'))

            function = lambda value: assign

        else:
            # Find an int
            match = re.search(r'-?[0-9]+', function)
            try:
                number = int(match.group())
            except (ValueError, AttributeError):
                funcs.error('Invalid function, invalid value for operator.')

            if operator == '-=':
                function = lambda value: int(int(value) - number)
            elif operator == '+=':
                function = lambda value: int(int(value) + number)
            elif operator == '*=':
                function = lambda value: int(int(value) * number)
            elif operator == '/=':
                function = lambda value: int(int(value) / number)

    return {name: function}
Esempio n. 4
0
def output(maze, cars, logs, colors=True):
    out = ''
    busstops = []
    for y, row in enumerate(maze):
        for x, cell in enumerate(row):
            if cell.name == 'bus-stop':
                busstops.append(Decimal(cell.bench))
            # Get value of cell without car.
            value = controls.display[cell.name].format(value=cell.value)
            color = controls.colors[cell.name]

            # Should only be one car in cell, but if not print last one.
            car = [car for car in cars if car.x == x and car.y == y]
            if car:
                # Replace value of cell with value of car if there is one.
                value = car[-1].value
                color = {
                    'bg': color['bg'],
                    'fg': WHITE if color['bg'] == BLACK else BLACK,
                    'style': None
                }

            # Two characters wide.
            value = funcs.escape(value)
            if len(value) > 2:
                value = value[:2]
            elif len(value) < 2:
                value = ('0' * (2 - len(value))) + value

            out += colorStr(value, **color) if colors else value
        out += CLS_END_LN + '\n'
    out += logs
    out += CLS_END_LN + '\n'
    out += CLS_END_LN + '\n'
    i = 0
    for car in cars:
        if isinstance(car.value, str):
            out += "car " + str(i) + " = " + car.value + " carrying [ "
            for passe in car.passengers:
                if isinstance(passe, str):
                    out += passe + " , "
                else:
                    out += str(passe) + " , "
            out += ' ] ' + CLS_END_LN + '\n'
        else:
            out += "car " + str(i) + " = " + str(Decimal(
                car.value)) + " carrying [ "
            for passe in car.passengers:
                if isinstance(passe, str):
                    out += passe + " , "
                else:
                    out += str(passe) + " , "
            out += ' ] ' + CLS_END_LN + '\n'
        i += 1
    i = 0
    for ibusstop in busstops:
        if Decimal(ibusstop) != Decimal(0):
            out += "Bus Stop " + str(i) + " = " + str(Decimal(ibusstop))
            out += CLS_END_LN + '\n'
        i += 1
    out += CLS_END_LN + '\n'
    global switchgate
    out += "switchgate = " + str(run.switchgate) + CLS_END_LN + '\n'
    out += "x hotel = [ "
    for i in run.xcoordplot:
        out += str(i) + " , "
    out += ' ] ' + CLS_END_LN + '\n'
    out += "y hotel = [ "
    for i in run.ycoordplot:
        out += str(i) + " , "
    out += ' ] ' + CLS_END_LN + '\n'
    out += "t hotel = [ "
    for i in run.tcoordplot:
        out += str(i) + " , "
    out += ' ] ' + CLS_END_LN + '\n'
    out += CLS_END_LN + '\n'
    out += CLS_END_LN + '\n'
    print(REDRAW + out + CLS_END)