Spaces:
Sleeping
Sleeping
File size: 4,113 Bytes
eb5b3fc d0d0d6a 0d631fb d0d0d6a 9c9c914 d0d0d6a 9c9c914 d0d0d6a eb5b3fc 0d631fb d0d0d6a 0d631fb eb5b3fc d0d0d6a eb5b3fc d0d0d6a eb5b3fc d0d0d6a eb5b3fc d0d0d6a eb5b3fc d0d0d6a eb5b3fc 9c9c914 | 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | import random
import streamlit as st
# Candy types
CANDY_TYPES = ["π¬", "π«", "π", "πͺ", "π"]
# Initialize the game grid
def initialize_grid(rows, cols):
return [[random.choice(CANDY_TYPES) for _ in range(cols)] for _ in range(rows)]
# Display the grid as buttons
def display_grid(grid, selected):
for r, row in enumerate(grid):
cols = st.columns(len(row))
for c, candy in enumerate(row):
# Use row and column indices to generate a unique key
button_key = f"btn_{r}_{c}"
if (r, c) == selected:
button_label = f"**{candy}**" # Highlight selected candy
else:
button_label = candy
if cols[c].button(button_label, key=button_key):
return (r, c) # Return the coordinates of the clicked candy
return None
# Check for matches (horizontal and vertical)
def check_matches(grid):
rows, cols = len(grid), len(grid[0])
matches = set()
# Check horizontal matches
for r in range(rows):
for c in range(cols - 2):
if grid[r][c] == grid[r][c + 1] == grid[r][c + 2]:
matches.update([(r, c), (r, c + 1), (r, c + 2)])
# Check vertical matches
for r in range(rows - 2):
for c in range(cols):
if grid[r][c] == grid[r + 1][c] == grid[r + 2][c]:
matches.update([(r, c), (r + 1, c), (r + 2, c)])
return matches
# Remove matched candies and drop new candies
def remove_matches(grid, matches):
rows, cols = len(grid), len(grid[0])
# Set matched candies to None
for r, c in matches:
grid[r][c] = None
# Drop candies down and refill the grid
for c in range(cols):
column = [grid[r][c] for r in range(rows) if grid[r][c] is not None]
while len(column) < rows:
column.insert(0, random.choice(CANDY_TYPES))
for r in range(rows):
grid[r][c] = column[r]
return grid
# Swap candies in the grid
def swap_candies(grid, first, second):
if first and second:
r1, c1 = first
r2, c2 = second
if (
0 <= r1 < len(grid)
and 0 <= c1 < len(grid[0])
and 0 <= r2 < len(grid)
and 0 <= c2 < len(grid[0])
):
grid[r1][c1], grid[r2][c2] = grid[r2][c2], grid[r1][c1]
return True
return False
# Main Streamlit app
def main():
st.title("Candy Crush Game")
st.write("Match 3 or more candies to crush them! π¬π«π")
rows, cols = 6, 6 # Grid size
# Initialize or reset the grid
if "grid" not in st.session_state:
st.session_state.grid = initialize_grid(rows, cols)
st.session_state.selected_candy = None
# Display the current grid as buttons
st.write("### Current Grid")
selected_candy = display_grid(st.session_state.grid, st.session_state.selected_candy)
# Handle candy selection
if selected_candy:
if st.session_state.selected_candy is None:
st.session_state.selected_candy = selected_candy
st.info(f"Selected candy at {selected_candy}")
else:
# Attempt to swap the two candies
if swap_candies(st.session_state.grid, st.session_state.selected_candy, selected_candy):
matches = check_matches(st.session_state.grid)
if matches:
st.success("Valid move! Candies matched!")
st.session_state.grid = remove_matches(st.session_state.grid, matches)
else:
st.warning("No match found. Swap reverted!")
swap_candies(st.session_state.grid, st.session_state.selected_candy, selected_candy) # Undo the swap
else:
st.error("Invalid swap!")
st.session_state.selected_candy = None # Reset selection
# Reset the game
if st.button("Reset Game"):
st.session_state.grid = initialize_grid(rows, cols)
st.session_state.selected_candy = None
st.write("Game reset!")
if __name__ == "__main__":
main()
|