Esempio n. 1
0
def parse(lines):
    cell_list = []  #Output list of cells - 2D cell list
    for line in lines:  #For each line do the following
        i = 0
        ch_list = []
        for ch in line:  #For each character in the line, append the character to a list representing that line
            ch_list.append(ch)
        line = ch_list.copy()  #Replace line with said list
        cell_list.append(line)  #Append output list with said line

    i = 0
    num_of_X = 0
    num_of_Y = 0
    teleport_list = []

    #For each cell, look at the cell character and replace it with the correct cell object
    for row in cell_list:
        j = 0
        for character in row:
            if character == "\n":
                row.remove(character)
            elif character == "X":
                cell_list[i][j] = Start()
                num_of_X += 1
            elif character == "Y":
                cell_list[i][j] = End()
                num_of_Y += 1
            elif character == " ":
                cell_list[i][j] = Air()
            elif character == "*":
                cell_list[i][j] = Wall()
            elif character == "F":
                cell_list[i][j] = Fire()
            elif character == "W":
                cell_list[i][j] = Water()
            elif character == "Y":
                cell_list[i][j] = End()
            elif character == "1" or character == "2" or character == "3" or character == "4" or character == "5" or character == "6" or character == "7" or character == "8" or character == "9":
                cell_list[i][j] = Teleport(character, i, j)
                teleport_list.append(character)
            else:
                raise ValueError(
                    f"Bad letter in configuration file: {character}.")
            j += 1
        i += 1

    #Checking for incorrect maze configurations
    if num_of_X == 0 or num_of_X > 1:  #Too many Xs
        raise ValueError(f"Expected 1 starting position, got {num_of_X}.")
    if num_of_Y == 0 or num_of_Y > 1:  #Too many Ys
        raise ValueError(f"Expected 1 ending position, got {num_of_Y}.")
    for teleport_pad in range(1, 10):  #Teleport pads don't come in pairs
        if teleport_list.count(str(teleport_pad)) != 2 and teleport_list.count(
                str(teleport_pad)) != 0:
            raise ValueError(
                f"Teleport pad {teleport_pad} does not have an exclusively matching pad."
            )
    return cell_list
Esempio n. 2
0
 def move_action(self, grid, player, move):
     if type(move) != str:
         return
     r = player.row
     c = player.col
     cell = grid[r][c]
     if r < 0 or c < 0:
         return Wall.step(cell, self, player, grid, move)
     return cell.step(self, player, grid, move)
Esempio n. 3
0
def parse(lines):
    """Transform the input into a grid.

    Arguments:
        lines -- list of strings representing the grid

    Returns:
        list: contains list of lists of Cells
    """
    grid = []
    x_count = 0
    y_count = 0
    ports = {}

    for line in lines:
        buf = []  # list of Cells in one line

        # convert string to list of cells
        for ch in line:
            if ch == 'X':
                buf.append(Start().display)
                x_count += 1
            elif ch == 'Y':
                buf.append(End().display)
                y_count += 1
            elif ch == ' ':
                buf.append(Air().display)
            elif ch == '*':
                buf.append(Wall().display)
            elif ch == 'F':
                buf.append(Fire().display)
            elif ch == 'W':
                buf.append(Water().display)
            elif ch in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
                buf.append(str(Teleport(int(ch)).display))
                if ch in ports.keys():
                    ports[ch].append(ch)
                else:
                    ports[ch] = [ch]
            elif ch == '\n':
                pass
            else:
                # find unknown letter
                raise ValueError("Bad letter in configuration file: " + ch)
        grid.append(buf)

    if x_count != 1:
        raise ValueError("Expected 1 starting position, got " + str(x_count) + ".")
    if y_count != 1:
        raise ValueError("Expected 1 ending position, got " + str(y_count) + ".")
    for port in ports.keys():

        # find teleport pad that does not come in pairs
        if len(ports[port]) != 2:
            raise ValueError("Teleport pad " + port + " does not have an exclusively matching pad.")
    return grid
Esempio n. 4
0
def test_wall_step():
    '''tests normal operation of Wall step method. Player postion should be set to what it was before the last move.'''
    game = Game.__new__(Game)
    game.player = Player.__new__(Player)
    game.player.col = 1
    game.player.row = 1
    game.temp_move = 's'
    Wall().step(game)
    assert game.player.col == 1, 'Failed Wall step: player col attribute should not change'
    assert game.player.row == 0, 'Failed Wall step: player row attribute should not change'
Esempio n. 5
0
    def game_move(self, move):
        # Store current location, so if an illegal move is made, it can go back
        y = self.player.row
        x = self.player.col

        # Perform the given move command
        self.player.move(move)

        if (self.player.row < 0 or self.player.col < 0
                or self.player.row >= len(self.grid)
                or self.player.col >= len(self.grid[0])):
            # If the player is on the border and tries to step out of bounds, act as a wall
            # None is used in step as we do not require a cell instance here
            result = Wall.step(None, self)
            self.msg = result[2]
        else:
            # The player did not try step out of bounds, so perform the step
            # command for the cell type that the player went to
            result = self.grid[self.player.row][self.player.col].step(self)

            if len(result) == 3:
                self.msg = result[2]
            else:
                # The player moved onto the cell that does not return a message
                self.msg = ''

        isLegalMove, isEndingMove = result[0], result[1]

        if isLegalMove:
            if isEndingMove:
                # Player landed on finish point (Won game)
                self.moves_made.append(move)
                self.finished = True
            else:
                # Player made a legal move that did not win the game_won
                # i.e. player stepped on air,TP pad, water, start
                self.moves_made.append(move)

        else:
            if isEndingMove:
                # Player stepped on fire and died (Game over)
                self.moves_made.append(move)
                self.finished = True
                self.lost = True
            else:
                # Player walked into a wall, revert back to previous position
                self.player.row = y
                self.player.col = x
Esempio n. 6
0
def test_cells():
    game = Game('board_hard.txt')
    player = Player()
    grid = game_parser.parse(game_parser.read_lines('board_hard.txt'))

    ## Testing cell.step() ##
    cell = Start('X')
    assert cell.step(game, player, grid,
                     'a') == '', 'cells testcase 1 failed, Start.step.'
    cell = End('Y')
    assert cell.step(
        game, player, grid, 'a'
    ) == 'end of game - successful', 'cells testcase 2 failed, End.step.'
    cell = Air(' ')
    assert cell.step(game, player, grid,
                     'w') == '', 'cells testcase 3 failed, Air.step.'
    cell = Wall('*')
    assert cell.step(
        game, player, grid, 's'
    ) == '\nYou walked into a wall. Oof!\n', 'cells testcase 4 failed, Wall.step.'
    cell = Fire('F')
    assert cell.step(
        game, player, grid, 'd'
    ) == 'end of game - unsuccessful', 'cells testcase 5 failed, Fire.step with no water buckets.'
    player.num_water_buckets = 1
    assert cell.step(game, player, grid, 'a') == '\nWith your strong acorn arms, you throw a water bucket at the fire.' \
                'You acorn roll your way through the extinguished flames!\n', 'cells testcase 6 failed, Fire.step with water bucket.'
    cell = Water('W')
    assert cell.step(
        game, player, grid, 's'
    ) == "\nThank the Honourable Furious Forest, you've found a bucket of water!\n", 'cells testcase 7 failed, Water.step'
    assert cell.step(
        game, player, grid, 'a'
    ) == '', 'cells testcase 8 failed, Water.step should only function when stepped on for the first time.'
    cell = Teleport('1')
    assert cell.step(game, player, grid, 'a') == '\nWhoosh! The magical gates break Physics as we know ' \
            'it and opens a wormhole through space and time.\n', 'cells testcase 9 failed, Teleport.step.'
def test_parse():
    # The first test case,both are positive tests
    wall = Wall()
    start = Start()
    end = End()
    teleplort = Teleport(2)
    f = open("wall.txt", "r")
    line = f.readlines(
    )  # get the string gird,put it in the the parse function
    f.close()
    new_grid = parse(line)
    i = 0
    count = 0
    maxnumber = len(new_grid) * len(new_grid[0])
    if type(new_grid[0][0]) == type(wall):
        count += 1
    if type(new_grid[0][1]) == type(start):
        count += 1
    if type(new_grid[0][2]) == type(wall):
        count += 1
    if type(new_grid[1][0]) == type(wall):
        count += 1
    if type(new_grid[1][1]) == type(end):
        count += 1
    if type(new_grid[1][2]) == type(wall):
        count += 1
    assert maxnumber == count, "Test case 1 did not pass"
    print("Test case 1 for Parse pass")

    # The second test case ,edge test case
    f = open("board_hard.txt", "r")
    line = f.readlines()
    f.close()
    grid_two = parse(line)
    assert isinstance(grid_two[0][0], type(wall)) and isinstance(grid_two[5][3], type(teleplort)) == True, \
        "Test case 2 did not pass"
    print("Test case 2 for Parse pass")
Esempio n. 8
0
def test_cell():
    start = Start()
    end = End()
    air = Air()
    wall = Wall()
    fire = Fire()
    water = Water()
    port_one = Teleport('1')
    port_two = Teleport('2')
    port_three = Teleport('3')
    port_four = Teleport('4')
    port_five = Teleport('5')
    port_six = Teleport('6')
    port_seven = Teleport('7')
    port_eight = Teleport('8')
    port_nine = Teleport('9')

    try:
        assert start.display == 'X'
        assert end.display == 'Y'
        assert air.display == ' '
        assert wall.display == '*'
        assert fire.display == 'F'
        assert water.display == 'W'
        assert port_one.display == '1'
        assert port_two.display == '2'
        assert port_three.display == '3'
        assert port_four.display == '4'
        assert port_five.display == '5'
        assert port_six.display == '6'
        assert port_seven.display == '7'
        assert port_eight.display == '8'
        assert port_nine.display == '9'
        print("Test 01: Cell Initialization passed!")
    except AssertionError:
        print("Test 01: Cell Initialization failed!")
Esempio n. 9
0
def parse(lines):
    """Transform the input into a grid.

    Arguments:
        lines -- list of strings representing the grid

    Returns:
        list -- contains list of lists of Cells
    """
    output = []
    good_letters = [
        'A', ' ', 'X', 'Y', '*', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'W', 'F', '\n'
    ]
    y_count = 0
    x_count = 0
    pad_counts = {}

    for row in lines:
        temp = []
        i = 0

        while i < len(row):
            if row[i] not in good_letters:
                raise ValueError(
                    'Bad letter in configuration file: {}.'.format(row[i]))
            if row[i] == 'X':
                temp.append(Start())
                x_count += 1
            if row[i] == 'Y':
                temp.append(End())
                y_count += 1
            if row[i] in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
                if pad_counts.get(row[i]) == None:
                    pad_counts[row[i]] = 1
                else:
                    pad_counts[row[i]] += 1
                temp.append(Teleport(row[i]))
            if row[i] == ' ':
                temp.append(Air())
            if row[i] == '*':
                temp.append(Wall())
            if row[i] == 'W':
                temp.append(Water())
            if row[i] == 'F':
                temp.append(Fire())
            if row[i] == '\n':
                i += 1
                continue
            i += 1

        output.append(temp)

    if x_count != 1:
        raise ValueError(
            'Expected 1 starting position, got {}.'.format(x_count))
    if y_count != 1:
        raise ValueError('Expected 1 ending position, got {}.'.format(y_count))
    for x in pad_counts.items():
        if x[1] != 2:
            raise ValueError(
                'Teleport pad {} does not have an exclusively matching pad.'.
                format(x[0]))

    return output
Esempio n. 10
0
def test_grid3():
    #edge case with 1 waterbucket at the other position
    grid = [[Wall(), Wall(), Start(), Wall(),
             Wall()], [Wall(), Water(),
                       Teleport('1'),
                       Wall(), Wall()],
            [Wall(), Air(), Fire(),
             Teleport('1'), Wall()], [Wall(),
                                      Wall(),
                                      End(),
                                      Wall(),
                                      Wall()]]
    player = Player()
    player.row = 2
    player.col = 1
    player.num_water_buckets = 1
    actual = grid_to_string(grid, player)
    excepted = '**X**\n*W1**\n*AF1*\n**Y**\n\nYou have 1 water bucket.'
    assert actual == excepted, 'tesecase test_grid3 failed.'
Esempio n. 11
0
def test_grid1():
    #postive testcase with 2 waterbucket at the other position
    grid = [[Wall(), Wall(), Start(), Wall(),
             Wall()], [Wall(), Water(),
                       Teleport('1'),
                       Wall(), Wall()],
            [Wall(), Air(), Fire(),
             Teleport('1'), Wall()], [Wall(),
                                      Wall(),
                                      End(),
                                      Wall(),
                                      Wall()]]
    player = Player()
    player.row = 1
    player.col = 1
    player.num_water_buckets = 2
    actual = grid_to_string(grid, player)
    excepted = '**X**\n*A1**\n* F1*\n**Y**\n\nYou have 2 water buckets.'
    assert actual == excepted, 'testcase test_grid1 failed.'
Esempio n. 12
0
def parse(lines):
    """Transform the input into a grid.

    Arguments:
        lines -- list of strings representing the grid

    Returns:
        list -- contains list of lists of Cells
    """
    i = 0
    while i < len(lines):
        holder = lines[i].strip("\n")
        lines[i] = holder
        i +=1

    print(lines)

    result_x = 0
    result_y = 0
    result_1 = 0
    result_2 = 0
    result_3 = 0
    result_4 = 0
    result_5 = 0
    result_6 = 0
    result_7 = 0
    result_8 = 0
    result_9 = 0

    for strings in lines:
        for objects in strings:
            if objects != "*" and objects != " " and objects != "X" and objects != "Y" and objects != "F" and objects != "W" and objects != "1" and objects != "2" and objects != "3" and objects != "4" and objects != "5" and objects != "6" and objects != "7" and objects != "8" and objects != "9":
                raise ValueError ("Bad letter configuration file: {}".format(objects))
            if objects == "X":
                result_x += 1
            if objects == "Y":
                result_y += 1
            if objects == "1":
                result_1 += 1
            if objects == "2":
                result_2 += 1
            if objects == "3":
                result_3 += 1
            if objects == "4":
                result_4 += 1
            if objects == "5":
                result_5 += 1
            if objects == "6":
                result_6 += 1
            if objects == "7":
                result_7 += 1
            if objects == "8":
                result_8 += 1
            if objects == "9":
                result_9 += 1

    if result_x != 1:
        raise ValueError ("Expected 1 starting position, got {}".format(result_x))
    if result_y != 1:
        raise ValueError ("Expected 1 ending position, got {}".format(result_y))
    if result_1 == 1 or result_1 > 2:
        raise ValueError ("1 does not have an exclusively matching pad")
    if result_2 == 1 or result_2 > 2:
        raise ValueError ("2 does not have an exclusively matching pad")
    if result_3 == 1 or result_3 > 2:
        raise ValueError ("3 does not have an exclusively matching pad")
    if result_4 == 1 or result_4 > 2:
        raise ValueError ("4 does not have an exclusively matching pad")
    if result_5 == 1 or result_5 > 2:
        raise ValueError ("5 does not have an exclusively matching pad")
    if result_6 == 1 or result_6 > 2:
        raise ValueError ("6 does not have an exclusively matching pad")
    if result_7 == 1 or result_7 > 2:
        raise ValueError ("7 does not have an exclusively matching pad")
    if result_8 == 1 or result_8 > 2:
        raise ValueError ("8 does not have an exclusively matching pad")
    if result_9 == 1 or result_9 > 2:
        raise ValueError ("9 does not have an exclusively matching pad")

    list_of_cells = []
    list_of_lists_of_cells = []
    for strings in lines:
        for objects in strings:
            if objects == "*":
                wall = Wall()
                list_of_cells.append(wall)
            if objects == " ":
                air = Air()
                list_of_cells.append(air)
            if objects == "X":
                start = Start()
                list_of_cells.append(start)
            if objects == "Y":
                end = End()
                list_of_cells.append(end)
            if objects == "F":
                fire = Fire()
                list_of_cells.append(fire)
            if objects == "W":
                water = Water()
                list_of_cells.append(water)
            if objects == "1":
                teleport1 = Teleport(1)
                list_of_cells.append(teleport1)
            if objects == "2":
                teleport2 = Teleport(2)
                list_of_cells.append(teleport2)
            if objects == "3":
                teleport3 = Teleport(3)
                list_of_cells.append(teleport3)
            if objects == "4":
                teleport4 = Teleport(4)
                list_of_cells.append(teleport4)
            if objects == "5":
                teleport5 = Teleport(5)
                list_of_cells.append(teleport5)
            if objects == "6":
                teleport6 = Teleport(6)
                list_of_cells.append(teleport6)
            if objects == "7":
                teleport7 = Teleport(7)
                list_of_cells.append(teleport7)
            if objects == "8":
                teleport8 = Teleport(8)
                list_of_cells.append(teleport8)
            if objects == "9":
                teleport9 = Teleport(9)
                list_of_cells.append(teleport9)

            #Do teleporter, figure it out u monkey
        list_of_lists_of_cells.append(list_of_cells)
        list_of_cells = []
    return list_of_lists_of_cells
Esempio n. 13
0
def parse(lines):
    """Transform the input into a grid.

    Arguments:
        lines -- list of strings representing the grid

    Returns:
        list -- contains list of lists of Cells
    """
    row = 0
    column = 0

    # Counters to help validate the given grid
    x_count = 0
    y_count = 0
    tp_pads_list = []

    # Grid in which the list of list of cell objects will be appended to
    grid = []

    while row < len(lines):
        line = []
        while column < len(lines[0]) - 1:  # -1 to ignore \n
            current_cell = lines[row][column]

            if not (current_cell.isnumeric()):  # Checking for incorrect letter
                if current_cell == "X":
                    x_count += 1
                    cell_object = Start(row, column)
                    line.append(cell_object)
                elif current_cell == "Y":
                    y_count += 1
                    cell_object = End()
                    line.append(cell_object)
                elif current_cell == "F":
                    cell_object = Fire()
                    line.append(cell_object)
                elif current_cell == "W":
                    cell_object = Water()
                    line.append(cell_object)
                elif current_cell == " ":
                    cell_object = Air()
                    line.append(cell_object)
                elif current_cell == "*":
                    cell_object = Wall()
                    line.append(cell_object)
                else:
                    raise ValueError(
                        f"Bad letter in configuration file: {current_cell}.")
            else:  # Checking for invalid teleport pad pairs or 0
                if current_cell == "0":
                    raise ValueError(
                        f"Bad letter in configuration file: {current_cell}.")
                cell_object = Teleport(current_cell, row, column)
                line.append(cell_object)

                hasPair = False
                # If a pair has been successfully found, we can remove it from the list
                # We know also know the 2 locations of the pair, and so we can update
                # the respective partner_location attributes
                for cell in tp_pads_list:
                    if cell.display == cell_object.display:
                        cell.partner_location = [
                            cell_object.location[0], cell_object.location[1]
                        ]
                        cell_object.partner_location = [
                            cell.location[0], cell.location[1]
                        ]
                        tp_pads_list.remove(cell)
                        hasPair = True

                # If current cell is the first TP pad, then add it to the tp_pads_list
                if not (hasPair):
                    tp_pads_list.append(cell_object)

            column += 1
        # The current row has been completed, move to next row
        grid.append(line)
        column = 0
        row += 1

    # Raising errors for invalid grids
    if x_count != 1:
        raise ValueError(f"Expected 1 starting position, got {x_count}.")
    if y_count != 1:
        raise ValueError(f"Expected 1 ending position, got {y_count}.")
    if tp_pads_list:
        raise ValueError(
            f"Teleport pad {tp_pads_list[0].display} does not have an exclusively matching pad."
        )

    return grid
Esempio n. 14
0
def parse(lines):
    """Transform the input into a grid.

    Arguments:
        lines -- list of strings representing the grid

    Returns:
        list -- contains list of lists of Cells
    """
    ls_of_line = []  #a  list of lists of cell
    ls_of_cells_inobject = []  #a list of cells' object
    x_occurrence = 0  #occurence of starting cell
    y_occurrence = 0  #occurence of ending cell
    teleport_checker = []  #a list to help check the teleport gates are valid or not
    cell_charater = ['X', 'Y', '*', ' ', 'W', 'F', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\n']  #valid cell character
    pad = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] #valid teleport pad
    for a in lines:
        for cell in a:
            if cell in cell_charater:
                if cell == 'X':
                    ls_of_cells_inobject.append(Start())  #storage the object of X
                    x_occurrence += 1

                elif cell == 'Y':
                    ls_of_cells_inobject.append(End())  #storage the object of Y
                    y_occurrence += 1

                elif cell == '*':
                    ls_of_cells_inobject.append(Wall())  #storage the object of *

                elif cell == ' ':
                    ls_of_cells_inobject.append(Air())  #storage the object of ' '

                elif cell == 'W':
                    ls_of_cells_inobject.append(Water())  #storage the object of W

                elif cell == 'F':
                    ls_of_cells_inobject.append(Fire())  #storage the object of F

                elif cell in pad:
                    teleport_checker.append(cell)
                    ls_of_cells_inobject.append(Teleport(cell))   #storage the object of teleport  pad
            else:
                raise ValueError('Bad letter in configuration file: {}.'.format(cell))
                #raise error if there is a bad letter

        ls_of_line.append(ls_of_cells_inobject)
        ls_of_cells_inobject = []  #renew the list for storaging object

    if x_occurrence > 1 or x_occurrence == 0:
        raise ValueError('Expected 1 starting position, got {}.'.format(x_occurrence))
        #check the occurrence of starting cell

    if y_occurrence > 1 or y_occurrence == 0:
        raise ValueError('Expected 1 ending position, got {}.'.format(y_occurrence))
        #check the occurrence of ending cell

    teleport_checker_set = set(teleport_checker)
    for each_pad_set in teleport_checker_set:
        count = 0
        for each_pad in teleport_checker:
            if each_pad_set == each_pad:
                count += 1
        if not count % 2 == 0:
            raise ValueError('Teleport pad {} does not have an exclusively matching pad.'.format(each_pad_set))
            #check the teleport pads exit in pair or not

    return ls_of_line
Esempio n. 15
0
def parse(lines):
    """Transform the input into a grid.

    Arguments:
        lines -- list of strings representing the grid

    Returns:
        list -- contains list of lists of Cells
    """
    result_x = 0  #to check for the amount of start, end and teleport cells
    result_y = 0
    result_1 = 0
    result_2 = 0
    result_3 = 0
    result_4 = 0
    result_5 = 0
    result_6 = 0
    result_7 = 0
    result_8 = 0
    result_9 = 0

    for strings in lines:
        for objects in strings:  #going through the list of strings to count the amount of start, end and teleport cells
            if objects != "*" and objects != " " and objects != "X" and objects != "Y" and objects != "F" and objects != "W" and objects != "1" and objects != "2" and objects != "3" and objects != "4" and objects != "5" and objects != "6" and objects != "7" and objects != "8" and objects != "9":
                raise ValueError(
                    "Bad letter in configuration file: {}.".format(objects))
            if objects == "X":
                result_x += 1
            if objects == "Y":
                result_y += 1
            if objects == "1":
                result_1 += 1
            if objects == "2":
                result_2 += 1
            if objects == "3":
                result_3 += 1
            if objects == "4":
                result_4 += 1
            if objects == "5":
                result_5 += 1
            if objects == "6":
                result_6 += 1
            if objects == "7":
                result_7 += 1
            if objects == "8":
                result_8 += 1
            if objects == "9":
                result_9 += 1

    if result_x != 1:  #if there is the wrong number of start, end and teleport cells in the list of strings, then raise a error notifying the user
        raise ValueError(
            "Expected 1 starting position, got {}.".format(result_x))
    if result_y != 1:
        raise ValueError(
            "Expected 1 ending position, got {}.".format(result_y))
    if result_1 == 1 or result_1 > 2:
        raise ValueError(
            "Teleport pad 1 does not have an exclusively matching pad.")
    if result_2 == 1 or result_2 > 2:
        raise ValueError(
            "Teleport pad 2 does not have an exclusively matching pad.")
    if result_3 == 1 or result_3 > 2:
        raise ValueError(
            "Teleport pad 3 does not have an exclusively matching pad.")
    if result_4 == 1 or result_4 > 2:
        raise ValueError(
            "Teleport pad 4 does not have an exclusively matching pad.")
    if result_5 == 1 or result_5 > 2:
        raise ValueError(
            "Teleport pad 5 does not have an exclusively matching pad.")
    if result_6 == 1 or result_6 > 2:
        raise ValueError(
            "Teleport pad 6 does not have an exclusively matching pad.")
    if result_7 == 1 or result_7 > 2:
        raise ValueError(
            "Teleport pad 7 does not have an exclusively matching pad.")
    if result_8 == 1 or result_8 > 2:
        raise ValueError(
            "Teleport pad 8 does not have an exclusively matching pad.")
    if result_9 == 1 or result_9 > 2:
        raise ValueError(
            "Teleport pad 9 does not have an exclusively matching pad.")

    list_of_cells = []
    list_of_lists_of_cells = []
    for strings in lines:  #going through the file and creating a list of lists of cells according to the text file
        for objects in strings:
            if objects == "*":
                wall = Wall()
                list_of_cells.append(wall)
            if objects == " ":
                air = Air()
                list_of_cells.append(air)
            if objects == "X":
                start = Start()
                list_of_cells.append(start)
            if objects == "Y":
                end = End()
                list_of_cells.append(end)
            if objects == "F":
                fire = Fire()
                list_of_cells.append(fire)
            if objects == "W":
                water = Water()
                list_of_cells.append(water)
            if objects == "1":
                teleport1 = Teleport(1)
                list_of_cells.append(teleport1)
            if objects == "2":
                teleport2 = Teleport(2)
                list_of_cells.append(teleport2)
            if objects == "3":
                teleport3 = Teleport(3)
                list_of_cells.append(teleport3)
            if objects == "4":
                teleport4 = Teleport(4)
                list_of_cells.append(teleport4)
            if objects == "5":
                teleport5 = Teleport(5)
                list_of_cells.append(teleport5)
            if objects == "6":
                teleport6 = Teleport(6)
                list_of_cells.append(teleport6)
            if objects == "7":
                teleport7 = Teleport(7)
                list_of_cells.append(teleport7)
            if objects == "8":
                teleport8 = Teleport(8)
                list_of_cells.append(teleport8)
            if objects == "9":
                teleport9 = Teleport(9)
                list_of_cells.append(teleport9)
            #Do teleporter, figure it out u monkey
        list_of_lists_of_cells.append(list_of_cells)
        list_of_cells = []
    return list_of_lists_of_cells
    def next_step(self, move):
        """
        
        Args:
            move: The move we made which are "a,s,d,q,w,e"

        Returns:
            nothing, but it can change the acorn's coordinate, when
            the acorn pass the water, fire or teleport, it will print
            the corresponding information. 

        """
        start = Start()
        end = End()
        air = Air()
        wall = Wall()
        fire = Fire()
        water = Water()

        if move == "w":
            x = -1
            y = 0

        elif move == "s":
            x = 1
            y = 0

        elif move == "a":
            x = 0
            y = -1

        elif move == "d":
            x = 0
            y = 1

        elif move == "e":
            x = 0
            y = 0

            if self.grid[self.player.row][self.player.col].display == "1":
                self.find_teleport(1, self.player.row, self.player.col)
                self.print_magic += 1
                self.player.move(move)
                return
            elif self.grid[self.player.row][self.player.col].display == "2":
                self.find_teleport(2, self.player.row, self.player.col)
                self.print_magic += 1
                self.player.move(move)
                return
            elif self.grid[self.player.row][self.player.col].display == "3":
                self.find_teleport(3, self.player.row, self.player.col)
                self.print_magic += 1
                self.player.move(move)
                return
            elif self.grid[self.player.row][self.player.col].display == "4":
                self.find_teleport(4, self.player.row, self.player.col)
                self.print_magic += 1
                self.player.move(move)
                return
            elif self.grid[self.player.row][self.player.col].display == "5":
                self.find_teleport(5, self.player.row, self.player.col)
                self.print_magic += 1
                self.player.move(move)
                return
            elif self.grid[self.player.row][self.player.col].display == "6":
                self.find_teleport(6, self.player.row, self.player.col)
                self.print_magic += 1
                self.player.move(move)
                return
            elif self.grid[self.player.row][self.player.col].display == "7":
                self.find_teleport(7, self.player.row, self.player.col)
                self.print_magic += 1
                self.player.move(move)
                return
            elif self.grid[self.player.row][self.player.col].display == "8":
                self.find_teleport(8, self.player.row, self.player.col)
                self.print_magic += 1
                self.player.move(move)
                return
            elif self.grid[self.player.row][self.player.col].display == "9":
                self.find_teleport(9, self.player.row, self.player.col)
                self.print_magic += 1
                self.player.move(move)
                return
            elif self.grid[self.player.row][self.player.col].display == "0":
                self.find_teleport(0, self.player.row, self.player.col)
                self.print_magic += 1
                self.player.move(move)
                return
        if type(self.grid[self.player.row + x][self.player.col + y]) == type(water):

            self.player.num_water_buckets = self.player.num_water_buckets + 1
            self.print_water += 1
            self.player.col = self.player.col + y
            self.player.row = self.player.row + x
            self.grid[self.player.row][self.player.col] = air

            self.player.move(move)


        elif type(self.grid[self.player.row + x][self.player.col + y]) == type(fire):
            self.player.num_water_buckets = self.player.num_water_buckets - 1
            if self.player.num_water_buckets < 0:
                self.player.move(move)
                self.print_fire += 1
                self.player.num_water_buckets = 0
            else:
                self.print_live_fire += 1
            self.player.col = self.player.col + y
            self.player.row = self.player.row + x
            self.grid[self.player.row][self.player.col] = air
            self.player.move(move)

        elif self.grid[self.player.row + x][self.player.col + y].display == "1":

            self.print_magic += 1
            self.find_teleport(1, self.player.row + x, self.player.col + y)
            self.player.move(move)
        elif self.grid[self.player.row + x][self.player.col + y].display == "2":
            self.print_magic += 1
            self.find_teleport(2, self.player.row + x, self.player.col + y)
            self.player.move(move)
        elif self.grid[self.player.row + x][self.player.col + y].display == "3":
            self.print_magic += 1
            self.find_teleport(3, self.player.row + x, self.player.col + y)
            self.player.move(move)
        elif self.grid[self.player.row + x][self.player.col + y].display == "4":
            self.print_magic += 1
            self.find_teleport(4, self.player.row + x, self.player.col + y)
            self.player.move(move)
        elif self.grid[self.player.row + x][self.player.col + y].display == "5":
            self.print_magic += 1
            self.find_teleport(5, self.player.row + x, self.player.col + y)
            self.player.move(move)
        elif self.grid[self.player.row + x][self.player.col + y].display == "6":
            self.print_magic += 1
            self.find_teleport(6, self.player.row + x, self.player.col + y)
            self.player.move(move)
        elif self.grid[self.player.row + x][self.player.col + y].display == "7":
            self.print_magic += 1
            self.find_teleport(7, self.player.row + x, self.player.col + y)
            self.player.move(move)
        elif self.grid[self.player.row + x][self.player.col + y].display == "8":
            self.print_magic += 1
            self.find_teleport(8, self.player.row + x, self.player.col + y)
            self.player.move(move)
        elif self.grid[self.player.row + x][self.player.col + y].display == "9":
            self.print_magic += 1
            self.find_teleport(9, self.player.row + x, self.player.col + y)
            self.player.move(move)
        elif self.grid[self.player.row + x][self.player.col + y].display == "0":
            self.print_magic += 1
            self.find_teleport(0, self.player.row + x, self.player.col + y)
            self.player.move(move)
        elif type(self.grid[self.player.row + x][self.player.col + y]) == type(air):
            self.player.move(move)
            self.player.row = self.player.row + x
            self.player.col = self.player.col + y

        elif type(self.grid[self.player.row + x][self.player.col + y]) == type(wall):
            self.print_wall += 1
        elif type(self.grid[self.player.row + x][self.player.col + y]) == type(end):
            self.player.row = self.player.row + x
            self.player.col = self.player.col + y
            self.player.move(move)
            self.print_end_game += 1
        elif type(self.grid[self.player.row + x][self.player.col + y]) == type(start):
            self.player.row = self.player.row + x
            self.player.col = self.player.col + y
            self.player.move(move)
        else:
            pass
Esempio n. 17
0
def test_grid2():
    #edge case with 0 waterbucket at the initial postion
    grid = [[Wall(), Wall(), Start(), Wall(),
             Wall()], [Wall(), Water(),
                       Teleport('1'),
                       Wall(), Wall()],
            [Wall(), Air(), Fire(),
             Teleport('1'), Wall()], [Wall(),
                                      Wall(),
                                      End(),
                                      Wall(),
                                      Wall()]]
    player = Player()
    actual = grid_to_string(grid, player)
    excepted = 'A*X**\n*W1**\n* F1*\n**Y**\n\nYou have 0 water buckets.'
    assert actual == excepted, 'testcase test_grid2 failed.'
def parse(lines):
    """Transform the input into a grid.

    Arguments:
        lines -- list of strings representing the grid

    Returns:
        list -- contains list of lists of Cells
    """

    start = Start()
    end = End()
    air = Air()
    wall = Wall()
    fire = Fire()
    water = Water()
    teleport_one = Teleport(1)
    teleport_two = Teleport(2)
    teleport_three = Teleport(3)
    teleport_four = Teleport(4)
    teleport_five = Teleport(5)
    teleport_six = Teleport(6)
    teleport_seven = Teleport(7)
    teleport_eight = Teleport(8)
    teleport_nine = Teleport(9)
    teleport_zero = Teleport(0)

    i = 0  # used for loop
    x = 0  # used for judging if X(Start position) exists when the loop of while is over
    y = 0  # used for judging if Y(End position) exists when the loop of while is over
    # The variables below represent the number of different teleport
    zero = 0
    one = 0
    two = 0
    three = 0
    four = 0
    five = 0
    six = 0
    seven = 0
    eight = 0
    nine = 0

    # Process the line(a list of list string) from here
    ls = []
    for i in lines:
        i = i.strip()
        ls.append(list(i))

    lines = ls

    i = 0

    while i < len(lines):
        j = 0
        while j < len(lines[i]):

            if lines[i][j] != "X" and lines[i][j] != "Y" and lines[i][j] != " " and \
                    lines[i][j] != "*" and lines[i][j] != "F" \
                    and lines[i][j] != "W" and lines[i][j] != "0" \
                    and lines[i][j] != "1" and lines[i][j] != "2" \
                    and lines[i][j] != "3" and lines[i][j] != "4" and \
                    lines[i][j] != "5" and lines[i][j] != "6" \
                    and lines[i][j] != "7" and lines[i][j] != "8" and \
                    lines[i][j] != "9":
                raise ValueError(
                    "Bad letter in configuration file: {}.".format(
                        lines[i][j]))
            pass
            if lines[i][j] == "X":
                x = x + 1
            if lines[i][j] == "Y":
                y = y + 1
                # check if the Teleport pads have an exclusively matching pad.
                # if mathched, then the number plus one
            if lines[i][j] == "0":
                zero = zero + 1
            if lines[i][j] == "1":
                one += 1
            if lines[i][j] == "2":
                two += 1
            if lines[i][j] == "3":
                three += 1
            if lines[i][j] == "4":
                four += 1
            if lines[i][j] == "5":
                five += 1
            if lines[i][j] == "6":
                six += 1
            if lines[i][j] == "7":
                seven += 1
            if lines[i][j] == "8":
                eight += 1
            if lines[i][j] == "9":
                nine += 1
            j = j + 1
        i = i + 1
    # If the Teleport pads do not have an exclusively matching pad, then raise ValueError
    if x != 1:
        raise ValueError("Expected 1 starting position, got {}.".format(x))
    if y != 1:
        raise ValueError("Expected 1 ending position, got {}.".format(y))
    if zero != 2 and zero != 0:
        raise ValueError(
            "Teleport pad 0 does not have an exclusively matching pad.")
    if one != 2 and one != 0:
        raise ValueError(
            "Teleport pad 1 does not have an exclusively matching pad.")
    if two != 2 and two != 0:
        raise ValueError(
            "Teleport pad 2 does not have an exclusively matching pad.")
    if three != 2 and three != 0:
        raise ValueError(
            "Teleport pad 3 does not have an exclusively matching pad.")
    if four != 2 and four != 0:
        raise ValueError(
            "Teleport pad 4 does not have an exclusively matching pad.")
    if five != 2 and five != 0:
        raise ValueError(
            "Teleport pad 5 does not have an exclusively matching pad.")
    if six != 2 and six != 0:
        raise ValueError(
            "Teleport pad 6 does not have an exclusively matching pad.")
    if seven != 2 and seven != 0:
        raise ValueError(
            "Teleport pad 7 does not have an exclusively matching pad.")
    if eight != 2 and eight != 0:
        raise ValueError(
            "Teleport pad 8 does not have an exclusively matching pad.")
    if nine != 2 and nine != 0:
        raise ValueError(
            "Teleport pad 9 does not have an exclusively matching pad.")
    ''' 
    After raise all the ValueError, if there is nothing wrong, we change a
    list of lists string into a list of lists instances(Object)
    '''
    i = 0
    while i < len(lines):
        j = 0
        while j < len(lines[i]):

            if lines[i][j] == start.display:
                lines[i][j] = start
            elif lines[i][j] == end.display:
                lines[i][j] = end
            elif lines[i][j] == air.display:
                lines[i][j] = air
            elif lines[i][j] == wall.display:
                lines[i][j] = wall
            elif lines[i][j] == fire.display:
                lines[i][j] = fire
            elif lines[i][j] == water.display:
                lines[i][j] = water
            elif lines[i][j] == teleport_zero.display:
                lines[i][j] = teleport_zero
            elif lines[i][j] == teleport_one.display:
                lines[i][j] = teleport_one
            elif lines[i][j] == teleport_two.display:
                lines[i][j] = teleport_two
            elif lines[i][j] == teleport_three.display:
                lines[i][j] = teleport_three
            elif lines[i][j] == teleport_four.display:
                lines[i][j] = teleport_four
            elif lines[i][j] == teleport_five.display:
                lines[i][j] = teleport_five
            elif lines[i][j] == teleport_six.display:
                lines[i][j] = teleport_six
            elif lines[i][j] == teleport_seven.display:
                lines[i][j] = teleport_seven
            elif lines[i][j] == teleport_eight.display:
                lines[i][j] = teleport_eight
            elif lines[i][j] == teleport_nine.display:
                lines[i][j] = teleport_nine

            j = j + 1
        i = i + 1

    return lines
Esempio n. 19
0
def parse(lines):
    """
    Transform the input into a grid.

    Arguments:
        lines -- list of strings representing the grid

    Returns:
        list -- contains list of lists of Cells
    """
    i = 0
    num_of_Xs = 0
    num_of_Ys = 0
    teleport_pads = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

    ## Changing the string objects to their respective cell object ##
    list_of_lists = []
    while i < len(lines):
        listCopy = []
        for char in lines[i]:
            if char == ' ':
                char = Air(char)
                listCopy.append(char)
            elif char == '*':
                char = Wall(char)
                listCopy.append(char)
            elif char == 'W':
                char = Water(char)
                listCopy.append(char)
            elif char == 'F':
                char = Fire(char)
                listCopy.append(char)
            elif char == 'X':
                char = Start(char)
                listCopy.append(char)
                num_of_Xs += 1  ## A counter to check that there is only 1 starting point ##
            elif char == 'Y':
                char = End(char)
                listCopy.append(char)
                num_of_Ys += 1  ## A counter to check that there is only 1 ending point ##
            elif char in teleport_pads:
                char = Teleport(char)
                listCopy.append(char)
            elif char == '\n':
                pass
            else:
                raise ValueError(
                    "Bad letter in configuration file: {}.".format(char))
        list_of_lists.append(listCopy)
        i += 1

    if num_of_Xs != 1:
        raise ValueError(
            "Expected 1 starting position, got {}.".format(num_of_Xs))
    if num_of_Ys != 1:
        raise ValueError(
            "Expected 1 ending position, got {}.".format(num_of_Ys))

    ## Checking that any teleport pads are in pairs ##
    teleport_pads = []
    for inner_list in list_of_lists:
        for cell in inner_list:
            try:
                if int(cell.display) in range(1, 10):
                    teleport_pads.append(cell.display)
            except:
                continue
    for pad in teleport_pads:
        if teleport_pads.count(pad) != 2:
            raise ValueError(
                'Teleport pad {} does not have an exclusively matching pad.'.
                format(pad))

    return list_of_lists


# print(parse(read_lines('small_board.txt')))
Esempio n. 20
0
from cells import (
    Start,
    End,
    Air,
    Wall,
    Fire,
    Water,
    Teleport
)

start = Start()
end = End()
air = Air()
water = Water()
fire = Fire()
wall = Wall()
teleport = Teleport(1)
def test_cells():
    assert start.display == "X", "Test Start Display failed!"
    print("Test Start Display passed!")

    assert end.display == "Y", "Test End Display failed!"
    print("Testt End Display passed!")

    assert air.display == " ", "Test Air Display failed!"
    print("Test Air Display passed!")

    assert water.display == "W", "Test Water Display failed!"
    print("Test Water Display passed!")

    assert fire.display == "F", "Test Fire Display failed!"