-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoardManager.py
More file actions
205 lines (169 loc) · 6.97 KB
/
BoardManager.py
File metadata and controls
205 lines (169 loc) · 6.97 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
class BoardManager:
def __init__(self, map_path:str):
with open(map_path, 'r') as map:
file_date = map.readlines()
self.set_map(file_date)
self.game_over:bool = False
self.hot_item = 0
self.cool_item = 0
self.go_reason = ""
def set_map(self,file_date):
self.map_data = []
for file_line in file_date:
if file_line.startswith('N'): #Nの行の処理
pass
if file_line.startswith('T'): #Tの行の処理
self.turn = int(file_line.split(":")[1])
if file_line.startswith('S'): #Sの行の処理
#座標系が違うので反転
self.map_size = list(map(int,file_line.split(":")[1].split(",")))
self.map_size.reverse()
if file_line.startswith('D'): #Dの行の処理
self.map_data.append(list(map(int,file_line.split(":")[1].split(","))))
if file_line.startswith('H'): #Hの行の処理
#座標系が違うので反転
self.hot_position = list(map(int,file_line.split(":")[1].split(",")))
self.hot_position.reverse()
if file_line.startswith('C'): #Cの行の処理
#座標系が違うので反転
self.cool_position = list(map(int,file_line.split(":")[1].split(",")))
self.cool_position.reverse()
def get_map_str(self):
return "\n".join([",".join(map(str,line)) for line in self.map_data])
def get_ready(self, character):
if character == "hot":
position = self.hot_position
enemy_position = self.cool_position
if character == "cool":
position = self.cool_position
enemy_position = self.hot_position
if self.game_over:
return [0 for i in range(10)]
if self.refer_map([position[0] - 1,position[1]]) == 2 and self.refer_map([position[0],position[1] + 1]) == 2 and self.refer_map([position[0] + 1,position[1]]) == 2 and self.refer_map([position[0],position[1] - 1]) == 2:
print(f"{character} around block ;;")
self.go_reason = f"{character} around block"
self.game_over = True
return [0 for i in range(10)]
if position[0] < 0 or position[1] < 0 or position[0] >= self.map_size[0] or position[1] >= self.map_size[1] or self.map_data[position[0]][position[1]] == 2: #2は壁
print(f"{character} fill up ;;")
self.go_reason = f"{character} fill up"
self.game_over = True
return [0 for i in range(10)]
return self.around_data(position,enemy_position)
def around_data(self,position,enemy_position):
data = [1]
for y in [-1,0,1]:
for x in [-1,0,1]:
data.append(self.refer_map([position[0] + y,position[1] + x],enemy_position))
return data
def refer_map(self, position,enemy_position=None):
if position[0] < 0 or position[1] < 0 or position[0] >= self.map_size[0] or position[1] >= self.map_size[1]:
return 2
else:
if position == enemy_position:
return 1
return self.map_data[position[0]][position[1]]
def walk(self, character, direction):
if character == "hot":
position = self.hot_position
point = self.hot_item
if character == "cool":
position = self.cool_position
point = self.cool_item
dx = 0
dy = 0
if direction == "u":
dy = -1
if direction == "d":
dy = 1
if direction == "r":
dx = 1
if direction == "l":
dx = -1
position = [position[0] + dy, position[1] + dx]
if self.map_data[position[0]][position[1]] == 3:
self.map_data[position[0]][position[1]] = 0
self.map_data[position[0] - dy][position[1] - dx] = 2
point += 1
if character == "hot":
self.hot_position = position
self.hot_item = point
if character == "cool":
self.cool_position = position
self.cool_item = point
return self.get_ready(character)
def look(self, character, direction):
if character == "hot":
position = self.hot_position
enemy_position = self.cool_position
if character == "cool":
position = self.cool_position
enemy_position = self.hot_position
if direction == "u":
data = self.around_data([position[0] - 2, position[1]],enemy_position)
if direction == "d":
data = self.around_data([position[0] + 2, position[1]],enemy_position)
if direction == "r":
data = self.around_data([position[0], position[1] + 2],enemy_position)
if direction == "l":
data = self.around_data([position[0], position[1] - 2],enemy_position)
return data
def search(self, character, direction):
if character == "hot":
position = self.hot_position
enemy_position = self.cool_position
if character == "cool":
position = self.cool_position
enemy_position = self.hot_position
x = 0
y = 0
if direction == "u":
x = -1
if direction == "d":
x = 1
if direction == "r":
y = 1
if direction == "l":
y = -1
data = [1]
for i in range(1,10):
data.append(self.refer_map([position[0] + i * x][position[0] + i * y],enemy_position))
return data
def put(self, character, direction):
if character == "hot":
position = self.hot_position
if character == "cool":
position = self.cool_position
dx = 0
dy = 0
if direction == "u":
dy = -1
if direction == "d":
dy = 1
if direction == "r":
dx = 1
if direction == "l":
dx = -1
if position[0] + dy < 0 or position[1] + dx < 0 or position[0] + dy >= self.map_size[0] or position[1] >= self.map_size[1] + dx:
return self.get_ready(character)
self.map_data[position[0] + dy][position[1] + dx] = 2
return self.get_ready(character)
def char_action(self, character:str, action:str):
if action.startswith("w"):
data = self.walk(character, action[1])
elif action.startswith("l"):
data = self.look(character, action[1])
elif action.startswith("s"):
data = self.search(character, action[1])
elif action.startswith("p"):
data = self.put(character, action[1])
else:
data = self.get_ready(character)
return data
def result(self):
if self.cool_item > self.hot_item:
return "cool","win"
elif self.hot_item > self.cool_item:
return "hot","win"
else:
return "","draw"