-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ1.py
More file actions
87 lines (73 loc) · 2.85 KB
/
Q1.py
File metadata and controls
87 lines (73 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def valid_index(size, pos):
"""valid_index check if a given index is located inside the cave"""
return True if -1 < pos[0] < size and -1 < pos[1] < size else False
def build_cave(data):
"""build_cave returns a matrix representation of a cave
data: a dictionary of features in the cave"""
# List to check for duplicate coordinates
coordinates_list = []
# Check for a size
if 'size' not in data:
return None
# Our cave to return
cave = [['.'] * data['size'] for _ in range(data['size'])]
# Check for an entrance
if 'entrance' not in data or not isinstance(data['entrance'], tuple):
return None
else:
if not valid_index(data['size'], data['entrance']):
return None
coordinates_list.append(data['entrance'])
cave[data['entrance'][0]][data['entrance'][1]] = '@'
# Check for an exit
if 'exit' not in data or not isinstance(data['exit'], tuple):
return None
else:
if not valid_index(data['size'], data['exit']):
return None
coordinates_list.append(data['exit'])
cave[data['exit'][0]][data['exit'][1]] = 'X'
# Check for a dragon
if 'dragon' in data:
if not isinstance(data['dragon'], tuple):
return None
elif not valid_index(data['size'], data['dragon']):
return None
# If dragon is adjacent to the entrance
elif (data['entrance'][0] - 1 <= data['dragon'][0] <=
data['entrance'][0] + 1 and data['entrance'][1] - 1 <=
data['dragon'][1] <= data['entrance'][1] + 1):
return None
coordinates_list.append(data['dragon'])
cave[data['dragon'][0]][data['dragon'][1]] = 'W'
# Check for a sword
if 'sword' in data:
if not isinstance(data['sword'], tuple):
return None
if not valid_index(data['size'], data['sword']):
return None
coordinates_list.append(data['sword'])
cave[data['sword'][0]][data['sword'][1]] = 't'
# Check for 3 or fewer treasures
if 'treasure' in data:
if len(data['treasure']) > 3 or not isinstance(data['treasure'], list):
return None
for coord in data['treasure']:
if not valid_index(data['size'], coord):
return None
coordinates_list.append(coord)
cave[coord[0]][coord[1]] = '$'
# Check for walls
if 'walls' in data:
if not isinstance(data['walls'], list):
return None
for coord in data['walls']:
if not valid_index(data['size'], coord):
return None
coordinates_list.append(coord)
cave[coord[0]][coord[1]] = '#'
# Check for conflicting coordinates
for coordinate in coordinates_list:
if coordinates_list.count(coordinate) > 1:
return None
return cave