-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
101 lines (79 loc) · 2.98 KB
/
main.py
File metadata and controls
101 lines (79 loc) · 2.98 KB
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
import random
from copy import deepcopy
from word_grid import WordGrid
from point import Point
from direction import Direction
from placement import Placement
from exceptions import (
FailedToGenerateWordSearchError,
FailedToPlaceAllWordsError,
NoLegalPlacementsError,
GridOverflowError,
)
def main():
word_list = ["PYTHON", "DOJO", "CODEHUB", "BRISTOL"]
try:
word_search = generate_word_search(rows=6, cols=9, word_list=word_list)
print(word_search)
print("Find these words:")
print(", ".join(word_list))
except FailedToGenerateWordSearchError:
print("Failed to generate word search.")
exit(1)
def generate_word_search(rows: int, cols: int, word_list: list[str]) -> WordGrid:
word_grid = WordGrid(rows, cols)
attempts = 0
max_attempts = 10
while attempts < max_attempts:
word_grid.initialise_grid()
try:
filled_word_search = place_words(word_grid, word_list)
filled_word_search.fill_blank_space()
return filled_word_search
except FailedToPlaceAllWordsError:
attempts += 1
else:
raise FailedToGenerateWordSearchError()
def place_words(word_grid: WordGrid, word_list: list[str]) -> WordGrid:
word_search = deepcopy(word_grid)
for word in word_list:
try:
placements = get_all_legal_placements_for_word(word_search, word)
position, direction = random.choice(placements)
word_search.write_line(position, direction, word)
except NoLegalPlacementsError:
raise FailedToPlaceAllWordsError()
return word_search
def get_all_legal_placements_for_word(
word_grid: WordGrid, word: str
) -> list[Placement]:
legal_placements = []
# Iterate through all possible grid locations and orientations
for row_index, row in enumerate(word_grid.grid):
for col_index, col in enumerate(row):
for direction in Direction:
position = Point(row_index, col_index)
line_can_be_written = word_grid.is_valid_line(
position, direction, len(word)
)
if not line_can_be_written:
continue
target_line = word_grid.read_line(
position, direction, len(word))
line_can_be_placed = is_legal_placement(
target_line=target_line, line_to_write=word
)
if not line_can_be_placed:
continue
legal_placements.append(Placement(position, direction))
if len(legal_placements) == 0:
raise NoLegalPlacementsError()
else:
return legal_placements
def is_legal_placement(target_line: str, line_to_write: str) -> bool:
for target_char, char_to_write in zip(target_line, line_to_write):
if (char_to_write != target_char) and (target_char != " "):
return False
return True
if __name__ == "__main__":
main()