awacke1 commited on
Commit
ed0e5f0
·
1 Parent(s): 105a7ae

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+ import random
5
+ import time
6
+
7
+
8
+ def generate_board(size):
9
+ """Generates a Boggle board of the specified size"""
10
+ letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
11
+ board = []
12
+ for i in range(size):
13
+ row = [random.choice(letters) for _ in range(size)]
14
+ board.append(row)
15
+ return board
16
+
17
+
18
+ def get_neighbors(row, col, size):
19
+ """Returns a list of neighboring cells for a given cell"""
20
+ neighbors = []
21
+ for i in range(max(0, row - 1), min(row + 2, size)):
22
+ for j in range(max(0, col - 1), min(col + 2, size)):
23
+ if i == row and j == col:
24
+ continue
25
+ neighbors.append((i, j))
26
+ return neighbors
27
+
28
+
29
+ def search_word(word, board, visited, row, col, size):
30
+ """Recursively searches for a word on the board"""
31
+ if not word:
32
+ return True
33
+ if row < 0 or col < 0 or row >= size or col >= size:
34
+ return False
35
+ if visited[row][col]:
36
+ return False
37
+ if board[row][col] != word[0]:
38
+ return False
39
+ visited[row][col] = True
40
+ for neighbor in get_neighbors(row, col, size):
41
+ if search_word(word[1:], board, visited, neighbor[0], neighbor[1], size):
42
+ return True
43
+ visited[row][col] = False
44
+ return False
45
+
46
+
47
+ def validate_word(word, board):
48
+ """Checks if a given word is valid on the board"""
49
+ size = len(board)
50
+ visited = [[False for _ in range(size)] for _ in range(size)]
51
+ for row in range(size):
52
+ for col in range(size):
53
+ if search_word(word, board, visited, row, col, size):
54
+ return True
55
+ return False
56
+
57
+
58
+ def boggle(size, time_limit):
59
+ """Main function for playing the Boggle game"""
60
+ st.title("Boggle Game")
61
+ st.write("Find as many words as possible by connecting adjacent letters on the board within the time limit.")
62
+ st.write("Words must be at least three letters long and can only be used once.")
63
+ board = generate_board(size)
64
+ board_df = pd.DataFrame(board)
65
+ st.write(board_df)
66
+ words = set()
67
+ start_time = time.time()
68
+ while time.time() - start_time < time_limit:
69
+ with st.form(key='new_word'):
70
+ new_word = st.text_input("Enter a new word:")
71
+ if len(new_word) >= 3 and new_word not in words and validate_word(new_word.upper(), board):
72
+ words.add(new_word)
73
+ st.write(f"Added {new_word} to the list of words!")
74
+ st.form_submit_button(label='Submit')
75
+ st.write("Time's up! Here are your words:")
76
+ st.write(words)
77
+
78
+
79
+ if __name__ == "__main__":
80
+ boggle(4, 60)