File size: 2,033 Bytes
b7e93cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random
import streamlit as st

def roll_dice():
    """Simulates rolling a six-sided dice."""
    return random.randint(1, 6)

def update_player_position(player_pos, dice, board):
    """Updates player position based on the dice roll and board."""
    player_pos += dice
    if player_pos in board:
        player_pos = board[player_pos]
        if player_pos > dice:
            st.write("Yay! You climbed a ladder.")
        else:
            st.write("Oops! You encountered a snake")
    return player_pos


def display_game_board():
  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"
  st.image(board_image, caption="Snake and Ladder Board", width=500)


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

    display_game_board()

    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)

          if player_pos > 100:
              player_pos -= dice
              st.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
    else:
        st.success("Congratulations! You won!")
        if st.button("Play Again"):
            st.session_state.player_pos = 1
            st.rerun()



if __name__ == "__main__":
    snake_and_ladder()