-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrossword.py
More file actions
81 lines (62 loc) · 2.45 KB
/
crossword.py
File metadata and controls
81 lines (62 loc) · 2.45 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
import random
import string
board_size = 10
words = input("Enter words separated by spaces: ").split()
#Generate the board
board = [['*' for _ in range(board_size)] for _ in range(board_size)]
#Print Grid/Board Function
def print_board():
for x in range(board_size):
print('\t'*4+' '.join(board[x]))
placements = ['horizontal','vertical']
for word in words:
word_length = len(word)
placed = False
while not placed:
placement = random.choice(placements)
if placement == 'horizontal':
step_x = 1
step_y = 0
if placement == 'vertical':
step_x = 0
step_y = 1
#Generate random starting location for word
x_position = random.randint(0,board_size)
y_position = random.randint(0,board_size)
ending_x = x_position + word_length*step_x
ending_y = y_position + word_length*step_y
#Check if word will fit on the board starting at the random position
if ending_x<0 or ending_x>= board_size: continue
if ending_y<0 or ending_y>= board_size: continue
failed = False
#Try placing the word if you run into an already placed character instead of star try again
for i in range(word_length):
character = word[i]
new_position_x = x_position + i*step_x
new_position_y = y_position + i*step_y
character_at_new_position = board[new_position_x][new_position_y]
if character_at_new_position != '*':
if character_at_new_position == character:
continue
else:
failed = True
break
if failed:
continue
#Place the word onto the grid
else:
for i in range(word_length):
character = word[i]
new_position_x = x_position + i*step_x
new_position_y = y_position + i*step_y
board[new_position_x][new_position_y] = character
placed = True
#Showcase the answers
print("Board before:")
print_board()
for x in range(board_size):
for y in range(board_size):
if (board[x][y]=='*'):
board[x][y]=random.choice(string.ascii_uppercase)
print("Final board:")
print_board()