awacke1 commited on
Commit
fc4809e
1 Parent(s): 8e34b4e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import random
3
+ import string
4
+
5
+ # Function to generate a random grid
6
+ def generate_grid(size, words):
7
+ grid = [[' ' for _ in range(size)] for _ in range(size)]
8
+
9
+ for word in words:
10
+ placed = False
11
+
12
+ while not placed:
13
+ direction = random.choice(['horizontal', 'vertical'])
14
+ if direction == 'horizontal':
15
+ row = random.randint(0, size - 1)
16
+ start_col = random.randint(0, size - len(word))
17
+
18
+ for col in range(start_col, start_col + len(word)):
19
+ if grid[row][col] != ' ' and grid[row][col] != word[col - start_col]:
20
+ break
21
+ else:
22
+ for col in range(start_col, start_col + len(word)):
23
+ grid[row][col] = word[col - start_col]
24
+ placed = True
25
+
26
+ elif direction == 'vertical':
27
+ col = random.randint(0, size - 1)
28
+ start_row = random.randint(0, size - len(word))
29
+
30
+ for row in range(start_row, start_row + len(word)):
31
+ if grid[row][col] != ' ' and grid[row][col] != word[row - start_row]:
32
+ break
33
+ else:
34
+ for row in range(start_row, start_row + len(word)):
35
+ grid[row][col] = word[row - start_row]
36
+ placed = True
37
+
38
+ return grid
39
+
40
+ # Streamlit app
41
+ st.title("Word Search with Memory")
42
+ st.write("Welcome to the Word Search game with memory! Add a new word and the grid will update without losing previously placed words.")
43
+
44
+ # Session state to store the word list
45
+ if 'word_list' not in st.session_state:
46
+ st.session_state.word_list = []
47
+
48
+ new_word = st.text_input("Enter a new word (up to 10 characters):").upper()
49
+ if len(new_word) <= 10 and new_word.isalpha():
50
+ st.session_state.word_list.append(new_word)
51
+
52
+ if st.button("Clear words"):
53
+ st.session_state.word_list = []
54
+
55
+ # Grid size
56
+ size = st.sidebar.slider("Grid size", min_value=10, max_value=20, value=10, step=1)
57
+
58
+ # Generate grid and display it
59
+ grid = generate_grid(size, st.session_state.word_list)
60
+ for row in grid:
61
+ st.write(" ".join(row))
62
+
63
+ st.write("Words added:")
64
+ st.write(", ".join(st.session_state.word_list))