-
Notifications
You must be signed in to change notification settings - Fork 2
/
crossword_website_final.html
1148 lines (913 loc) · 48.6 KB
/
crossword_website_final.html
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!-- /$$$$$$ /$$ -->
<!-- /$$__ $$ | $$ -->
<!-- | $$ \__/ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$$ /$$ /$$ /$$ /$$$$$$ /$$$$$$ /$$$$$$$ -->
<!-- | $$ /$$__ $$ /$$__ $$ /$$_____//$$_____/| $$ | $$ | $$ /$$__ $$ /$$__ $$ /$$__ $$ -->
<!-- | $$ | $$ \__/| $$ \ $$| $$$$$$| $$$$$$ | $$ | $$ | $$| $$ \ $$| $$ \__/| $$ | $$ -->
<!-- | $$ $$| $$ | $$ | $$ \____ $$\____ $$| $$ | $$ | $$| $$ | $$| $$ | $$ | $$ -->
<!-- | $$$$$$/| $$ | $$$$$$/ /$$$$$$$//$$$$$$$/| $$$$$/$$$$/| $$$$$$/| $$ | $$$$$$$ -->
<!-- \______/ |__/ \______/ |_______/|_______/ \_____/\___/ \______/ |__/ \_______/ -->
<!-- -->
<!-- /$$$$$$ /$$ -->
<!-- /$$__ $$ | $$ -->
<!-- | $$ \__/ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ -->
<!-- | $$ /$$__ $$ /$$__ $$ |____ $$|_ $$_/ /$$__ $$ /$$__ $$ -->
<!-- | $$ | $$ \__/| $$$$$$$$ /$$$$$$$ | $$ | $$ \ $$| $$ \__/ -->
<!-- | $$ $$| $$ | $$_____/ /$$__ $$ | $$ /$$| $$ | $$| $$ -->
<!-- | $$$$$$/| $$ | $$$$$$$| $$$$$$$ | $$$$/| $$$$$$/| $$ -->
<!-- \______/ |__/ \_______/ \_______/ \___/ \______/ |__/ -->
<!-- This project was created by https://github.com/bskdany/ -->
<html >
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/brython@3.8.9/brython.min.js">
</script>
<script src="https://cdn.jsdelivr.net/npm/brython@3.8.9/brython_stdlib.js">
</script>
</head>
<div class="header">
<div class='headerText'>Crossword Creator</div>
<div class= 'subtitleText'>A simple tool to create a crossword with user given words</div>
<div class='logo'></div>
</div>
<hr style="width:80%", size="2", color=white>
<body onload="brython()" class='main'>
<center><div class='container' >
<div class='numbers' >
<div> </div>
<div> </div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
<div>13</div>
<div>14</div>
<div>15</div>
<div>16</div>
<div>17</div>
<div>18</div>
<div>19</div>
<div>20</div>
</div>
<div class = 'inpt'>
<center><h1>Words input</h1></center>
<input id = 'word1' ><br>
<input id = 'word2' ><br>
<input id = 'word3' ><br>
<input id = 'word4' ><br>
<input id = 'word5' ><br>
<input id = 'word6' ><br>
<input id = 'word7' ><br>
<input id = 'word8' ><br>
<input id = 'word9' ><br>
<input id = 'word10' ><br>
<input id = 'word11' ><br>
<input id = 'word12' ><br>
<input id = 'word13' ><br>
<input id = 'word14' ><br>
<input id = 'word15' ><br>
<input id = 'word16' ><br>
<input id = 'word17' ><br>
<input id = 'word18' ><br>
<input id = 'word19' ><br>
<input id = 'word20' ><br>
</div>
<div class='inpt2'>
<center><h1>Words definition</h1></center>
<input id = 'word1def' ><br>
<input id = 'word2def' ><br>
<input id = 'word3def' ><br>
<input id = 'word4def' ><br>
<input id = 'word5def' ><br>
<input id = 'word6def' ><br>
<input id = 'word7def' ><br>
<input id = 'word8def' ><br>
<input id = 'word9def' ><br>
<input id = 'word10def' ><br>
<input id = 'word11def' ><br>
<input id = 'word12def' ><br>
<input id = 'word13def' ><br>
<input id = 'word14def' ><br>
<input id = 'word15def' ><br>
<input id = 'word16def' ><br>
<input id = 'word17def' ><br>
<input id = 'word18def' ><br>
<input id = 'word19def' ><br>
<input id = 'word20def' ><br>
</div>
<div class='user_manual'>
<h1>How to use:</h1>
<div>Place the words you want to make a crossword with in the words input field,
then write a definition for those words in the words definition field, if the
cell is blank the definition will be equal to the word.<br>
The Crossword size option defines how big the crossword is, the size is the lenght
of each side of the crossword, if left blank it will use the value of 10.<br>
The how many crosswords option defines how many crosswords you want to create with the
given words, if left blank it will create only one crossword.<br>
The show words tick auto-completes the crossword.<br>
The time for the algorythm to run depends on many factors, like how many words you use and so on.<br>
If two words are the same, if there is an empty cell between two input fields
or if there is no way to place a word in the crossword the program won't work correctly.<br>
</div>
</div>
<div class='options' >
<div style='float: left;'>Options:</div>
<div style='float: left;'>Crossword size :<input id = 'size' maxlength="2" size='2'> </div>
<div style='float:left'>How many crosswords : <input id = 'how_many_crosswords' maxlength="2" size='2'> </div>
<div style='float: left; margin-left: 1px;' >Randomize crossword: <input type="checkbox" id="random_tick" ></div>
<button style='float: left;width:100px;height:30px;' id="submit">submit</button><div style='float: left; margin-left: 10px;' >Show words: <input type="checkbox" id="show_words" checked></div>
</div>
</div></center>
<center><div class='alert'>
<div class = 'error' id = 'error_msg' ></div>
<div class = 'error' id = 'error_msg_/' ></div>
<div class = 'error' id = 'error_msg_$' ></div>
<div class = 'error' id = 'error_msg_dup' ></div>
</div></center>
<div class = 'cycle'><center>
<div>Cycle between crosswords</div>
<button id="previous">< </button>
<button id="next"> ></button>
</center></div>
<center><div id='zone'> </div></center>
<center><div class='words_definitions'>
<div class='vrt_text'>Vertical</div>
<div class='hrz_text'>Horizzontal</div>
<div id = 'vrt' class='vrt_def'></div>
<div id = 'hrz' class='hrz_def'></div>
</div></center>
<div style="width: 100%;height: 400px;"></div>
</body>
<hr style="width:80%", size="2", color=white>
<footer style='width: 100%;height: 50px;color: #eeeeee;font-size: medium;text-align: center;'>
<center>Made with ❤️by <a href="https://github.com/bskdany" target="_blank" style='color: rgb(28, 120, 136);'>bskdany</a></center>
</footer>
<style>
.main{
background-color: #2e2d33;
}
*{
font-family: sans-serif;
font-weight: normal;
font-size: 1.1em;
}
table{
background-color: rgb(255, 253, 253);
text-align: center;
empty-cells: hide;
}
.header {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto auto;
grid-template-areas:
'x y .'
'x z .';
width: 100%;
background-color: #2e2d33;
top: 0;
margin: 0;
height:150px;
white-space: nowrap;
}
.logo{
grid-area: x;
margin-left: 5px;
top: 0;
background: url("https://i.ibb.co/Vvwkr5G/logo2.png") no-repeat;
width: 142px;
height: 150px;
float: left;
border: 0;
}
.headerText{
grid-area: y;
color: #eeeeee;
font-family: 'Trebuchet MS', sans-serif;
font-size: 380%;
text-align: center;
}
.subtitleText{
grid-area: z;
color: #eeeeee;
font-family: 'Trebuchet MS', sans-serif;
font-size: 100%;
text-align: center;
}
.numbers{
display: grid;
row-gap: 1fr;
grid-area: a;
}
.inpt{
grid-area: b;
}
.inpt2{
grid-area: c;
margin-left: 5px;
width: min-content ;
}
.options{
grid-area: d;
font-size: smaller;
white-space: nowrap;
height: min-content;
width: min-content;
align-items: flex-end;
border: 1px solid black;
padding: 3px;
line-height: 1.4;
margin-bottom: 0px;
margin-top: 30px;
}
.cycle{
margin-top: 10px;
margin-bottom: 10px;
font-size: smaller;
color: #eeeeee;
}
.vrt_text{
grid-area: aa;
border: 1px solid black;
padding: 10px;
}
.hrz_text{
grid-area: ab;
border: 1px solid black;
padding: 10px;
}
.vrt_def{
grid-area: ac;
border: 1px solid black;
padding: 10px;
text-align: left;
}
.hrz_def{
grid-area: ad;
border: 1px solid black;
padding: 10px;
text-align: left;
}
.words_definitions{
display: grid;
grid-template-columns:1fr 1fr;
grid-template-rows: min-content 80%;
grid-template-areas:
'aa ab'
'ac ad';
margin-top: 20px;
margin-left: 20%;
margin-right: 20%;
color: #eeeeee;
}
.user_manual{
grid-area: e;
text-align: left;
font-size: medium;
border: 1px solid black;
padding: 10px;
line-height: 1.3;
}
.container {
display: grid;
grid-template-columns: min-content min-content min-content 1fr ;
grid-template-areas:
'a b c e'
'a b c d';
grid-template-rows: auto auto;
margin-top: 70px;
width: min-content;
border: 1px solid black;
padding: 10px;
column-gap: 10px;
color: #eeeeee;
}
.alert{
color: #eeeeee;
margin-top: 5px;
font-size: medium;
}
</style>
<script type="text/python">
from browser import document, timer
from browser.html import TABLE, TR, TH, TD
import html
import itertools
import math
import random
#function that creates "the bone structure" of the crosswords
#it returns an array of arrays, the first array is an array
#of lenght crossword_size*crossword_size, so it's like an
#empty crossword
def new_array():
default_array =["/"] * crossword_size*crossword_size
temp_array = [[] for _ in range(30)] # creates a base for all the crosswords
temp_array[0].append(default_array) # the first empty crossword is placed in the first place of temp_array
return temp_array
#function that prints the finished array on the website
#in a crossword form
def print_array_site(array):
global show_words_var
coords = array[1] #array is divided in crossword and coordinates of the words
array = array[0] #that are placed inside of it
array1 = array.copy()
#placeholder symbols are removed from both arrays
counter = 0
while counter != len(array):
if array[counter] == '/' or array[counter] == '$':
array[counter] = ''
counter = counter +1
counter = 0
while counter != len(array1):
if array1[counter] == '/' or array1[counter] == '$':
array1[counter] = ''
counter = counter +1
#crossword_size is calculated
crossword_size = math.sqrt(len(array))
#adds a , between words indices if necessary
for coord in coords:
where = coord[1]
if str(array1[where]).isnumeric():
array1[where] = str(array1[where]) +','+ str(coord[0])
else:
array1[where] = coord[0]
#table is initialized
t = TABLE(cellspacing='0',border="2px")
x = 0
while x < crossword_size:
y = 0
while y < crossword_size:
#if show words is true the cells are placed normally
if show_words_var == True:
t <= TD(array[x*crossword_size+y],style={'height':31, 'width':31})
#if show words is False
else:
#if its a number it's placed top right of the cell
if str(array1[x*crossword_size+y]).isnumeric() or len(array1[x*crossword_size+y]) >1:
t <= TD(array1[x*crossword_size+y],style={'height':31, 'width':31,'font-size':'15px', 'vertical-align':'top', 'text-align':'left'})
#if it's a letter it's placed with white font
else:
t <= TD(array1[x*crossword_size+y],style={'height':31, 'width':31,'color':'white'})
y = y+1
t <= TR()
x = x +1
#table is sent to html
document['zone'].text = ''
document['zone']<= t
#the words definitions are sent to be printed
print_word_def_site(coords)
#event that happens when the submit button is pressed
def submit(event):
#global variables imported and initialized
global end_crosswords
global end_crosswords_counter
global word_list
global words_def
global how_many_crosswords
global crossword_size
global words
words_def = []
end_crosswords = []
end_crosswords_counter = 0
##if how_many_crosswords field is empty value is set to 1
if document['how_many_crosswords'].value == '':
how_many_crosswords = 1
else:
how_many_crosswords = document['how_many_crosswords'].value
how_many_crosswords = int(how_many_crosswords)
#if crossword_size field is empty value is set to 10
if document['size'].value == '':
crossword_size = 10
else:
crossword_size = document['size'].value
crossword_size = int(crossword_size)
#html fields cleared
document["how_many_crosswords"].clear()
document["size"].clear()
document["error_msg"].clear()
document["error_msg_/"].clear()
document["error_msg_$"].clear()
document["error_msg_dup"].clear()
#takes the words from the words input
words = []
counter = 1
while counter != 21:
id = 'word' + str(counter)
word = document[id].value
#exception
if len(word) <= crossword_size:
bad_word = False
for letter in word:
#searches for '/' and '$' inside the words because
#they are used as placeholders later
if letter =='/' :
#if found an alert is prompted
msg = "Ops, you cannot place the '/' sign in the word "+word+" \n"
document["error_msg_/"] <= (f"{msg}")
bad_word = True
if letter =='$' :
msg = "Ops, you cannot place the '$' sign in the word "+word+" \n"
document["error_msg_$"] <= (f"{msg}")
bad_word = True
#if everything is ok word is appended
if word != '' and bad_word == False:
words.append(document[id].value)
#alert for word bigger than crossword_size
else:
msg = "Ops, the word '"+ word+ "' cannot be longer than Crossword size\n"
document["error_msg"] <= (f"{msg}")
counter = counter+1
to_run = True
for i in words:
counter = 0
duplicate = False
first_appareance = False
while counter != len(words):
if i == words[counter]:
if first_appareance == False:
first_appareance = True
else:
duplicate = True
counter += 1
if duplicate == True:
msg = "Ops, the word '"+i+ "' appears to be used two or more times\n"
document["error_msg_dup"] <= (f"{msg}")
to_run = False
word_list = words
#words are capitalized
for i in range(len(words)):
words[i] = words[i].upper()
#words are sorted
if random_words_var:
random.shuffle(words)
words1 = words
else:
words1 = sorted(words, key=len,reverse=True)
#takes the words definition input
counter = 1
while counter != 21:
word_def = ''
id = 'word' + str(counter) +'def'
#if field is empty the word input is used as word definition
if document[id].value == '':
id = 'word' + str(counter)
words_def.append(document[id].value)
else:
words_def.append(document[id].value)
counter = counter+1
#if there is more than one word the main function is called
if len(words1) > 0 and to_run:
def dolore():
print("main function called")
main(words1,how_many_crosswords)
loading()
timer.set_timeout(dolore, 500)
def loading():
word = "LOADING"
first_word_coord = int(crossword_size/2)+crossword_size*int(crossword_size/2)-crossword_size
word_len = len(word)
i_position = int(first_word_coord - (word_len/2))
array = new_array()[0][0]
array = place_horizzontally(word,i_position,array)
counter = 0
while counter != len(array):
if array[counter] == '/' or array[counter] == '$':
array[counter] = ''
counter = counter +1
t = TABLE(cellspacing='0',border="2px")
x = 0
while x < crossword_size:
y = 0
while y < crossword_size:
t <= TD(array[x*crossword_size+y],style={'height':32, 'width':32})
y = y+1
t <= TR()
x = x +1
document['zone'].text = ''
document['zone']<= t
#function that given an array prints it in the website console in a crossword form
def print_array(given_array):
x = 0
y = crossword_size
while x != crossword_size*crossword_size:
print(given_array[x:y]) #prints the first line of the crossword of lenght crossword_size
x = x + crossword_size
y = y + crossword_size
print("\n")
#function that places a given word in a crossword vertically
#given the initial position of that word and the crossword
#it should be placed in
def place_vertically(word, i_position, given_array): #iposition is the coordinate of the first letter in the word
word_len = len(word)
#if msg equals none at the end of the function the crossword is aborted
msg = 0
#this is necessary so that the word isn't placed outside of the array
if i_position + (word_len*crossword_size) <= crossword_size*crossword_size and i_position >= 0:
letters = []
for letter in word:
letters.append(letter) #all the letters of the word are appended to an array
counter = 0
while counter != word_len :
#the letter is placed only if there is an empty space
if given_array[i_position] == letters[counter] or given_array[i_position] == '/':
#the letter is placed
given_array[i_position] = letters[counter]
i_position = i_position + crossword_size
else:
msg = None
counter = counter + 1
if msg != None:
return given_array
if msg == None:
return msg
#the msg thing is here to avoid errors and wrong made crosswords
#function that places a given word in a crossword horizzontally
#given the initial position of that word and the crossword
#it should be placed in
def place_horizzontally(word, i_position, given_array): # i_position is the coordinate of the first letter in the word
word_len = len(word)
#if msg equals none at the end of the function the crossword is aborted
msg = 0
#this is necessary so that the word isn't placed outside of the array
#and so that the word isn't split in two lines
if i_position + word_len - 1 <= (i_position // crossword_size) * crossword_size + crossword_size-1:
letters = []
for letter in word:
letters.append(letter) #all the letters of the word are appended to an array
counter = 0
while counter != word_len :
#the letter is placed only if there is an empty space
if given_array[i_position] == letters[counter] or given_array[i_position] == '/':
#the letter is placed
given_array[i_position] = letters[counter]
i_position = i_position + 1
else:
msg = None
counter = counter + 1
if msg != None:
return given_array
if msg == None:
return msg
#the msg thing is here to avoid errors and wrong made crosswords
#a completely unoptimized function that searches where
#there are similar letters given a word and a crossword
def find_same_letters(array,word):
same_letters = []
letters = []
for letter in word:
letters.append(letter)
letters_len = len(letters)
counter = 0
array_counter = 0
#brute searches similarities
while counter != letters_len:
while array_counter != crossword_size*crossword_size-1:
#if similarity is found
if letters[counter] == array[array_counter]:
#the infomation is defined with an array
#['letter of the word','where is that letter equal','position of that letter in the word']
same_letters.append([letters[counter], array_counter, counter ])
array_counter = array_counter + 1
counter = counter + 1
array_counter = 0
return same_letters
# function that places the first word in an empty array
def first_word_place(array,word,all_words):
global words
# used to calculate the center of a crosswords with a given lenght
first_word_coord = int(crossword_size/2)+crossword_size*int(crossword_size/2)-crossword_size
word_len = len(word)
i_position = int(first_word_coord - (word_len/2))
# the word is placed horizzontaly in the 'center' of the crossword
array = place_horizzontally(word,i_position,array)
# this is a thing that places symbols before and after the word if it's possible
# and if it doesn't break any crossword rule like going to the next line or
# outside the array. This part is necessary to avoid wrong made crosswords
try:
if (i_position // crossword_size) * crossword_size != i_position:
array[i_position-1] = '$'
if (i_position // crossword_size) * crossword_size + crossword_size-1 != i_position + len(word)-1:
array[i_position+len(word)] = '$'
except Exception:
pass
# exeption handling in case of error
if array != 'none' and array != None:
# coords is where and how the word is placed
# in this case is ['position of the word in the list of given words at the beginning',
# 'position in the array of the first letter of the word','horizzontal or vertical,]
coords = [words.index(word)+1,i_position,"hrz"]
return [array,[coords]]
# This is the main core that binds together all the other functions to create a finished product
# It creates all the possible crosswords with the given words and a given crossword size
# To do this it creates all the possible permutation with the given words and then tries to place
# them in all the possible ways one by one
# When how_many_crosswords isn't infinite this function creates a user wanted number of crosswords
# It works in a recursive way, it places the first word, sees for example that the next word can
# be placed in two different ways and it places it, now there are two crossword in which the next
# word can be placed in and so on
# When the last word of a permutation of words is placed in the crossword, that crossword become a
# finished one and it's appended to the end_crosswords array and then prompted at the end to the user
# During all this procedure the place where the words are placed is also saved with the crossword
# in the variable usually called coords
def main(all_words,how_many_crosswords):
#global variables imported
global words
global permutation
#variables are initialized
similarity_tot = 0
similarity_loop_counter = 0
permutation_tot_counter = 0
end_crosswords_counter = 0
similarity_tot = 0
similarity_loop_counter = 0
end_crosswords_counter = 0
words_len = len(all_words)
#permutations are created one by one
permutations = itertools.permutations(all_words)
#if set to true the process is aborted
to_break = False
#for every permutation created with the given words
for permutation in permutations:
print('Permutation used: '+str(permutation))
permutation_tot_counter +=1
#a new crossword bone structure is created
temp_crosswords = new_array()
#the empty crossword is taken
empty_crossword = temp_crosswords[0][0].copy()
#the first word of the permutation is placed in the empty array
first_array = first_word_place(empty_crossword, permutation[0],all_words)
if len(first_array) == 1:
first_array.append([])
#the first crossword with only one word in it is appended to the temp_crosswords array
temp_crosswords[1].append(first_array)
words_counter = 1
#for every word in the permutation
while words_counter != words_len :
#how many crosswords were created with the previous word of the permutation
last_created_crosswords_len = len(temp_crosswords[words_counter])
last_created_crossword = 0
#this part limits the amount of words placing that can be done
#thats because if the words are too many the possible ways one
#word can be placed becomes too big, for example if in the first crossword
#i have one word, then I place the next one in two possible ways, the next
#one in 4 possible ways, the next one in 12 and so on there is an overflow.
#The problem can beclearly seen if the words are too many, so a limit of possible placements
#is implemented. If it's value is low it will find a crossword faster if there
#are a lot of words, but there is a chance that with few words any crossword is found.
how_many_last_created_crossword_cycles = 0
if last_created_crosswords_len <= 10:
how_many_last_created_crossword_cycles = last_created_crosswords_len
else:
how_many_last_created_crossword_cycles = 10
#for every crossword created with the previous word
while last_created_crossword != how_many_last_created_crossword_cycles:
similarity_loop_counter = similarity_loop_counter + 1
#similarities between the word that needs to be placed and the crossword are searched
similarity = find_same_letters(temp_crosswords[words_counter][last_created_crossword][0], permutation[words_counter])
similarity_len = len(similarity)
similarity_tot = similarity_tot + similarity_len
similarity_element = 0
#for every similarity between the word and the crossword
while similarity_element != similarity_len :
temp_array0 = 0
#a copy of various arrays is made
temp_array_vrt = temp_crosswords[words_counter][last_created_crossword][0].copy()
temp_array_hrz = temp_crosswords[words_counter][last_created_crossword][0].copy()
temp_array0 = temp_crosswords[words_counter][last_created_crossword][0].copy()
#the coordinates of where the words were placed in crossword
#created with the previous word are copied
where = temp_crosswords[words_counter][last_created_crossword][1].copy()
#if set to true the function is aborted
to_discard = False
word_orientation = 0
#coordinates for the word to fit in the crossword are calculated
#both to be placed vertically and horizzontally
coords_vrt = similarity[similarity_element][1] - crossword_size * similarity[similarity_element][2]
coords_hrz = similarity[similarity_element][1] - similarity[similarity_element][2]
space_in_beetween_words = False
#this part checks if around the letter in the crossword in which the word needs
#to be placed has at has empty cells at it's corners, for example :
# /// //g
# aaa the corners are empty aaa the corners are not empty and if a word is placed
# /// /// vertically the crossword will be wrong
if similarity[similarity_element][1] <= crossword_size*crossword_size-2-crossword_size and similarity[similarity_element][1] >= crossword_size+1:
if temp_array0[similarity[similarity_element][1]+crossword_size+1] == '/'and temp_array0[similarity[similarity_element][1]+crossword_size-1] == '/' and temp_array0[similarity[similarity_element][1]-crossword_size-1] == '/'and temp_array0[similarity[similarity_element][1]-crossword_size+1] == '/':
space_in_beetween_words = True
#this part checks if around the letter in the crossword in which the word needs
#to be placed has at has empty cells at it's corners, for example :
# /// //g
# aaa the corners are empty aaa the corners are not empty and if a word is placed
# /// /// vertically the crossword will be wrong
place_vertically_data = place_vertically(permutation[words_counter], coords_vrt, temp_array_vrt)
place_horizzontally_data = place_horizzontally(permutation[words_counter], coords_hrz, temp_array_hrz)
if place_vertically_data != None and space_in_beetween_words == True:
word_orientation = 'vrt'
#the word is placed vertically
temp_array = place_vertically_data
#if after or before the word there is a letter from another word
try:
if temp_array[coords_vrt-crossword_size].isalpha()==True or temp_array[coords_vrt+(len(permutation[words_counter]))*crossword_size].isalpha()==True:
to_discard = True
except Exception:
pass
#if possible a placeholder '$' is placed before the word
try:
if temp_array[coords_vrt-crossword_size] == '/' and coords_vrt>crossword_size-1:
temp_array[coords_vrt-crossword_size] = '$'
except Exception:
pass
#if possible a placeholder '$' is placed after the word
try:
if temp_array[coords_vrt+(len(permutation[words_counter]))*crossword_size] == '/':
temp_array[coords_vrt+(len(permutation[words_counter]))*crossword_size] = '$'
except Exception:
pass
#checks if the word can be placed horizzontally
elif place_horizzontally_data != None and space_in_beetween_words == True:
word_orientation = 'hrz'
#the word is placed
temp_array = place_horizzontally_data #piazza la parola in una copia dell'array originale
#if after or before the word there is a letter from another word
try:
if (coords_hrz // crossword_size) * crossword_size != coords_hrz and temp_array[coords_hrz-1].isalpha() == True or temp_array[coords_hrz+len(permutation[words_counter])].isalpha()==True:
to_discard = True
except Exception:
pass
#if possible a placeholder '$' is placed before the word
try:
if (coords_hrz // crossword_size) * crossword_size != coords_hrz and temp_array[coords_hrz-1] == '/':
temp_array[coords_hrz-1] = '$'
except Exception:
pass
#if possible a placeholder '$' is placed after the word
try:
if (coords_hrz // crossword_size) * crossword_size + crossword_size-1 != coords_hrz + len(permutation[words_counter])-1 and temp_array[coords_hrz+len(permutation[words_counter])] == '/':
temp_array[coords_hrz+len(permutation[words_counter])] = '$'
except Exception:
pass
else:
to_discard = True
if word_orientation == 'hrz':
coords = coords_hrz
elif word_orientation == 'vrt':
coords = coords_vrt
else:
to_discard = True
if to_discard == False:
#creates the coords info to be appended to the array
where_this_word = [words.index(permutation[words_counter])+1,coords,word_orientation]
where.append(where_this_word)
to_append = [temp_array,where]
#if no errors occur the crossword is appended and the cycle is repeated
if temp_array != 'None' and temp_array != None:
try:
temp_crosswords[words_counter+1].append(to_append)
except:
Pass
similarity_element = similarity_element + 1
last_created_crossword = last_created_crossword + 1
words_counter = words_counter + 1
permutation_tot_counter = permutation_tot_counter + 1
#this part checks if all the words of the permutation
#have been placed
crosswords_len = 0
for i in temp_crosswords:
if i != []:
crosswords_len = crosswords_len + 1
if crosswords_len == len(permutation) + 1:
counter = 0
while counter != crosswords_len + 1:
if to_break == True:
break
#finished crosswords are appended to the end_crosswords
#array, this part is stopped when the limit determined
#by how_many_crosswords is reached
if temp_crosswords[counter] == []:
for crossword in temp_crosswords[counter-1]:
end_crosswords.append(crossword)