Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import streamlit as st
|
3 |
+
|
4 |
+
def roll_dice():
|
5 |
+
"""Simulates rolling a six-sided dice."""
|
6 |
+
return random.randint(1, 6)
|
7 |
+
|
8 |
+
def update_player_position(player_pos, dice, board):
|
9 |
+
"""Updates player position based on the dice roll and board."""
|
10 |
+
player_pos += dice
|
11 |
+
if player_pos in board:
|
12 |
+
player_pos = board[player_pos]
|
13 |
+
if player_pos > dice:
|
14 |
+
st.write("Yay! You climbed a ladder.")
|
15 |
+
else:
|
16 |
+
st.write("Oops! You encountered a snake")
|
17 |
+
return player_pos
|
18 |
+
|
19 |
+
|
20 |
+
def display_game_board():
|
21 |
+
board_image = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8f/Snakes_and_ladders_board_simple.svg/1920px-Snakes_and_ladders_board_simple.svg.png"
|
22 |
+
st.image(board_image, caption="Snake and Ladder Board", width=500)
|
23 |
+
|
24 |
+
|
25 |
+
def snake_and_ladder():
|
26 |
+
st.title("Snake and Ladder Game")
|
27 |
+
|
28 |
+
board = {
|
29 |
+
1: 1, 4: 25, 8: 13, 11: 6, 14: 30,
|
30 |
+
16: 10, 19: 3, 22: 37, 24: 16,
|
31 |
+
26: 12, 28: 43, 33: 2, 35: 17,
|
32 |
+
39: 50, 41: 20, 46: 5, 48: 63,
|
33 |
+
52: 81, 55: 18, 57: 59, 60: 32,
|
34 |
+
62: 65, 64: 45, 67: 71, 73: 69,
|
35 |
+
76: 44, 78: 54, 80: 99, 82: 89,
|
36 |
+
84: 21, 87: 100, 91: 68, 93: 70,
|
37 |
+
95: 23, 98: 79
|
38 |
+
}
|
39 |
+
|
40 |
+
if 'player_pos' not in st.session_state:
|
41 |
+
st.session_state.player_pos = 1
|
42 |
+
|
43 |
+
player_pos = st.session_state.player_pos
|
44 |
+
|
45 |
+
display_game_board()
|
46 |
+
|
47 |
+
if player_pos < 100:
|
48 |
+
if st.button("Roll Dice"):
|
49 |
+
dice = roll_dice()
|
50 |
+
st.write(f"You rolled a {dice}")
|
51 |
+
|
52 |
+
player_pos = update_player_position(player_pos, dice, board)
|
53 |
+
|
54 |
+
if player_pos > 100:
|
55 |
+
player_pos -= dice
|
56 |
+
st.write("You need to get to 100 to win, Try Again!")
|
57 |
+
else:
|
58 |
+
st.write(f"Your position is {player_pos}")
|
59 |
+
|
60 |
+
st.session_state.player_pos = player_pos
|
61 |
+
else:
|
62 |
+
st.success("Congratulations! You won!")
|
63 |
+
if st.button("Play Again"):
|
64 |
+
st.session_state.player_pos = 1
|
65 |
+
st.rerun()
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
snake_and_ladder()
|