def test_player_portal_block_step(): player = Player() path = Path(0, 0) portal = Portal(0, 1) path.step(player) assert player.is_finished == False portal.step(player) assert player.is_finished == True
def create_end_point(self): os.system('cls' if os.name == 'nt' else 'clear') player = Player() self.field.enter(player) print(self.field.render()) print() selection = input( "Enter the coordinate of the item (e.g row, col)\n'B' to return to configure menu.\n'M' to return to main menu. " ).lower() if not (selection == 'm' or selection == 'b'): try: split = selection.split(',') row = int(split[0]) col = int(split[1]) block = self.field.blocks[row - 1][col - 1] if not isinstance(block, Path): print(f"Cannot place an end point on non-passageways!") input("Press Enter to continue...") return if block == self.field.start_block: print(f"Cannot place an end point on the start block!") input("Press Enter to continue...") return blocks = [block for row in self.field.blocks for block in row] portals = [ block for block in blocks if isinstance(block, Portal) ] for portal in portals: self.field.blocks[portal.y][portal.x] = Path( portal.x, portal.y) self.field.blocks[row - 1][col - 1] = Portal(col - 1, row - 1) self.field.enter(player) os.system('cls' if os.name == 'nt' else 'clear') self.field.enter(Player()) print(self.field.render()) print() print(f"End Point block placed at {row}, {col}") input("Press Enter to continue...") except: print("Invalid block coordinates!") input("Press Enter to continue...") elif selection == 'm': self.end()
def load_maze(self): os.system('cls' if os.name == 'nt' else 'clear') file_name = input("Enter file name (without extension): ") try: csv_file = open(f'{file_name}.csv') csv_reader = csv.reader(csv_file, delimiter=',') csv_data = list(csv_reader) blocks = [[] for _ in range(sum(1 for row in csv_data))] start_block = None is_invalid = False csv_file.close() for i, row in enumerate(csv_data): for ii, col in enumerate(row): block = (Wall(ii, i) if col == 'X' else Portal(ii, i) if col == 'B' else Path(ii, i) if col == 'A' or col == 'O' else True) blocks[i].append(block) if col == 'A': start_block = block if block == True: is_invalid = True break if is_invalid: print( "Invalid file type/content, please ensure that the file is proper." ) input("Press Enter to continue...") return if start_block is None: print( "Maze does not contain a start point, please verify the maze file!" ) input("Press Enter to continue...") return print(f'Processed {len(blocks)} lines.') self.field = Field(blocks, start_block) self.player = Player() self.field.enter(self.player) print("\n" + self.field.render() + '\n') print("Successfully loaded maze!") input("Press Enter to continue...") except IOError: print("File does not exist.") input("Press Enter to continue...")
def test_field_move_portal(): left_top = Path(0, 0) right_top = Portal(1, 0) left_bottom = Path(0, 1) right_bottom = Path(1, 1) blocks = [[left_top, right_top], [left_bottom, right_bottom]] field = Field(blocks, left_top) player = Player() field.enter(player) field.move(player, Direction.right) assert player.block == right_top assert player.is_finished
def test_block_render(): assert Path(0, 0).render() == 'O' assert Wall(0, 0).render() == 'X' assert Portal(0, 0).render() == 'B'