File size: 2,916 Bytes
1591c02
 
 
 
89f1b7a
 
 
1591c02
89f1b7a
 
a9476d9
 
 
 
 
0f047f9
a9476d9
 
 
1591c02
89f1b7a
 
 
 
 
 
 
 
 
1591c02
89f1b7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
973e097
 
 
 
 
 
e0f5fa9
0f047f9
 
 
 
 
 
e0f5fa9
973e097
 
0f047f9
 
e0f5fa9
973e097
 
 
 
a800fe4
0f047f9
 
e0f5fa9
973e097
a800fe4
 
973e097
 
e0f5fa9
973e097
1591c02
89f1b7a
973e097
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
import streamlit as st
import random
import string

board_size = 15
words = []
board = [[' ' for _ in range(board_size)] for _ in range(board_size)]

def load_word_list():
    global words
    try:
        with open("word_list.txt", "r") as f:
            words = f.read().split("\n")
    except FileNotFoundError:
        pass
    st.text_area("Enter a list of words (one per line)", "\n".join(words), key="words_area")
    st.sidebar.subheader("Word List:")
    for i, word in enumerate(words):
        st.sidebar.write(f"{i+1}. {word}")

def save_word_list():
    global words
    with open("word_list.txt", "w") as f:
        f.write("\n".join(words))
    st.write("Word list saved successfully!")

def generate_board():
    global board, words
    board = [[' ' for _ in range(board_size)] for _ in range(board_size)]
    for word in words:
        word = word.upper()
        row, col = random.randint(0, board_size - 1), random.randint(0, board_size - 1)
        direction = random.choice(['horizontal', 'vertical', 'diagonal'])
        if direction == 'horizontal' and col + len(word) <= board_size:
            for i, letter in enumerate(word):
                board[row][col+i] = letter
        elif direction == 'vertical' and row + len(word) <= board_size:
            for i, letter in enumerate(word):
                board[row+i][col] = letter
        elif direction == 'diagonal' and row + len(word) <= board_size and col + len(word) <= board_size:
            for i, letter in enumerate(word):
                board[row+i][col+i] = letter
    for i in range(board_size):
        for j in range(board_size):
            if board[i][j] == ' ':
                board[i][j] = random.choice(string.ascii_uppercase)

buttons = {
    "Load Word List": load_word_list,
    "Save Word List": save_word_list,
    "Generate Board": generate_board
}

if "words_area" not in st.session_state:
    try:
        with open("word_list.txt", "r") as f:
            words = f.read().split("\n")
    except FileNotFoundError:
        pass
    st.text_area("Enter a list of words (one per line)", "\n".join(words), key="words_area")

for button_label, button_func in buttons.items():
    if st.sidebar.button(button_label):
        with open("word_list.txt", "w") as f:
            f.write(st.session_state.words_area)
        words = st.session_state.words_area.split("\n")
        button_func()
        st.sidebar.subheader("Word List:")
        for i, word in enumerate(words):
            st.sidebar.write(f"{i+1}. {word}")

with open("word_list.txt", "w") as f:
    f.write(st.session_state.words_area)
words = st.session_state.words_area
if st.button("Save Word List", key="save_word_list_btn"):
    words = words.split("\n")
    save_word_list()

st.sidebar.subheader("Word List:")
for i, word in enumerate(words.split("\n")):
    st.sidebar.write(f"{i+1}. {word}")

st.write("Word Search Board:")
st.table(board)