-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
211 lines (182 loc) · 6.12 KB
/
app.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
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
206
207
208
209
210
211
from flask import Flask, render_template, request
from random import randrange, choice
from copy import deepcopy
app = Flask(__name__)
size = 20
word_list = []
def horizontal_forward(word):
""" Place a word horizontally, from left to right """
attempt = 0
while attempt < 10:
attempt += 1
collision = False
y = randrange(0, size)
x = randrange(0, size - len(word) + 1)
for i in range(len(word)):
if grid[y][x + i] != '_' and grid[y][x + i] != word[i]:
collision = True
break
if collision is False:
break
if attempt == 10:
return False
for count, letter in enumerate(word):
grid[y][x + count] = letter
return True
def horizontal_backward(word):
""" Place a word horizontally, from right to left"""
attempt = 0
while attempt < 10:
attempt += 1
collision = False
y = randrange(0, size)
x = randrange(len(word), size)
for i in range(len(word)):
if grid[y][x - i] != '_' and grid[y][x - i] != word[i]:
collision = True
break
if collision is False:
break
if attempt == 10:
return False
for count, letter in enumerate(word):
grid[y][x - count] = letter
return True
def vertical_down(word):
""" Place a word vertically, from top to bottom """
attempt = 0
while attempt < 10:
attempt += 1
collision = False
y = randrange(0, size - len(word) + 1)
x = randrange(0, size)
for i in range(len(word)):
if grid[y + i][x] != '_' and grid[y + i][x] != word[i]:
collision = True
break
if collision is False:
break
if attempt == 10:
return False
for count, letter in enumerate(word):
grid[y + count][x] = letter
return True
def vertical_up(word):
""" Place a word vertically, from bottom to top """
attempt = 0
while attempt < 10:
attempt += 1
collision = False
y = randrange(len(word), size)
x = randrange(0, size)
for i in range(len(word)):
if grid[y - i][x] != '_' and grid[y - i][x] != word[i]:
collision = True
break
if collision is False:
break
if attempt == 10:
return False
for count, letter in enumerate(word):
grid[y - count][x] = letter
return True
def diagonal_up(word):
""" Place a word diagonally, from bottom-left to top-right """
attempt = 0
while attempt < 10:
attempt += 1
collision = False
y = randrange(len(word), size)
x = randrange(0, size - len(word) + 1)
for i in range(len(word)):
if grid[y - i][x + i] != '_' and grid[y - i][x + i] != word[i]:
collision = True
break
if collision is False:
break
if attempt == 10:
return False
for count, letter in enumerate(word):
grid[y - count][x + count] = letter
return True
def diagonal_down(word):
""" Place a word diagonally, from top-left to bottom-right"""
attempt = 0
while attempt < 10:
attempt += 1
collision = False
y = randrange(0, size - len(word) + 1)
x = randrange(0, size - len(word) + 1)
for i in range(len(word)):
if grid[y + i][x + i] != '_' and grid[y + i][x+ i] != word[i]:
collision = True
break
if collision is False:
break
if attempt == 10:
return False
for count, letter in enumerate(word):
grid[y + count][x + count] = letter
return True
def randomizer():
"""Randomly choose a placement function, weighted by placement_functions. If placement fails, return False"""
placement_functions = [vertical_down] * 4 + [vertical_up] * 1 + [diagonal_up] * 2 + [diagonal_down] * 2 + [horizontal_backward] * 1 + [horizontal_forward] * 4
for word in word_list:
status = choice(placement_functions)(word)
if status is False:
return False
return True
def fill_in_the_blanks():
""" Fill in random letters for empty squares in grid"""
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for i in range(size):
for j in range(size):
if grid[i][j] == '_':
grid[i][j] = choice(alpha)
def initialize_grid():
""" Replace all letters in grid with '_' """
for i in range(size):
for j in range(size):
grid[i][j] = '_'
def populate_puzzle(new_word_list):
""" Populate the grid and answer_grid """
if 'grid' in globals():
initialize_grid()
else:
global grid
grid = [['_' for i in range(size)] for j in range(size)]
word_list.clear()
word_list.extend(new_word_list)
status = randomizer()
if status is False:
return False
global answer_grid
answer_grid = deepcopy(grid)
fill_in_the_blanks()
return True
@app.route('/', methods=['POST', 'GET'])
def input_words():
""" Take words from user and return links to a
word search puzzle and answer key, or an error message"""
if request.method == 'POST':
words = request.form.get('words').upper()
new_word_list = words.split()
for word in new_word_list:
if len(word) > size:
return render_template('error.html')
status = populate_puzzle(sorted(new_word_list, key=len, reverse=True))
if status is False:
return render_template('error_2.html')
return render_template('home2.html')
return render_template('home.html')
@app.route('/puzzle', strict_slashes=False)
def puzzle():
""" Get the generated word search puzzle """
return render_template('puzzle.html', puzzle=grid, words=word_list)
@app.route('/answer', strict_slashes=False)
def answer():
""" Get the generated answer key s"""
return render_template('puzzle.html', puzzle=answer_grid, words=word_list)
""" disable app.run for deployment with gunicorn """
# if __name__ == '__main__':
# app.run(host='0.0.0.0', port='8000', debug='true')