Skip to content

Commit 36d5919

Browse files
committed
Adding a random arena layout.
1 parent de8614b commit 36d5919

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

game/obj/arena.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Defines the arena container class."""
22

33
import math
4+
import random
45
from .block import Block
56

67
class Arena:
@@ -16,6 +17,7 @@ class Arena:
1617
2 -- four columns near corners
1718
3 -- wall with a central passage
1819
4 -- plus sign
20+
5 -- randomized
1921
"""
2022

2123
#-------------------------------------------------------------------------
@@ -26,7 +28,7 @@ def get_names():
2628
"""
2729

2830
return ["Empty Arena", "Central Column Arena", "Corner Column Arena",
29-
"Doorway Arena", "Plus-Shaped Arena"]
31+
"Doorway Arena", "Plus-Shaped Arena", "Randomized Arena"]
3032

3133
#-------------------------------------------------------------------------
3234

@@ -117,6 +119,9 @@ def __init__(self, game, size=(800, 800), layout=0):
117119
elif layout == 4:
118120
# Plus sign
119121
self._plus_blocks()
122+
elif layout == 5:
123+
# Randomized
124+
self._random_blocks()
120125

121126
#-------------------------------------------------------------------------
122127

@@ -186,6 +191,62 @@ def _plus_blocks(self):
186191
self._blocks.append(Block(self.game, math.floor(self.size[0]/3),
187192
math.ceil(2*self.size[0]/3),
188193
(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
189250

190251
#-------------------------------------------------------------------------
191252

game/obj/block.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, game, left, right, bottom, top, col="black"):
2828
Requires the following positional arguments:
2929
game (tcgame.TurtleCombatGame) -- game driver object
3030
left (int) -- smallest x-coordinate (px)
31-
right (int) -- lartst x-coordinate (px)
31+
right (int) -- largest x-coordinate (px)
3232
bottom (int) -- smallest y-coordinate (px)
3333
top (int) -- largest y-coordinate (px)
3434

0 commit comments

Comments
 (0)