Update src/streamlit_app.py
Browse files- src/streamlit_app.py +182 -129
src/streamlit_app.py
CHANGED
|
@@ -1,7 +1,20 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import random
|
|
|
|
| 3 |
from typing import List, Tuple, Dict, Optional
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Constants
|
| 6 |
SOLVED_STATE = [1, 2, 3, 4, 5, 6, 7, 8, 0]
|
| 7 |
|
|
@@ -145,9 +158,20 @@ if 'game' not in st.session_state:
|
|
| 145 |
# Custom CSS for styling
|
| 146 |
st.markdown("""
|
| 147 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
.main > div {
|
| 149 |
-
padding-top:
|
| 150 |
-
padding-bottom:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
}
|
| 152 |
|
| 153 |
.game-container {
|
|
@@ -158,6 +182,7 @@ st.markdown("""
|
|
| 158 |
margin: 20px auto;
|
| 159 |
text-align: center;
|
| 160 |
max-width: 600px;
|
|
|
|
| 161 |
}
|
| 162 |
|
| 163 |
.puzzle-grid {
|
|
@@ -170,34 +195,37 @@ st.markdown("""
|
|
| 170 |
padding: 20px;
|
| 171 |
background: rgba(0,0,0,0.05);
|
| 172 |
border-radius: 8px;
|
|
|
|
| 173 |
}
|
| 174 |
|
| 175 |
-
.
|
| 176 |
-
width: 100px;
|
| 177 |
-
height: 100px;
|
| 178 |
-
border: none;
|
| 179 |
-
border-radius: 8px;
|
| 180 |
-
background: linear-gradient(145deg, #2196F3, #1976D2);
|
| 181 |
-
color: white;
|
| 182 |
-
font-size: 24px;
|
| 183 |
-
font-weight: 700;
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
| 189 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 190 |
}
|
| 191 |
|
| 192 |
-
.
|
| 193 |
-
transform: translateY(-2px) scale(1.02);
|
| 194 |
-
box-shadow: 0 4px 8px rgba(0,0,0,0.15);
|
| 195 |
}
|
| 196 |
|
| 197 |
.empty-tile {
|
|
|
|
|
|
|
| 198 |
background: rgba(255,255,255,0.8);
|
| 199 |
border: 2px dashed #ccc;
|
| 200 |
box-shadow: inset 0 2px 4px rgba(0,0,0,0.1);
|
|
|
|
|
|
|
| 201 |
}
|
| 202 |
|
| 203 |
.moves-counter {
|
|
@@ -259,21 +287,21 @@ st.markdown("""
|
|
| 259 |
opacity: 0.9;
|
| 260 |
}
|
| 261 |
|
| 262 |
-
.
|
| 263 |
-
width: 140px;
|
| 264 |
-
height: 45px;
|
| 265 |
-
border-radius: 8px;
|
| 266 |
-
font-weight: 600;
|
| 267 |
-
font-size: 16px;
|
| 268 |
-
text-transform: uppercase;
|
| 269 |
-
letter-spacing: 0.5px;
|
| 270 |
-
margin: 5px;
|
| 271 |
-
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
| 272 |
}
|
| 273 |
|
| 274 |
-
.
|
| 275 |
-
transform: translateY(-2px);
|
| 276 |
-
box-shadow: 0 4px 8px rgba(0,0,0,0.15);
|
| 277 |
}
|
| 278 |
|
| 279 |
.instructions {
|
|
@@ -311,108 +339,133 @@ st.markdown("""
|
|
| 311 |
border: 2px dashed #ccc;
|
| 312 |
}
|
| 313 |
|
| 314 |
-
/*
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
</style>
|
| 319 |
""", unsafe_allow_html=True)
|
| 320 |
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
# Game container
|
| 325 |
-
game = st.session_state.game
|
| 326 |
-
|
| 327 |
-
# Status card (moves counter or congratulations)
|
| 328 |
-
if game.game_won:
|
| 329 |
-
st.markdown(f"""
|
| 330 |
-
<div class="congratulations-card">
|
| 331 |
-
<div class="congratulations-title">π Congratulations!</div>
|
| 332 |
-
<div class="congratulations-subtitle">Puzzle solved in {game.moves} moves!</div>
|
| 333 |
-
</div>
|
| 334 |
-
""", unsafe_allow_html=True)
|
| 335 |
-
else:
|
| 336 |
-
st.markdown(f"""
|
| 337 |
-
<div class="moves-counter">
|
| 338 |
-
<div class="moves-value">{game.moves}</div>
|
| 339 |
-
<div class="moves-label">Moves</div>
|
| 340 |
-
</div>
|
| 341 |
-
""", unsafe_allow_html=True)
|
| 342 |
-
|
| 343 |
-
# Puzzle grid
|
| 344 |
-
st.markdown('<div class="puzzle-grid">', unsafe_allow_html=True)
|
| 345 |
-
|
| 346 |
-
# Create 3x3 grid of tiles
|
| 347 |
-
cols = st.columns(3)
|
| 348 |
-
for i in range(9):
|
| 349 |
-
col_idx = i % 3
|
| 350 |
-
|
| 351 |
-
with cols[col_idx]:
|
| 352 |
-
if game.puzzle and len(game.puzzle) > i:
|
| 353 |
-
value = game.puzzle[i]
|
| 354 |
-
|
| 355 |
-
if value == 0:
|
| 356 |
-
# Empty tile
|
| 357 |
-
st.markdown('<div class="empty-tile puzzle-tile"></div>', unsafe_allow_html=True)
|
| 358 |
-
else:
|
| 359 |
-
# Number tile - make it clickable
|
| 360 |
-
if st.button(str(value), key=f"tile_{i+1}", help=f"Move tile {value}"):
|
| 361 |
-
game.make_move(i + 1)
|
| 362 |
-
st.rerun()
|
| 363 |
-
|
| 364 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 365 |
-
|
| 366 |
-
# Control buttons
|
| 367 |
-
col1, col2 = st.columns(2)
|
| 368 |
-
|
| 369 |
-
with col1:
|
| 370 |
-
if st.button("π² New Game", key="new_game", type="primary"):
|
| 371 |
-
game.new_game()
|
| 372 |
-
st.rerun()
|
| 373 |
-
|
| 374 |
-
with col2:
|
| 375 |
-
if st.button("π Reset", key="reset_game", type="secondary"):
|
| 376 |
-
game.reset_game()
|
| 377 |
-
st.rerun()
|
| 378 |
-
|
| 379 |
-
# Instructions
|
| 380 |
-
with st.expander("π― How to Play", expanded=False):
|
| 381 |
st.markdown("""
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
|
| 389 |
-
|
| 390 |
-
""
|
| 391 |
|
| 392 |
-
#
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
</div>
|
| 406 |
-
|
| 407 |
-
|
|
|
|
| 408 |
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
st.write("Current puzzle state:", game.puzzle)
|
| 412 |
-
st.write("Moves:", game.moves)
|
| 413 |
-
st.write("Game won:", game.game_won)
|
| 414 |
-
if game.puzzle:
|
| 415 |
-
blank_pos = game.get_blank_position(game.puzzle)
|
| 416 |
-
valid_moves = game.get_valid_moves(blank_pos)
|
| 417 |
-
st.write("Blank position:", blank_pos)
|
| 418 |
-
st.write("Valid moves:", valid_moves)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import random
|
| 3 |
+
import os
|
| 4 |
from typing import List, Tuple, Dict, Optional
|
| 5 |
|
| 6 |
+
# Fix for Hugging Face Spaces - set Streamlit config directory
|
| 7 |
+
os.environ['STREAMLIT_CONFIG_DIR'] = '/tmp/.streamlit'
|
| 8 |
+
os.makedirs('/tmp/.streamlit', exist_ok=True)
|
| 9 |
+
|
| 10 |
+
# Streamlit configuration for Hugging Face Spaces
|
| 11 |
+
st.set_page_config(
|
| 12 |
+
page_title="Puzzle8 Game",
|
| 13 |
+
page_icon="π§©",
|
| 14 |
+
layout="centered",
|
| 15 |
+
initial_sidebar_state="collapsed"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
# Constants
|
| 19 |
SOLVED_STATE = [1, 2, 3, 4, 5, 6, 7, 8, 0]
|
| 20 |
|
|
|
|
| 158 |
# Custom CSS for styling
|
| 159 |
st.markdown("""
|
| 160 |
<style>
|
| 161 |
+
/* Hide Streamlit elements */
|
| 162 |
+
#MainMenu {visibility: hidden;}
|
| 163 |
+
footer {visibility: hidden;}
|
| 164 |
+
header {visibility: hidden;}
|
| 165 |
+
.stDeployButton {display: none;}
|
| 166 |
+
|
| 167 |
.main > div {
|
| 168 |
+
padding-top: 1rem;
|
| 169 |
+
padding-bottom: 1rem;
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
.stApp {
|
| 173 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 174 |
+
min-height: 100vh;
|
| 175 |
}
|
| 176 |
|
| 177 |
.game-container {
|
|
|
|
| 182 |
margin: 20px auto;
|
| 183 |
text-align: center;
|
| 184 |
max-width: 600px;
|
| 185 |
+
backdrop-filter: blur(10px);
|
| 186 |
}
|
| 187 |
|
| 188 |
.puzzle-grid {
|
|
|
|
| 195 |
padding: 20px;
|
| 196 |
background: rgba(0,0,0,0.05);
|
| 197 |
border-radius: 8px;
|
| 198 |
+
max-width: 350px;
|
| 199 |
}
|
| 200 |
|
| 201 |
+
.stButton > button {
|
| 202 |
+
width: 100px !important;
|
| 203 |
+
height: 100px !important;
|
| 204 |
+
border: none !important;
|
| 205 |
+
border-radius: 8px !important;
|
| 206 |
+
background: linear-gradient(145deg, #2196F3, #1976D2) !important;
|
| 207 |
+
color: white !important;
|
| 208 |
+
font-size: 24px !important;
|
| 209 |
+
font-weight: 700 !important;
|
| 210 |
+
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
|
| 211 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1) !important;
|
| 212 |
+
margin: 0 !important;
|
| 213 |
+
padding: 0 !important;
|
|
|
|
|
|
|
| 214 |
}
|
| 215 |
|
| 216 |
+
.stButton > button:hover {
|
| 217 |
+
transform: translateY(-2px) scale(1.02) !important;
|
| 218 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.15) !important;
|
| 219 |
}
|
| 220 |
|
| 221 |
.empty-tile {
|
| 222 |
+
width: 100px;
|
| 223 |
+
height: 100px;
|
| 224 |
background: rgba(255,255,255,0.8);
|
| 225 |
border: 2px dashed #ccc;
|
| 226 |
box-shadow: inset 0 2px 4px rgba(0,0,0,0.1);
|
| 227 |
+
border-radius: 8px;
|
| 228 |
+
margin: 0;
|
| 229 |
}
|
| 230 |
|
| 231 |
.moves-counter {
|
|
|
|
| 287 |
opacity: 0.9;
|
| 288 |
}
|
| 289 |
|
| 290 |
+
.control-button > button {
|
| 291 |
+
width: 140px !important;
|
| 292 |
+
height: 45px !important;
|
| 293 |
+
border-radius: 8px !important;
|
| 294 |
+
font-weight: 600 !important;
|
| 295 |
+
font-size: 16px !important;
|
| 296 |
+
text-transform: uppercase !important;
|
| 297 |
+
letter-spacing: 0.5px !important;
|
| 298 |
+
margin: 5px !important;
|
| 299 |
+
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
|
| 300 |
}
|
| 301 |
|
| 302 |
+
.control-button > button:hover {
|
| 303 |
+
transform: translateY(-2px) !important;
|
| 304 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.15) !important;
|
| 305 |
}
|
| 306 |
|
| 307 |
.instructions {
|
|
|
|
| 339 |
border: 2px dashed #ccc;
|
| 340 |
}
|
| 341 |
|
| 342 |
+
/* Mobile responsive */
|
| 343 |
+
@media (max-width: 768px) {
|
| 344 |
+
.puzzle-grid {
|
| 345 |
+
grid-template-columns: repeat(3, 80px);
|
| 346 |
+
grid-template-rows: repeat(3, 80px);
|
| 347 |
+
max-width: 280px;
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
.stButton > button {
|
| 351 |
+
width: 80px !important;
|
| 352 |
+
height: 80px !important;
|
| 353 |
+
font-size: 20px !important;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
.empty-tile {
|
| 357 |
+
width: 80px;
|
| 358 |
+
height: 80px;
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
.game-container {
|
| 362 |
+
padding: 20px;
|
| 363 |
+
margin: 10px;
|
| 364 |
+
}
|
| 365 |
+
}
|
| 366 |
</style>
|
| 367 |
""", unsafe_allow_html=True)
|
| 368 |
|
| 369 |
+
def main():
|
| 370 |
+
# Page title
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
st.markdown("""
|
| 372 |
+
<div class="game-container">
|
| 373 |
+
<h1 style='text-align: center; background: linear-gradient(145deg, #2196F3, #1976D2); -webkit-background-clip: text; -webkit-text-fill-color: transparent; font-weight: 800; font-size: 3rem; margin-bottom: 30px;'>
|
| 374 |
+
π§© Puzzle8
|
| 375 |
+
</h1>
|
| 376 |
+
""", unsafe_allow_html=True)
|
| 377 |
|
| 378 |
+
# Game container
|
| 379 |
+
game = st.session_state.game
|
| 380 |
+
|
| 381 |
+
# Status card (moves counter or congratulations)
|
| 382 |
+
if game.game_won:
|
| 383 |
+
st.markdown(f"""
|
| 384 |
+
<div class="congratulations-card">
|
| 385 |
+
<div class="congratulations-title">π Congratulations!</div>
|
| 386 |
+
<div class="congratulations-subtitle">Puzzle solved in {game.moves} moves!</div>
|
| 387 |
+
</div>
|
| 388 |
+
""", unsafe_allow_html=True)
|
| 389 |
+
else:
|
| 390 |
+
st.markdown(f"""
|
| 391 |
+
<div class="moves-counter">
|
| 392 |
+
<div class="moves-value">{game.moves}</div>
|
| 393 |
+
<div class="moves-label">Moves</div>
|
| 394 |
+
</div>
|
| 395 |
+
""", unsafe_allow_html=True)
|
| 396 |
|
| 397 |
+
# Puzzle grid
|
| 398 |
+
st.markdown('<div class="puzzle-grid">', unsafe_allow_html=True)
|
| 399 |
|
| 400 |
+
# Create 3x3 grid of tiles
|
| 401 |
+
for row in range(3):
|
| 402 |
+
cols = st.columns(3)
|
| 403 |
+
for col in range(3):
|
| 404 |
+
i = row * 3 + col
|
| 405 |
+
|
| 406 |
+
with cols[col]:
|
| 407 |
+
if game.puzzle and len(game.puzzle) > i:
|
| 408 |
+
value = game.puzzle[i]
|
| 409 |
+
|
| 410 |
+
if value == 0:
|
| 411 |
+
# Empty tile
|
| 412 |
+
st.markdown('<div class="empty-tile"></div>', unsafe_allow_html=True)
|
| 413 |
+
else:
|
| 414 |
+
# Number tile - make it clickable
|
| 415 |
+
if st.button(str(value), key=f"tile_{i+1}"):
|
| 416 |
+
game.make_move(i + 1)
|
| 417 |
+
st.rerun()
|
| 418 |
+
|
| 419 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 420 |
+
|
| 421 |
+
# Control buttons
|
| 422 |
+
col1, col2 = st.columns(2)
|
| 423 |
+
|
| 424 |
+
with col1:
|
| 425 |
+
st.markdown('<div class="control-button">', unsafe_allow_html=True)
|
| 426 |
+
if st.button("π² New Game", key="new_game", type="primary"):
|
| 427 |
+
game.new_game()
|
| 428 |
+
st.rerun()
|
| 429 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 430 |
+
|
| 431 |
+
with col2:
|
| 432 |
+
st.markdown('<div class="control-button">', unsafe_allow_html=True)
|
| 433 |
+
if st.button("π Reset", key="reset_game", type="secondary"):
|
| 434 |
+
game.reset_game()
|
| 435 |
+
st.rerun()
|
| 436 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 437 |
+
|
| 438 |
+
# Instructions
|
| 439 |
+
with st.expander("π― How to Play", expanded=False):
|
| 440 |
+
st.markdown("""
|
| 441 |
+
**Goal:** Arrange numbers 1-8 in order with the empty space in the bottom-right corner.
|
| 442 |
+
|
| 443 |
+
**Rules:**
|
| 444 |
+
- Click on tiles adjacent to the empty space to move them
|
| 445 |
+
- Only tiles next to the empty space can be moved
|
| 446 |
+
- Try to solve the puzzle in as few moves as possible!
|
| 447 |
+
|
| 448 |
+
**Target arrangement:**
|
| 449 |
+
""")
|
| 450 |
+
|
| 451 |
+
# Goal grid visualization
|
| 452 |
+
st.markdown("""
|
| 453 |
+
<div style="text-align: center;">
|
| 454 |
+
<div class="goal-grid">
|
| 455 |
+
<div class="goal-tile">1</div>
|
| 456 |
+
<div class="goal-tile">2</div>
|
| 457 |
+
<div class="goal-tile">3</div>
|
| 458 |
+
<div class="goal-tile">4</div>
|
| 459 |
+
<div class="goal-tile">5</div>
|
| 460 |
+
<div class="goal-tile">6</div>
|
| 461 |
+
<div class="goal-tile">7</div>
|
| 462 |
+
<div class="goal-tile">8</div>
|
| 463 |
+
<div class="goal-tile goal-empty"></div>
|
| 464 |
+
</div>
|
| 465 |
</div>
|
| 466 |
+
""", unsafe_allow_html=True)
|
| 467 |
+
|
| 468 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 469 |
|
| 470 |
+
if __name__ == "__main__":
|
| 471 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|