[READ-ONLY] Mirror of https://github.com/mrgnw/python-scripts.
0

Configure Feed

Select the types of activity you want to include in your feed.

python-scripts / proj13.py
2.5 kB 111 lines
1from copy import deepcopy 2import os.path 3 4 5 6def read_map(fname): 7 rows = [] 8 9 if os.path.exists(fname): 10 with open(fname) as f: 11 rows = f.readlines() 12 13 w = len(rows[0])-1 14 #print w 15 16 h = len(rows) 17 #print h 18 #print rows 19 else: 20 # TODO: Generate a blank map when file does not exist 21 print fname, 'does not exist' 22 23 #for line in enumerate(map_gen()): 24 # rows[i] = line 25 26 return rows 27 28 29def map_string(map_list): 30 if map_list == None: 31 return "map_list is None" 32 33 row = "" 34 #print "* stringinate *" 35 36 for i in map_list: 37 for c in i: 38 row += c 39 #row += "\n" 40 41 return row 42 43 44# BASE: char == old_char 45# work: char = new_char 46# BASE: flood_fill (x+1, y+1) / (x-1, y-1) / (x+1, y-1) / (x-1, y+1) 47def flood_fill(map, x = 0, y = 0, old =".", new = "*"): 48 if x < 0 or x > len(map[0])-1: 49 return 50 if y < 0 or y > len(map)-1: 51 return 52 53 54 if not map[y][x] == old: 55 return map# STOP - Base case 56 57 # CHANGE THE STRING AT POSITION 58 if map[y][x] == old: 59 #print map[y] 60 map[y] = map[y][:x] + new + map[y][x+1:] 61 #print map_string(map) 62 63 #print map[y][x] 64 65 flood_fill(map, x+1, y, old, new) # right 66 flood_fill(map, x-1, y, old, new) # left 67 flood_fill(map, x, y+1, old, new) # up 68 flood_fill(map, x, y-1, old, new) # down 69 70 71class Map(object): 72 73 def __init__(self, filename='mapfile1.txt'): 74 self.rows = read_map(filename) 75 76 def __str__(self): 77 return map_string(self.rows) 78 79 def reset(self): 80 pass 81 82 def fill(self, x = 0, y = 0): 83 return flood_fill(self.rows, x, y) 84 85 def rotate(self): 86 ''' rotates the map 90 degrees clockwise. 87 Makes row 1 into column (last) 88 ''' 89 pass 90 91 #if __name__ == '__main__': 92 # single_room_map = Map("mapfile1.txt") 93 # multi_room_map = Map("mapfile2.txt") 94 # single_room_map.flood_fill(5,6) # Prints the filled single-room map. 95 # single_room_map.flood_fill(3,2) # Printed again from a different point. 96 # multi_room_map.flood_fill(5,6) 97 # multi_room_map.flood_fill(3,2) 98 99 100 101treasure_map = Map('text_map.txt') 102blank = Map('mapfile1.txt') 103 104print 105print treasure_map 106#flood_fill(treasure_map.rows) 107treasure_map.fill() 108print treasure_map 109 110treasure_map.fill(8,4) 111print treasure_map