Spaces:
Sleeping
Sleeping
Dua Rajper commited on
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import randomimport streamlit as st
# Candy typesCANDY_TYPES = ["π¬", "π«", "π", "πͺ", "π"]
# Initialize the game griddef initialize_grid(rows, cols): return [[random.choice(CANDY_TYPES) for _ in range(cols)] for _ in range(rows)]
# Display the griddef display_grid(grid): for row in grid: st.write(" ".join(row))
# 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 candiesdef 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
# Main Streamlit appdef 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)
# Display the current grid st.write("### Current Grid") display_grid(st.session_state.grid)
# Check for matches if st.button("Check Matches"): matches = check_matches(st.session_state.grid) if matches: st.success(f"Found {len(matches)} candies to crush!") st.session_state.grid = remove_matches(st.session_state.grid, matches) else: st.warning("No matches found! Try again.")
st.write("### Updated Grid") display_grid(st.session_state.grid)
# Reset the game if st.button("Reset Game"): st.session_state.grid = initialize_grid(rows, cols) st.write("Game reset!") display_grid(st.session_state.grid)
if __name__ == "__main__": main()app.pyAdded+87