-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.py
89 lines (85 loc) · 3.24 KB
/
world.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
from room import Room
import random
import math
class World:
def __init__(self):
self.starting_room = None
self.rooms = {}
self.room_grid = []
self.grid_size = 0
def load_graph(self, room_graph):
num_rooms = len(room_graph)
rooms = [None] * num_rooms
grid_size = 1
for i in range(0, num_rooms):
x = room_graph[i][0][0]
grid_size = max(grid_size, room_graph[i][0][0], room_graph[i][0][1])
self.rooms[i] = Room(f"Room {i}", f"({room_graph[i][0][0]},{room_graph[i][0][1]})",i, room_graph[i][0][0], room_graph[i][0][1])
self.room_grid = []
grid_size += 1
self.grid_size = grid_size
for i in range(0, grid_size):
self.room_grid.append([None] * grid_size)
for room_id in room_graph:
room = self.rooms[room_id]
self.room_grid[room.x][room.y] = room
if 'n' in room_graph[room_id][1]:
self.rooms[room_id].connect_rooms('n', self.rooms[room_graph[room_id][1]['n']])
if 's' in room_graph[room_id][1]:
self.rooms[room_id].connect_rooms('s', self.rooms[room_graph[room_id][1]['s']])
if 'e' in room_graph[room_id][1]:
self.rooms[room_id].connect_rooms('e', self.rooms[room_graph[room_id][1]['e']])
if 'w' in room_graph[room_id][1]:
self.rooms[room_id].connect_rooms('w', self.rooms[room_graph[room_id][1]['w']])
self.starting_room = self.rooms[0]
def print_rooms(self):
rotated_room_grid = []
for i in range(0, len(self.room_grid)):
rotated_room_grid.append([None] * len(self.room_grid))
for i in range(len(self.room_grid)):
for j in range(len(self.room_grid[0])):
rotated_room_grid[len(self.room_grid[0]) - j - 1][i] = self.room_grid[i][j]
print("#####")
str = ""
for row in rotated_room_grid:
all_null = True
for room in row:
if room is not None:
all_null = False
break
if all_null:
continue
# PRINT NORTH CONNECTION ROW
str += "#"
for room in row:
if room is not None and room.n_to is not None:
str += " | "
else:
str += " "
str += "#\n"
# PRINT ROOM ROW
str += "#"
for room in row:
if room is not None and room.w_to is not None:
str += "-"
else:
str += " "
if room is not None:
str += f"{room.id}".zfill(3)
else:
str += " "
if room is not None and room.e_to is not None:
str += "-"
else:
str += " "
str += "#\n"
# PRINT SOUTH CONNECTION ROW
str += "#"
for room in row:
if room is not None and room.s_to is not None:
str += " | "
else:
str += " "
str += "#\n"
print(str)
print("#####")