-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrid_gen2.py
60 lines (46 loc) · 1.88 KB
/
grid_gen2.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
import string
import random
#Creating 15x15 grid with '-' as the placeholders
width = 10
height = 10
grid = [['-' for i in range(0, width)] for j in range(0, height)]
def generator(word_list):
k=0 #Number of completed iterations
while(k<len(word_list)):
m = 0 #flag bit to keep track of number of characters placed
word = word_list[k]
d = random.choice([[1, 0], [0, 1], [1, 1]]) #To choose between horizontal, vertical and diagonal placement
xsize = width if d[0] == 0 else width - len(word)
ysize = height if d[1] == 0 else height - len(word)
x = random.randrange(0, xsize)
y = random.randrange(0, ysize)
for i in range(0, len(word)):
x_coordinate = y + d[1]*i
y_coordinate = x + d[0]*i
if grid[x_coordinate][y_coordinate] == '-' or grid[x_coordinate][y_coordinate] == word[i]:
grid[x_coordinate][y_coordinate] = word[i]
m += 1
else:
break
if (m==len(word)):
print(word,"==",[x+1, y+1])
k += 1
def main():
# To get words from a file
# file = open(r"words.txt", "r")
# sentence = file.read()
# file.close()
# word_list = sentence.split()
word_list = ["HELLO","FIRE","NOOB","PLANETS","EARTH","MARS","VENUS"]
word_list.sort()
for i in range(0, len(word_list)):
x = word_list[i]
word_list[i] = random.choice([x, x[::-1]]) #To choose between normal and reverse string
generator(word_list)
for i in range(0, width):
for j in range(0,height):
if grid[i][j] == '-':
grid[i][j] = random.choice(string.ascii_uppercase) #Filing the empty spots with characters
print("\n".join(map(lambda row: " ". join(row), grid)))
#print("],\n[".join(map(lambda row: '","'. join(row), grid)))
main()