awacke1's picture
Update app.py
89f1b7a
raw
history blame
2.09 kB
import streamlit as st
import random
import string
# Initialize global variables
board_size = 15
words = []
board = [[' ' for _ in range(board_size)] for _ in range(board_size)]
# Function to load word list
def load_word_list():
global words
words = st.text_area("Enter a list of words (one per line)", "").split("\n")
# Function to save word list
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!")
# Function to generate board
def generate_board():
global board, words
# Clear board
board = [[' ' for _ in range(board_size)] for _ in range(board_size)]
# Add words to board
for word in words:
word = word.upper()
# Generate random starting position and direction for word
row, col = random.randint(0, board_size - 1), random.randint(0, board_size - 1)
direction = random.choice(['horizontal', 'vertical', 'diagonal'])
# Check if word can fit in selected direction
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
# Fill remaining board spaces with random letters
for i in range(board_size):
for j in range(board_size):
if board[i][j] == ' ':
board[i][j] = random.choice(string.ascii_uppercase)
# Display sidebar buttons and handle user input
st.sidebar.button("Load Word List", on_click=load_word_list)
st.sidebar.button("Save Word List", on_click=save_word_list)
if st.sidebar.button("Generate Board"):
generate_board()
# Display letter board
st.write("Word Search Board:")
st.table(board)