File size: 4,586 Bytes
ed0e5f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc09212
4c5e15c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed0e5f0
bc09212
 
ed0e5f0
bc09212
ed0e5f0
 
 
 
 
488f16a
bc09212
e242256
bc09212
 
 
 
 
 
 
 
 
198b6be
ed0e5f0
 
bc09212
 
ab1cbaa
bc09212
ab1cbaa
64b516f
 
ab1cbaa
64b516f
bc09212
 
 
 
 
 
 
64b516f
 
ab1cbaa
61d47a4
 
 
76053c3
ed0e5f0
bc09212
ab1cbaa
bc09212
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import streamlit as st
import numpy as np
import pandas as pd
import random
import time


def generate_board(size):
    """Generates a Boggle board of the specified size"""
    letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    board = []
    for i in range(size):
        row = [random.choice(letters) for _ in range(size)]
        board.append(row)
    return board


def get_neighbors(row, col, size):
    """Returns a list of neighboring cells for a given cell"""
    neighbors = []
    for i in range(max(0, row - 1), min(row + 2, size)):
        for j in range(max(0, col - 1), min(col + 2, size)):
            if i == row and j == col:
                continue
            neighbors.append((i, j))
    return neighbors


def search_word(word, board, visited, row, col, size):
    """Recursively searches for a word on the board"""
    if not word:
        return True
    if row < 0 or col < 0 or row >= size or col >= size:
        return False
    if visited[row][col]:
        return False
    if board[row][col] != word[0]:
        return False
    visited[row][col] = True
    for neighbor in get_neighbors(row, col, size):
        if search_word(word[1:], board, visited, neighbor[0], neighbor[1], size):
            return True
    visited[row][col] = False
    return False


def validate_word(word, board):
    """Checks if a given word is valid on the board"""
    size = len(board)
    visited = [[False for _ in range(size)] for _ in range(size)]
    for row in range(size):
        for col in range(size):
            if search_word(word, board, visited, row, col, size):
                return True
    return False

def boggle_1(size, time_limit):
    """Main function for playing the Boggle game"""
    st.title("Boggle Game")
    st.write("Find as many words as possible by connecting adjacent letters on the board.")
    st.write("Words must be at least three letters long and can only be used once.")
    board = generate_board(size)
    board_df = pd.DataFrame(board)
    st.write(board_df)
    words = set()

    with st.form(key="new_word"):
        new_word = st.text_input("Enter a new word:")
        if st.form_submit_button("Submit"):
            if len(new_word) >= 3 and new_word not in words and validate_word(new_word.upper(), board):
                words.add(new_word)
                st.write(f"Added {new_word} to the list of words!")
            elif new_word in words:
                st.write("Word already used!")
            else:
                st.write("Invalid word. Try again.")

    st.write("Here are your words:")
    st.write(words)

def boggle(size):
    """Function for setting up the Boggle game"""
    st.title("Boggle Game")
    st.write("Find as many words as possible by connecting adjacent letters on the board.")
    st.write("Words must be at least three letters long and can only be used once.")
    board = generate_board(size)
    board_df = pd.DataFrame(board)
    st.write(board_df)
    words = set()

    with st.form(key="new_word"):
        new_word = st.text_input("Enter a new word:")
        if st.form_submit_button("Submit"):
            if len(new_word) >= 3 and new_word not in words and validate_word(new_word.upper(), board):
                words.add(new_word)
                st.write(f"Added {new_word} to the list of words!")
            elif new_word in words:
                st.write("Word already used!")
            else:
                st.write("Invalid word. Try again.")

    st.write("Here are your words:")
    st.write(words)

    return board, words

def play_boggle(board, words):
    """Function for playing the Boggle game"""
    with st.form(key="new_word"):
        new_word = st.text_input("Enter a new word:")
        submit_button = st.form_submit_button("Submit")
        while True:
            if submit_button:
                if len(new_word) >= 3 and new_word not in words and validate_word(new_word.upper(), board):
                    words.add(new_word)
                    st.write(f"Added {new_word} to the list of words!")
                elif new_word in words:
                    st.write("Word already used!")
                else:
                    st.write("Invalid word. Try again.")
            st.write("Here are your words:")
            st.write(words)
            with st.form(key="new_word"):
                new_word = st.text_input("Enter a new word:")
                submit_button = st.form_submit_button("Submit")


if __name__ == "__main__":
    board, words = boggle(7)
    play_boggle(board, words)
    
#if __name__ == "__main__":
#    boggle(7, 60)