Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,14 +14,12 @@ def update_player_position(player_pos, dice, board, message_placeholder):
|
|
| 14 |
if player_pos in board:
|
| 15 |
player_pos = board[player_pos]
|
| 16 |
if player_pos > original_pos:
|
| 17 |
-
message = "Yay! You climbed a ladder.
|
| 18 |
elif player_pos < original_pos:
|
| 19 |
-
message = "Oops! You encountered a snake.
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
time.sleep(2)
|
| 24 |
-
message_placeholder.empty()
|
| 25 |
return player_pos
|
| 26 |
|
| 27 |
def draw_board(player_position, board):
|
|
@@ -29,15 +27,13 @@ def draw_board(player_position, board):
|
|
| 29 |
board_size = 10
|
| 30 |
board_squares = []
|
| 31 |
for i in range(100):
|
| 32 |
-
row = i // board_size
|
| 33 |
col = i % board_size
|
| 34 |
-
square_number = i + 1
|
| 35 |
|
| 36 |
-
# Adjust
|
|
|
|
| 37 |
if row % 2 == 1:
|
| 38 |
-
|
| 39 |
-
else:
|
| 40 |
-
square_number = (row * board_size) + col + 1
|
| 41 |
|
| 42 |
square_content = str(square_number)
|
| 43 |
if square_number in board:
|
|
@@ -51,6 +47,7 @@ def draw_board(player_position, board):
|
|
| 51 |
square_style = "background-color: red; padding: 10px; border: 1px solid black; text-align: center; color:white; font-weight: bold;"
|
| 52 |
board_squares.append(f"<div style='{square_style}'>{square_content}</div>")
|
| 53 |
|
|
|
|
| 54 |
# create a layout of 10 rows each with 10 columns
|
| 55 |
for row in range(board_size):
|
| 56 |
cols = st.columns(board_size)
|
|
@@ -58,6 +55,7 @@ def draw_board(player_position, board):
|
|
| 58 |
cols[col].markdown(board_squares[(row * board_size) + col], unsafe_allow_html=True)
|
| 59 |
|
| 60 |
|
|
|
|
| 61 |
def snake_and_ladder():
|
| 62 |
st.title("Snake and Ladder Game")
|
| 63 |
|
|
@@ -79,7 +77,6 @@ def snake_and_ladder():
|
|
| 79 |
player_pos = st.session_state.player_pos
|
| 80 |
|
| 81 |
message_placeholder = st.empty()
|
| 82 |
-
draw_board(player_pos, board)
|
| 83 |
|
| 84 |
if player_pos < 100:
|
| 85 |
if st.button("Roll Dice"):
|
|
@@ -90,18 +87,16 @@ def snake_and_ladder():
|
|
| 90 |
|
| 91 |
if player_pos > 100:
|
| 92 |
player_pos -= dice
|
| 93 |
-
|
| 94 |
-
st.write("You need to get to 100 to win, Try Again!")
|
| 95 |
-
time.sleep(2)
|
| 96 |
-
message_placeholder.empty()
|
| 97 |
else:
|
| 98 |
st.write(f"Your position is {player_pos}")
|
| 99 |
|
| 100 |
st.session_state.player_pos = player_pos
|
| 101 |
st.rerun()
|
| 102 |
else:
|
| 103 |
-
|
| 104 |
if st.button("Play Again"):
|
| 105 |
st.session_state.player_pos = 1
|
| 106 |
st.rerun()
|
|
|
|
| 107 |
snake_and_ladder()
|
|
|
|
| 14 |
if player_pos in board:
|
| 15 |
player_pos = board[player_pos]
|
| 16 |
if player_pos > original_pos:
|
| 17 |
+
message = "Yay! You climbed a ladder."
|
| 18 |
elif player_pos < original_pos:
|
| 19 |
+
message = "Oops! You encountered a snake."
|
| 20 |
+
|
| 21 |
+
message_placeholder.write(message)
|
| 22 |
+
|
|
|
|
|
|
|
| 23 |
return player_pos
|
| 24 |
|
| 25 |
def draw_board(player_position, board):
|
|
|
|
| 27 |
board_size = 10
|
| 28 |
board_squares = []
|
| 29 |
for i in range(100):
|
| 30 |
+
row = (99 - i) // board_size # Reversed row calculation
|
| 31 |
col = i % board_size
|
|
|
|
| 32 |
|
| 33 |
+
# Adjust numbering for snake-ladder layout, start from bottom left
|
| 34 |
+
square_number = (row * board_size) + col + 1
|
| 35 |
if row % 2 == 1:
|
| 36 |
+
square_number = (row * board_size) + (board_size - col)
|
|
|
|
|
|
|
| 37 |
|
| 38 |
square_content = str(square_number)
|
| 39 |
if square_number in board:
|
|
|
|
| 47 |
square_style = "background-color: red; padding: 10px; border: 1px solid black; text-align: center; color:white; font-weight: bold;"
|
| 48 |
board_squares.append(f"<div style='{square_style}'>{square_content}</div>")
|
| 49 |
|
| 50 |
+
|
| 51 |
# create a layout of 10 rows each with 10 columns
|
| 52 |
for row in range(board_size):
|
| 53 |
cols = st.columns(board_size)
|
|
|
|
| 55 |
cols[col].markdown(board_squares[(row * board_size) + col], unsafe_allow_html=True)
|
| 56 |
|
| 57 |
|
| 58 |
+
|
| 59 |
def snake_and_ladder():
|
| 60 |
st.title("Snake and Ladder Game")
|
| 61 |
|
|
|
|
| 77 |
player_pos = st.session_state.player_pos
|
| 78 |
|
| 79 |
message_placeholder = st.empty()
|
|
|
|
| 80 |
|
| 81 |
if player_pos < 100:
|
| 82 |
if st.button("Roll Dice"):
|
|
|
|
| 87 |
|
| 88 |
if player_pos > 100:
|
| 89 |
player_pos -= dice
|
| 90 |
+
message_placeholder.write("You need to get to 100 to win, Try Again!")
|
|
|
|
|
|
|
|
|
|
| 91 |
else:
|
| 92 |
st.write(f"Your position is {player_pos}")
|
| 93 |
|
| 94 |
st.session_state.player_pos = player_pos
|
| 95 |
st.rerun()
|
| 96 |
else:
|
| 97 |
+
message_placeholder.success("Congratulations! You won!")
|
| 98 |
if st.button("Play Again"):
|
| 99 |
st.session_state.player_pos = 1
|
| 100 |
st.rerun()
|
| 101 |
+
draw_board(player_pos, board)
|
| 102 |
snake_and_ladder()
|