Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -2,83 +2,74 @@ import streamlit as st
|
|
2 |
import random
|
3 |
import string
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
board = [[' ' for _ in range(board_size)] for _ in range(board_size)]
|
8 |
|
9 |
def load_word_list():
|
10 |
-
global words
|
11 |
try:
|
12 |
-
with open(
|
13 |
words = f.read().split("\n")
|
14 |
except FileNotFoundError:
|
15 |
-
|
16 |
st.text_area("Enter a list of words (one per line)", "\n".join(words), key="words_area")
|
17 |
st.sidebar.subheader("Word List:")
|
18 |
for i, word in enumerate(words):
|
19 |
st.sidebar.write(f"{i+1}. {word}")
|
|
|
20 |
|
21 |
-
def save_word_list():
|
22 |
-
|
23 |
-
with open("word_list.txt", "w") as f:
|
24 |
f.write("\n".join(words))
|
25 |
st.write("Word list saved successfully!")
|
26 |
|
27 |
-
def generate_board():
|
28 |
-
|
29 |
-
board = [[' ' for _ in range(board_size)] for _ in range(board_size)]
|
30 |
for word in words:
|
31 |
word = word.upper()
|
32 |
-
row, col = random.randint(0,
|
33 |
direction = random.choice(['horizontal', 'vertical', 'diagonal'])
|
34 |
-
if direction == 'horizontal' and col + len(word) <=
|
35 |
for i, letter in enumerate(word):
|
36 |
board[row][col+i] = letter
|
37 |
-
elif direction == 'vertical' and row + len(word) <=
|
38 |
for i, letter in enumerate(word):
|
39 |
board[row+i][col] = letter
|
40 |
-
elif direction == 'diagonal' and row + len(word) <=
|
41 |
for i, letter in enumerate(word):
|
42 |
board[row+i][col+i] = letter
|
43 |
-
for i in range(
|
44 |
-
for j in range(
|
45 |
if board[i][j] == ' ':
|
46 |
board[i][j] = random.choice(string.ascii_uppercase)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
buttons = {
|
49 |
"Load Word List": load_word_list,
|
50 |
-
"Save Word List": save_word_list,
|
51 |
-
"Generate Board": generate_board
|
52 |
}
|
53 |
|
54 |
-
|
55 |
-
try:
|
56 |
-
with open("word_list.txt", "r") as f:
|
57 |
-
words = f.read().split("\n")
|
58 |
-
except FileNotFoundError:
|
59 |
-
pass
|
60 |
-
st.text_area("Enter a list of words (one per line)", "\n".join(words), key="words_area")
|
61 |
-
|
62 |
for button_label, button_func in buttons.items():
|
63 |
if st.sidebar.button(button_label):
|
64 |
-
|
65 |
-
|
66 |
-
words = st.session_state.words_area.split("\n")
|
67 |
-
button_func()
|
68 |
-
st.sidebar.subheader("Word List:")
|
69 |
-
for i, word in enumerate(words):
|
70 |
-
st.sidebar.write(f"{i+1}. {word}")
|
71 |
-
|
72 |
-
with open("word_list.txt", "w") as f:
|
73 |
-
f.write(st.session_state.words_area)
|
74 |
-
words = st.session_state.words_area
|
75 |
-
if st.button("Save Word List", key="save_word_list_btn"):
|
76 |
-
words = words.split("\n")
|
77 |
-
save_word_list()
|
78 |
-
|
79 |
-
st.sidebar.subheader("Word List:")
|
80 |
-
for i, word in enumerate(words.split("\n")):
|
81 |
-
st.sidebar.write(f"{i+1}. {word}")
|
82 |
|
83 |
-
|
84 |
-
st.table(board)
|
|
|
2 |
import random
|
3 |
import string
|
4 |
|
5 |
+
BOARD_SIZE = 15
|
6 |
+
WORDS_FILE = "word_list.txt"
|
|
|
7 |
|
8 |
def load_word_list():
|
|
|
9 |
try:
|
10 |
+
with open(WORDS_FILE, "r") as f:
|
11 |
words = f.read().split("\n")
|
12 |
except FileNotFoundError:
|
13 |
+
words = []
|
14 |
st.text_area("Enter a list of words (one per line)", "\n".join(words), key="words_area")
|
15 |
st.sidebar.subheader("Word List:")
|
16 |
for i, word in enumerate(words):
|
17 |
st.sidebar.write(f"{i+1}. {word}")
|
18 |
+
return words
|
19 |
|
20 |
+
def save_word_list(words):
|
21 |
+
with open(WORDS_FILE, "w") as f:
|
|
|
22 |
f.write("\n".join(words))
|
23 |
st.write("Word list saved successfully!")
|
24 |
|
25 |
+
def generate_board(words):
|
26 |
+
board = [[' ' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
|
|
|
27 |
for word in words:
|
28 |
word = word.upper()
|
29 |
+
row, col = random.randint(0, BOARD_SIZE - 1), random.randint(0, BOARD_SIZE - 1)
|
30 |
direction = random.choice(['horizontal', 'vertical', 'diagonal'])
|
31 |
+
if direction == 'horizontal' and col + len(word) <= BOARD_SIZE:
|
32 |
for i, letter in enumerate(word):
|
33 |
board[row][col+i] = letter
|
34 |
+
elif direction == 'vertical' and row + len(word) <= BOARD_SIZE:
|
35 |
for i, letter in enumerate(word):
|
36 |
board[row+i][col] = letter
|
37 |
+
elif direction == 'diagonal' and row + len(word) <= BOARD_SIZE and col + len(word) <= BOARD_SIZE:
|
38 |
for i, letter in enumerate(word):
|
39 |
board[row+i][col+i] = letter
|
40 |
+
for i in range(BOARD_SIZE):
|
41 |
+
for j in range(BOARD_SIZE):
|
42 |
if board[i][j] == ' ':
|
43 |
board[i][j] = random.choice(string.ascii_uppercase)
|
44 |
+
return board
|
45 |
+
|
46 |
+
def display_word_list(words):
|
47 |
+
st.sidebar.subheader("Word List:")
|
48 |
+
for i, word in enumerate(words):
|
49 |
+
st.sidebar.write(f"{i+1}. {word}")
|
50 |
+
|
51 |
+
def display_board(board):
|
52 |
+
st.write("Word Search Board:")
|
53 |
+
st.table(board)
|
54 |
+
|
55 |
+
def paste_story():
|
56 |
+
story = st.text_area("Paste your story here")
|
57 |
+
if st.button("Build Word List"):
|
58 |
+
words = [word.upper() for word in story.split() if len(word) > 4]
|
59 |
+
words = sorted(words, key=len, reverse=True)[:10]
|
60 |
+
save_word_list(words)
|
61 |
+
board = generate_board(words)
|
62 |
+
display_word_list(words)
|
63 |
+
display_board(board)
|
64 |
|
65 |
buttons = {
|
66 |
"Load Word List": load_word_list,
|
|
|
|
|
67 |
}
|
68 |
|
69 |
+
words = setup()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
for button_label, button_func in buttons.items():
|
71 |
if st.sidebar.button(button_label):
|
72 |
+
words = button_func()
|
73 |
+
display_word_list(words)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
paste_story()
|
|