Spaces:
Sleeping
Sleeping
File size: 3,493 Bytes
b7e93cf ca7fb07 b7e93cf ca7fb07 b7e93cf d6abb83 b7e93cf ca7fb07 b7e93cf d6abb83 c7fb69f d6abb83 c7fb69f b7e93cf ca7fb07 606367f c7fb69f 606367f d6abb83 c7fb69f 606367f c7fb69f d6abb83 ca7fb07 606367f ca7fb07 b7e93cf c7fb69f 606367f b7e93cf c7fb69f b7e93cf ca7fb07 b7e93cf d6abb83 b7e93cf ca7fb07 b7e93cf d6abb83 c7fb69f d6abb83 b7e93cf d6abb83 b7e93cf c7fb69f b7e93cf c7fb69f d6abb83 |
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 |
import random
import streamlit as st
import time
def roll_dice():
"""Simulates rolling a six-sided dice."""
return random.randint(1, 6)
def update_player_position(player_pos, dice, board, message_placeholder):
"""Updates player position based on the dice roll and board."""
original_pos = player_pos
player_pos += dice
message = ""
if player_pos in board:
player_pos = board[player_pos]
if player_pos > original_pos:
message = "Yay! You climbed a ladder."
elif player_pos < original_pos:
message = "Oops! You encountered a snake."
message_placeholder.write(message)
return player_pos
def draw_board(player_position, board):
"""Draws the game board using Streamlit."""
board_size = 10
board_squares = []
for i in range(100):
row = (99 - i) // board_size # Reversed row calculation
col = i % board_size
# Adjust numbering for snake-ladder layout, start from bottom left
square_number = (row * board_size) + col + 1
if row % 2 == 1:
square_number = (row * board_size) + (board_size - col)
square_content = str(square_number)
if square_number in board:
if board[square_number] > square_number:
square_content = f"{square_number} ⬆️" #Ladder Icon
elif board[square_number] < square_number:
square_content = f"{square_number} 🐍" #Snake Icon
square_style = "background-color: lightgray; padding: 10px; border: 1px solid black; text-align: center;"
if square_number == player_position:
square_style = "background-color: red; padding: 10px; border: 1px solid black; text-align: center; color:white; font-weight: bold;"
board_squares.append(f"<div style='{square_style}'>{square_content}</div>")
# create a layout of 10 rows each with 10 columns
for row in range(board_size):
cols = st.columns(board_size)
for col in range(board_size):
cols[col].markdown(board_squares[(row * board_size) + col], unsafe_allow_html=True)
def snake_and_ladder():
st.title("Snake and Ladder Game")
board = {
1: 1, 4: 25, 8: 13, 11: 6, 14: 30,
16: 10, 19: 3, 22: 37, 24: 16,
26: 12, 28: 43, 33: 2, 35: 17,
39: 50, 41: 20, 46: 5, 48: 63,
52: 81, 55: 18, 57: 59, 60: 32,
62: 65, 64: 45, 67: 71, 73: 69,
76: 44, 78: 54, 80: 99, 82: 89,
84: 21, 87: 100, 91: 68, 93: 70,
95: 23, 98: 79
}
if 'player_pos' not in st.session_state:
st.session_state.player_pos = 1
player_pos = st.session_state.player_pos
message_placeholder = st.empty()
if player_pos < 100:
if st.button("Roll Dice"):
dice = roll_dice()
st.write(f"You rolled a {dice}")
player_pos = update_player_position(player_pos, dice, board, message_placeholder)
if player_pos > 100:
player_pos -= dice
message_placeholder.write("You need to get to 100 to win, Try Again!")
else:
st.write(f"Your position is {player_pos}")
st.session_state.player_pos = player_pos
st.rerun()
else:
message_placeholder.success("Congratulations! You won!")
if st.button("Play Again"):
st.session_state.player_pos = 1
st.rerun()
draw_board(player_pos, board)
snake_and_ladder() |