File size: 2,968 Bytes
b7e93cf
 
 
 
 
 
 
 
 
d6abb83
b7e93cf
 
 
d6abb83
b7e93cf
d6abb83
 
b7e93cf
 
606367f
 
 
 
 
 
 
 
d6abb83
606367f
 
 
 
 
d6abb83
606367f
 
 
d6abb83
606367f
b7e93cf
606367f
 
 
 
 
b7e93cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
606367f
b7e93cf
 
d6abb83
 
 
b7e93cf
d6abb83
b7e93cf
d6abb83
 
 
 
 
b7e93cf
d6abb83
 
b7e93cf
 
 
 
 
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
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."""
    original_pos = player_pos
    player_pos += dice
    if player_pos in board:
        player_pos = board[player_pos]
        if player_pos > original_pos:
            st.write("Yay! You climbed a ladder.")
        elif player_pos < original_pos:
             st.write("Oops! You encountered a snake")
    return player_pos

def draw_board(player_position):
    """Draws the game board using Streamlit."""
    board_size = 10
    board_squares = []
    for i in range(100):
        row = i // board_size
        col = i % board_size
        square_number = i + 1

         #Adjust the number according to the row odd or even
        if row % 2 == 1:
            square_number = (row * board_size) + (board_size - col)
        else:
            square_number = (row * board_size) + col + 1

        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_number}</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

    draw_board(player_pos)

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