-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrossword_puzzle_generator.py
80 lines (66 loc) · 2.24 KB
/
crossword_puzzle_generator.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
import random
# Define the grid size
GRID_SIZE = 10
# Define a list of words to be used in the crossword puzzle
WORDS = ["python", "java", "ruby", "perl", "swift", "kotlin", "typescript", "javascript", "html", "css"]
# Initialize the crossword grid
grid = [[' ' for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]
def can_place_word_horizontally(word, row, col):
if col + len(word) > GRID_SIZE:
return False
for i in range(len(word)):
if grid[row][col + i] not in (' ', word[i]):
return False
return True
def can_place_word_vertically(word, row, col):
if row + len(word) > GRID_SIZE:
return False
for i in range(len(word)):
if grid[row + i][col] not in (' ', word[i]):
return False
return True
def place_word_horizontally(word, row, col):
for i in range(len(word)):
grid[row][col + i] = word[i]
def place_word_vertically(word, row, col):
for i in range(len(word)):
grid[row + i][col] = word[i]
def place_words(words):
random.shuffle(words)
for word in words:
placed = False
for _ in range(100): # Try 100 times to place the word
row = random.randint(0, GRID_SIZE - 1)
col = random.randint(0, GRID_SIZE - 1)
if random.choice([True, False]):
if can_place_word_horizontally(word, row, col):
place_word_horizontally(word, row, col)
placed = True
break
else:
if can_place_word_vertically(word, row, col):
place_word_vertically(word, row, col)
placed = True
break
if not placed:
print(f"Failed to place the word: {word}")
def print_grid():
for row in grid:
print(' '.join(row))
def generate_clues(words):
clues = {}
for word in words:
clues[word] = f"Clue for {word}"
return clues
def print_clues(clues):
for word, clue in clues.items():
print(f"{word.upper()}: {clue}")
# Place the words in the crossword grid
place_words(WORDS)
# Print the crossword grid
print("Crossword Grid:")
print_grid()
# Generate and print the clues
clues = generate_clues(WORDS)
print("\nClues:")
print_clues(clues)