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)) 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 } for button_label, button_func in buttons.items(): if st.sidebar.button(button_label): words = st.text_area("Enter a list of words (one per line)", "\n".join(words)).split("\n") button_func() st.sidebar.subheader("Word List:") for i, word in enumerate(words): st.sidebar.write(f"{i+1}. {word}") words = st.text_area("Enter a list of words (one per line)", "\n".join(words)) 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): st.sidebar.write(f"{i+1}. {word}") st.write("Word Search Board:") st.table(board)