awacke1 commited on
Commit
973e097
·
1 Parent(s): a800fe4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -35
app.py CHANGED
@@ -2,46 +2,35 @@ import streamlit as st
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
- # Check if word list text file exists
14
  try:
15
  with open("word_list.txt", "r") as f:
16
  words = f.read().split("\n")
17
  except FileNotFoundError:
18
  pass
19
- # Update word list textbox
20
  st.text_area("Enter a list of words (one per line)", "\n".join(words))
21
- # Update word list on sidebar
22
  st.sidebar.subheader("Word List:")
23
  for i, word in enumerate(words):
24
  st.sidebar.write(f"{i+1}. {word}")
25
 
26
- # Function to save word list
27
  def save_word_list():
28
  global words
29
  with open("word_list.txt", "w") as f:
30
  f.write("\n".join(words))
31
  st.write("Word list saved successfully!")
32
 
33
- # Function to generate board
34
  def generate_board():
35
  global board, words
36
- # Clear board
37
  board = [[' ' for _ in range(board_size)] for _ in range(board_size)]
38
- # Add words to board
39
  for word in words:
40
  word = word.upper()
41
- # Generate random starting position and direction for word
42
  row, col = random.randint(0, board_size - 1), random.randint(0, board_size - 1)
43
  direction = random.choice(['horizontal', 'vertical', 'diagonal'])
44
- # Check if word can fit in selected direction
45
  if direction == 'horizontal' and col + len(word) <= board_size:
46
  for i, letter in enumerate(word):
47
  board[row][col+i] = letter
@@ -51,38 +40,33 @@ def generate_board():
51
  elif direction == 'diagonal' and row + len(word) <= board_size and col + len(word) <= board_size:
52
  for i, letter in enumerate(word):
53
  board[row+i][col+i] = letter
54
- # Fill remaining board spaces with random letters
55
  for i in range(board_size):
56
  for j in range(board_size):
57
  if board[i][j] == ' ':
58
  board[i][j] = random.choice(string.ascii_uppercase)
59
 
60
- # Display sidebar buttons and handle user input
61
- st.sidebar.button("Load Word List", on_click=load_word_list)
62
- if st.sidebar.button("Save Word List", on_click=save_word_list):
63
- # Get words from word list textbox
64
- words = st.text_area("Enter a list of words (one per line)", "\n".join(words)).split("\n")
65
- # Update word list on sidebar
66
- st.sidebar.subheader("Word List:")
67
- for i, word in enumerate(words):
68
- st.sidebar.write(f"{i+1}. {word}")
69
- if st.sidebar.button("Generate Board"):
70
- # Get words from word list textbox
71
- words = st.text_area("Enter a list of words (one per line)", "\n".join(words)).split("\n")
72
- # Update word list on sidebar
73
- st.sidebar.subheader("Word List:")
74
- for i, word in enumerate(words):
75
- st.sidebar.write(f"{i+1}. {word}")
76
- generate_board()
77
 
78
- # Display word list text box and "Save Word List" button
79
  words = st.text_area("Enter a list of words (one per line)", "\n".join(words))
80
- if st.button("Save Word List"):
81
  words = words.split("\n")
82
  save_word_list()
83
- st.sidebar.subheader("Word List:")
84
- for i, word in enumerate(words):
85
- st.sidebar.write(f"{i+1}. {word}")
 
86
 
87
  st.write("Word Search Board:")
88
- st.table(board)
 
2
  import random
3
  import string
4
 
 
5
  board_size = 15
6
  words = []
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("word_list.txt", "r") as f:
13
  words = f.read().split("\n")
14
  except FileNotFoundError:
15
  pass
 
16
  st.text_area("Enter a list of words (one per line)", "\n".join(words))
 
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
  global words
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
  global board, words
 
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, board_size - 1), random.randint(0, board_size - 1)
33
  direction = random.choice(['horizontal', 'vertical', 'diagonal'])
 
34
  if direction == 'horizontal' and col + len(word) <= board_size:
35
  for i, letter in enumerate(word):
36
  board[row][col+i] = 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
  for i in range(board_size):
44
  for j in range(board_size):
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
+ for button_label, button_func in buttons.items():
55
+ if st.sidebar.button(button_label):
56
+ words = st.text_area("Enter a list of words (one per line)", "\n".join(words)).split("\n")
57
+ button_func()
58
+ st.sidebar.subheader("Word List:")
59
+ for i, word in enumerate(words):
60
+ st.sidebar.write(f"{i+1}. {word}")
 
 
 
 
61
 
 
62
  words = st.text_area("Enter a list of words (one per line)", "\n".join(words))
63
+ if st.button("Save Word List", key="save_word_list_btn"):
64
  words = words.split("\n")
65
  save_word_list()
66
+
67
+ st.sidebar.subheader("Word List:")
68
+ for i, word in enumerate(words):
69
+ st.sidebar.write(f"{i+1}. {word}")
70
 
71
  st.write("Word Search Board:")
72
+ st.table(board)