Vishwas1 commited on
Commit
ca7fb07
·
verified ·
1 Parent(s): 606367f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -10
app.py CHANGED
@@ -1,23 +1,30 @@
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
  original_pos = player_pos
11
  player_pos += dice
 
12
  if player_pos in board:
13
  player_pos = board[player_pos]
14
  if player_pos > original_pos:
15
- st.write("Yay! You climbed a ladder.")
16
  elif player_pos < original_pos:
17
- st.write("Oops! You encountered a snake")
 
 
 
 
 
18
  return player_pos
19
 
20
- def draw_board(player_position):
21
  """Draws the game board using Streamlit."""
22
  board_size = 10
23
  board_squares = []
@@ -26,17 +33,23 @@ def draw_board(player_position):
26
  col = i % board_size
27
  square_number = i + 1
28
 
29
- #Adjust the number according to the row odd or even
30
  if row % 2 == 1:
31
  square_number = (row * board_size) + (board_size - col)
32
  else:
33
  square_number = (row * board_size) + col + 1
34
 
 
 
 
 
 
 
 
35
  square_style = "background-color: lightgray; padding: 10px; border: 1px solid black; text-align: center;"
36
  if square_number == player_position:
37
  square_style = "background-color: red; padding: 10px; border: 1px solid black; text-align: center; color:white; font-weight: bold;"
38
-
39
- board_squares.append(f"<div style='{square_style}'>{square_number}</div>")
40
 
41
  # create a layout of 10 rows each with 10 columns
42
  for row in range(board_size):
@@ -65,18 +78,22 @@ def snake_and_ladder():
65
 
66
  player_pos = st.session_state.player_pos
67
 
68
- draw_board(player_pos)
 
69
 
70
  if player_pos < 100:
71
  if st.button("Roll Dice"):
72
  dice = roll_dice()
73
  st.write(f"You rolled a {dice}")
74
 
75
- player_pos = update_player_position(player_pos, dice, board)
76
 
77
  if player_pos > 100:
78
  player_pos -= dice
79
- st.write("You need to get to 100 to win, Try Again!")
 
 
 
80
  else:
81
  st.write(f"Your position is {player_pos}")
82
 
 
1
  import random
2
  import streamlit as st
3
+ import time
4
 
5
  def roll_dice():
6
  """Simulates rolling a six-sided dice."""
7
  return random.randint(1, 6)
8
 
9
+ def update_player_position(player_pos, dice, board, message_placeholder):
10
  """Updates player position based on the dice roll and board."""
11
  original_pos = player_pos
12
  player_pos += dice
13
+ message = ""
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
+ if message:
21
+ with message_placeholder:
22
+ st.write(message)
23
+ time.sleep(2)
24
+ message_placeholder.empty()
25
  return player_pos
26
 
27
+ def draw_board(player_position, board):
28
  """Draws the game board using Streamlit."""
29
  board_size = 10
30
  board_squares = []
 
33
  col = i % board_size
34
  square_number = i + 1
35
 
36
+ # Adjust the number according to the row odd or even
37
  if row % 2 == 1:
38
  square_number = (row * board_size) + (board_size - col)
39
  else:
40
  square_number = (row * board_size) + col + 1
41
 
42
+ square_content = str(square_number)
43
+ if square_number in board:
44
+ if board[square_number] > square_number:
45
+ square_content = f"{square_number} ⬆️" #Ladder Icon
46
+ elif board[square_number] < square_number:
47
+ square_content = f"{square_number} 🐍" #Snake Icon
48
+
49
  square_style = "background-color: lightgray; padding: 10px; border: 1px solid black; text-align: center;"
50
  if square_number == player_position:
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):
 
78
 
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"):
86
  dice = roll_dice()
87
  st.write(f"You rolled a {dice}")
88
 
89
+ player_pos = update_player_position(player_pos, dice, board, message_placeholder)
90
 
91
  if player_pos > 100:
92
  player_pos -= dice
93
+ with message_placeholder:
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