-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.py
127 lines (94 loc) · 3.93 KB
/
room.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from exceptions import *
from random import *
from hero import Beast
import utils
allDirections = ["left", "right", "back", "front" ]
oppositeDirections = {"left": "right", "right": "left", "back": "front",
"front": "back"}
class Wall:
def __init__ (self):
pass
class Door(Wall):
def __init__(self, state):
self._state = state
def get_state(self):
return self._state
def set_state(self, state):
self._state = state
class PossibleBeast:
def __init__(self, beast, probability = 0):
self.beast = beast
self.appearanceProb = probability
class PossibleThing:
def __init__(self, thing, probability = 0):
self.thing = thing
self.appearanceProb = probability
class Room:
def __init__(self, allBeasts = {}, allThings = {} ):
self.coordinates = (0, 0)
self.greeting = ""
self.direction = {}
for side in allDirections:
self.direction[side] = None
self.allThings = allThings
self.allBeasts = allBeasts
self.possibleThings = []
self.possibleBeasts = []
self.thingsInRoom = [] # Оставленные вещи
self.beastsInRoom = [] # Оставленные существа
self.firstEnter = True
def add_thing_into_room(self, thing):
self.possibleThings.append( thing )
def get_chose(self, chose):
print("Function get_chose (",chose, ")")
def read_from_file(self, fileStream):
for line in fileStream:
try:
words = utils.split_by_words(line)
except ConfFileError as e:
msg = "Ошибка при чтении описания комнаты в строке '" + line + \
"': " + e.what()
raise ConfFileError(msg)
if not words:
continue
if words[0] == "#Coordinates":
self.coordinates = (int(words[1]), int(words[2]))
elif words[0] == "#Wall":
self.set_walls(words[1])
elif words[0] == "#Thing":
self.check_thing (words[1])
self.possibleThings.append( PossibleThing( self.allThings[words[1]], probability = int(words[2])) )
elif words[0] == "#Beast":
self.check_beast(words[1])
self.possibleBeasts.append( PossibleBeast( self.allBeasts[words[1]], probability = int(words[2])) )
elif words[0] == "#Greeting":
self.greeting = utils.read_until_empty_line( line[len(words[0]):], fileStream )
elif words[0] == "#EndRoom":
return
def set_walls(self, walls: str):
if 'l' in walls:
self.direction["left"] = Wall()
if 'r' in walls:
self.direction["right"] = Wall()
if 'f' in walls:
self.direction["front"] = Wall()
if 'b' in walls:
self.direction["back"] = Wall()
def activate(self):
print (self.greeting)
for curBeast in self.possibleBeasts:
if random()*100. < curBeast.appearanceProb:
self.beastsInRoom.append(curBeast.beast)
for curThing in self.possibleThings:
if random()*100. < curThing.appearanceProb:
self.thingsInRoom.append(curThing.thing)
def check_thing(self, thingName):
if thingName not in self.allThings.keys():
msg = "Ошибка. Нет конфигурации для вещи '" + thingName + "' " \
+ "в комнате " + str(self.coordinates)
raise ConfFileError(msg)
def check_beast(self, beastName):
if beastName not in self.allBeasts.keys():
msg = "Ошибка. Нет конфигурации для вещи '" + beastName + "' " \
+ "в комнате " + str(self.coordinates)
raise ConfFileError(msg)