-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnections_game.py
60 lines (49 loc) · 2.19 KB
/
connections_game.py
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
import streamlit as st
import random
# Define your categories and words
categories = {
"Risk-Taking": ["Chutzpah", "Boldness", "Audacity", "Risk"],
"Academics": ["Technion", "University", "STEM", "Research"],
"Military": ["Military", "Unit", "Skills", "Problem-Solving"],
"Innovation": ["Grants", "Funding", "Authority", "Cooperation"]
}
# Initialize session state variables
if 'selected_words' not in st.session_state:
st.session_state.selected_words = []
if 'found_categories' not in st.session_state:
st.session_state.found_categories = set()
# Create a list of all words and shuffle them
words = [word for category in categories.values() for word in category]
random.shuffle(words)
# Function to check if a selected group matches any category
def check_group(selected_words, categories):
for category, group_words in categories.items():
if sorted(selected_words) == sorted(group_words):
return category
return None
# Streamlit UI setup
st.title("Connections Game")
st.write("Select 4 words that belong to the same category.")
# Layout for displaying the word buttons
cols = st.columns(4) # Create a grid of 4 columns
for i, word in enumerate(words):
with cols[i % 4]: # Ensure words are distributed evenly across columns
if st.button(word):
if word not in st.session_state.selected_words:
st.session_state.selected_words.append(word)
# Display the selected words
st.write("Selected Words:", st.session_state.selected_words)
# Allow the user to submit their guess when 4 words are selected
if len(st.session_state.selected_words) == 4:
category = check_group(st.session_state.selected_words, categories)
if category and category not in st.session_state.found_categories:
st.success(f"Correct! These words belong to the '{category}' category.")
st.session_state.found_categories.add(category)
else:
st.error("Incorrect group. Try again!")
# Reset selected words after checking
st.session_state.selected_words = []
# Check if the game is finished
if len(st.session_state.found_categories) == len(categories):
st.balloons()
st.write("Congratulations! You've found all the groups!")