-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
107 lines (84 loc) · 2.75 KB
/
utils.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
GAME_LEVELS = {
# dungeon layout: max moves allowed
"game1.txt": 7,
"game2.txt": 12,
"game3.txt": 19,
}
PLAYER = "O"
KEY = "K"
DOOR = "D"
WALL = "#"
MOVE_INCREASE = "M"
SPACE = " "
DIRECTIONS = {
"W": (-1, 0),
"S": (1, 0),
"D": (0, 1),
"A": (0, -1)
}
INVESTIGATE = "I"
QUIT = "Q"
HELP = "H"
VALID_ACTIONS = [INVESTIGATE, QUIT, HELP]
VALID_ACTIONS.extend(list(DIRECTIONS.keys()))
HELP_MESSAGE = f"Here is a list of valid actions: {VALID_ACTIONS}"
INVALID = """That's invalid."""
WIN_TEXT = "You have won the game with your strength and honour!"
LOSE_TEST = "You have lost all your strength and honour."
class Display:
"""Display of the dungeon."""
def __init__(self, game_information, dungeon_size):
"""Construct a view of the dungeon.
Parameters:
game_information (dict<tuple<int, int>: Entity): Dictionary
containing the position and the corresponding Entity
dungeon_size (int): the width of the dungeon.
"""
self._game_information = game_information
self._dungeon_size = dungeon_size
def display_game(self, player_pos):
"""Displays the dungeon.
Parameters:
player_pos (tuple<int, int>): The position of the Player
"""
dungeon = ""
for i in range(self._dungeon_size):
rows = ""
for j in range(self._dungeon_size):
position = (i, j)
entity = self._game_information.get(position)
if entity is not None:
char = entity.get_id()
elif position == player_pos:
char = PLAYER
else:
char = SPACE
rows += char
if i < self._dungeon_size - 1:
rows += "\n"
dungeon += rows
print(dungeon)
def display_moves(self, moves):
"""Displays the number of moves the Player has left.
Parameters:
moves (int): THe number of moves the Player can preform.
"""
print(f"Moves left: {moves}" + "\n")
def load_game(filename):
"""Create a 2D array of string representing the dungeon to display.
Parameters:
filename (str): A string representing the name of the level.
Returns:
(list<list<str>>): A 2D array of strings representing the
dungeon.
"""
dungeon_layout = []
with open(filename, 'r') as file:
file_contents = file.readlines()
for i in range(len(file_contents)):
line = file_contents[i].strip()
row = []
for j in range(len(file_contents)):
row.append(line[j])
dungeon_layout.append(row)
return dungeon_layout