Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -2,71 +2,56 @@ import streamlit as st
|
|
2 |
import random
|
3 |
import string
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
|
|
8 |
|
9 |
-
#
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
row = [random.choice(string.ascii_uppercase) for j in range(size)]
|
14 |
-
grid.append(row)
|
15 |
-
return grid
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
for word in words:
|
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 |
-
# Load words from file or user input
|
50 |
-
if st.sidebar.button("Load words"):
|
51 |
-
with open(WORDS_FILE, "r") as f:
|
52 |
-
words = [word.strip() for word in f.readlines()]
|
53 |
-
else:
|
54 |
-
words_input = st.sidebar.text_area("Enter words (one per line)")
|
55 |
-
words = [word.strip() for word in words_input.split("\n")]
|
56 |
-
|
57 |
-
# Save words to file
|
58 |
-
if st.sidebar.button("Save words"):
|
59 |
-
with open(WORDS_FILE, "w") as f:
|
60 |
-
for word in words:
|
61 |
-
f.write(word + "\n")
|
62 |
-
|
63 |
-
# Generate and display word search
|
64 |
-
if words:
|
65 |
-
grid = generate_grid(SIZE)
|
66 |
-
grid = add_words_to_grid(words, grid)
|
67 |
-
st.write("Find the following words:")
|
68 |
-
st.write(words)
|
69 |
-
for row in grid:
|
70 |
-
st.write(" ".join(row))
|
71 |
-
else:
|
72 |
-
st.write("Enter words to generate a word search.")
|
|
|
2 |
import random
|
3 |
import string
|
4 |
|
5 |
+
# Initialize global variables
|
6 |
+
board_size = 15
|
7 |
+
words = []
|
8 |
+
board = [[' ' for _ in range(board_size)] for _ in range(board_size)]
|
9 |
|
10 |
+
# Function to load word list
|
11 |
+
def load_word_list():
|
12 |
+
global words
|
13 |
+
words = st.text_area("Enter a list of words (one per line)", "").split("\n")
|
|
|
|
|
|
|
14 |
|
15 |
+
# Function to save word list
|
16 |
+
def save_word_list():
|
17 |
+
global words
|
18 |
+
with open("word_list.txt", "w") as f:
|
19 |
+
f.write("\n".join(words))
|
20 |
+
st.write("Word list saved successfully!")
|
21 |
+
|
22 |
+
# Function to generate board
|
23 |
+
def generate_board():
|
24 |
+
global board, words
|
25 |
+
# Clear board
|
26 |
+
board = [[' ' for _ in range(board_size)] for _ in range(board_size)]
|
27 |
+
# Add words to board
|
28 |
for word in words:
|
29 |
+
word = word.upper()
|
30 |
+
# Generate random starting position and direction for word
|
31 |
+
row, col = random.randint(0, board_size - 1), random.randint(0, board_size - 1)
|
32 |
+
direction = random.choice(['horizontal', 'vertical', 'diagonal'])
|
33 |
+
# Check if word can fit in selected direction
|
34 |
+
if direction == 'horizontal' and col + len(word) <= board_size:
|
35 |
+
for i, letter in enumerate(word):
|
36 |
+
board[row][col+i] = letter
|
37 |
+
elif direction == 'vertical' and row + len(word) <= board_size:
|
38 |
+
for i, letter in enumerate(word):
|
39 |
+
board[row+i][col] = letter
|
40 |
+
elif direction == 'diagonal' and row + len(word) <= board_size and col + len(word) <= board_size:
|
41 |
+
for i, letter in enumerate(word):
|
42 |
+
board[row+i][col+i] = letter
|
43 |
+
# Fill remaining board spaces with random letters
|
44 |
+
for i in range(board_size):
|
45 |
+
for j in range(board_size):
|
46 |
+
if board[i][j] == ' ':
|
47 |
+
board[i][j] = random.choice(string.ascii_uppercase)
|
48 |
+
|
49 |
+
# Display sidebar buttons and handle user input
|
50 |
+
st.sidebar.button("Load Word List", on_click=load_word_list)
|
51 |
+
st.sidebar.button("Save Word List", on_click=save_word_list)
|
52 |
+
if st.sidebar.button("Generate Board"):
|
53 |
+
generate_board()
|
54 |
|
55 |
+
# Display letter board
|
56 |
+
st.write("Word Search Board:")
|
57 |
+
st.table(board)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|