-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapeGenerator.py
55 lines (45 loc) · 1.71 KB
/
shapeGenerator.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
import random
BLOCK = "■"
EMPTY = " "
def genereate(blockCount=4, areaSize=4):
maxI = areaSize-1
def getValidCords(cord: tuple[int, int]) -> list[tuple[int, int]]:
y = cord[0]
x = cord[1]
validCords = []
if y > 0:
if (y-1, x) not in blocks:
validCords.append((y-1, x))
if y < maxI:
if (y+1, x) not in blocks:
validCords.append((y+1, x))
if x > 0:
if (y, x-1) not in blocks:
validCords.append((y, x-1))
if x < maxI:
if (y, x+1) not in blocks:
validCords.append((y, x+1))
return validCords
if blockCount > areaSize**2:
raise Exception("blockCount is greater than content of the area")
blocks = [(areaSize//2, areaSize//2)]
choices = [(areaSize//2, areaSize//2)]
for _ in range(blockCount-1):
while True:
choisedCord = random.choice(choices)
validCords = getValidCords(choisedCord)
if len(validCords) == 0:
choices.remove(choisedCord)
else:
if len(validCords) == 1:
choices.remove(choisedCord)
newcords = random.choice(validCords)
blocks.append(newcords)
choices.append(newcords)
break
minY = min([block[0] for block in blocks])
maxY = max([block[0] for block in blocks])
minX = min([block[1] for block in blocks])
maxX = max([block[1] for block in blocks])
shape = [[BLOCK if (y, x) in blocks else EMPTY for x in range(minX, maxX+1)] for y in range(minY, maxY+1)]
return shape