1
1
"""Defines the arena container class."""
2
2
3
3
import math
4
+ import random
4
5
from .block import Block
5
6
6
7
class Arena :
@@ -16,6 +17,7 @@ class Arena:
16
17
2 -- four columns near corners
17
18
3 -- wall with a central passage
18
19
4 -- plus sign
20
+ 5 -- randomized
19
21
"""
20
22
21
23
#-------------------------------------------------------------------------
@@ -26,7 +28,7 @@ def get_names():
26
28
"""
27
29
28
30
return ["Empty Arena" , "Central Column Arena" , "Corner Column Arena" ,
29
- "Doorway Arena" , "Plus-Shaped Arena" ]
31
+ "Doorway Arena" , "Plus-Shaped Arena" , "Randomized Arena" ]
30
32
31
33
#-------------------------------------------------------------------------
32
34
@@ -117,6 +119,9 @@ def __init__(self, game, size=(800, 800), layout=0):
117
119
elif layout == 4 :
118
120
# Plus sign
119
121
self ._plus_blocks ()
122
+ elif layout == 5 :
123
+ # Randomized
124
+ self ._random_blocks ()
120
125
121
126
#-------------------------------------------------------------------------
122
127
@@ -186,6 +191,62 @@ def _plus_blocks(self):
186
191
self ._blocks .append (Block (self .game , math .floor (self .size [0 ]/ 3 ),
187
192
math .ceil (2 * self .size [0 ]/ 3 ),
188
193
(self .size [1 ]/ 2 )- 30 , (self .size [1 ]/ 2 )+ 30 ))
194
+
195
+ #-------------------------------------------------------------------------
196
+
197
+ def _random_blocks (self ):
198
+ """Arena._random_blocks() -> None
199
+ Generates a randomized arena.
200
+
201
+ The randomized layout is made up of 3-7 blocks, arranged to have
202
+ 2-fold rotational symmetry about the center, and prohibited from
203
+ intersecting the turtles' starting coordinates.
204
+ """
205
+
206
+ # Initialize a random block height and width
207
+ h = random .randrange (10 , 151 )
208
+ w = random .randrange (10 , 151 )
209
+
210
+ # Decide whether to include a central block
211
+ if random .random () < 0.5 :
212
+
213
+ # Add central block
214
+ self ._blocks .append (Block (self .game , (self .size [0 ]/ 2 )- w ,
215
+ (self .size [0 ]/ 2 )+ w , (self .size [1 ]/ 2 )- h ,
216
+ (self .size [1 ]/ 2 )+ h ))
217
+
218
+ # Determine number of additional blocks on sides
219
+ num = random .randrange (1 , 4 )
220
+
221
+ # Generate side blocks
222
+ iter = 0 # iteration counter
223
+ while iter < num :
224
+
225
+ # Generate random dimensions and centers
226
+ h = random .randrange (10 , 121 )
227
+ w = random .randrange (10 , 121 )
228
+ cx = random .randrange (self .size [0 ]+ 1 )
229
+ cy = random .randrange (self .size [1 ]+ 1 )
230
+
231
+ # Generate tentative blocks
232
+ self ._blocks .append (Block (self .game , cx - w , cx + w , cy - h , cy + h ))
233
+ self ._blocks .append (Block (self .game , self .size [0 ]- cx - w ,
234
+ self .size [0 ]- cx + w , self .size [1 ]- cy - h ,
235
+ self .size [1 ]- cy + h ))
236
+
237
+ # Test whether the starting coordinates are free
238
+ if (self .blocked (self .get_p1_coords ()) or
239
+ self .blocked (self .get_p2_coords ())):
240
+
241
+ # If not, delete the tentative blocks and retry
242
+ del self ._blocks [- 1 ]
243
+ del self ._blocks [- 1 ]
244
+ continue
245
+
246
+ else :
247
+
248
+ # Otherwise increment the counter
249
+ iter += 1
189
250
190
251
#-------------------------------------------------------------------------
191
252
0 commit comments