Spaces:
Sleeping
Sleeping
Dua Rajper commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
# Candy types
|
| 5 |
+
CANDY_TYPES = ["π¬", "π«", "π", "πͺ", "π"]
|
| 6 |
+
|
| 7 |
+
# Initialize the game grid
|
| 8 |
+
def initialize_grid(rows, cols):
|
| 9 |
+
return [[random.choice(CANDY_TYPES) for _ in range(cols)] for _ in range(rows)]
|
| 10 |
+
|
| 11 |
+
# Display the grid
|
| 12 |
+
def display_grid(grid):
|
| 13 |
+
for row in grid:
|
| 14 |
+
st.write(" ".join(row))
|
| 15 |
+
|
| 16 |
+
# Check for matches (horizontal and vertical)
|
| 17 |
+
def check_matches(grid):
|
| 18 |
+
rows, cols = len(grid), len(grid[0])
|
| 19 |
+
matches = set()
|
| 20 |
+
|
| 21 |
+
# Check horizontal matches
|
| 22 |
+
for r in range(rows):
|
| 23 |
+
for c in range(cols - 2):
|
| 24 |
+
if grid[r][c] == grid[r][c + 1] == grid[r][c + 2]:
|
| 25 |
+
matches.update([(r, c), (r, c + 1), (r, c + 2)])
|
| 26 |
+
|
| 27 |
+
# Check vertical matches
|
| 28 |
+
for r in range(rows - 2):
|
| 29 |
+
for c in range(cols):
|
| 30 |
+
if grid[r][c] == grid[r + 1][c] == grid[r + 2][c]:
|
| 31 |
+
matches.update([(r, c), (r + 1, c), (r + 2, c)])
|
| 32 |
+
|
| 33 |
+
return matches
|
| 34 |
+
|
| 35 |
+
# Remove matched candies and drop new candies
|
| 36 |
+
def remove_matches(grid, matches):
|
| 37 |
+
rows, cols = len(grid), len(grid[0])
|
| 38 |
+
|
| 39 |
+
# Set matched candies to None
|
| 40 |
+
for r, c in matches:
|
| 41 |
+
grid[r][c] = None
|
| 42 |
+
|
| 43 |
+
# Drop candies down and refill the grid
|
| 44 |
+
for c in range(cols):
|
| 45 |
+
column = [grid[r][c] for r in range(rows) if grid[r][c] is not None]
|
| 46 |
+
while len(column) < rows:
|
| 47 |
+
column.insert(0, random.choice(CANDY_TYPES))
|
| 48 |
+
for r in range(rows):
|
| 49 |
+
grid[r][c] = column[r]
|
| 50 |
+
|
| 51 |
+
return grid
|
| 52 |
+
|
| 53 |
+
# Main Streamlit app
|
| 54 |
+
def main():
|
| 55 |
+
st.title("Candy Crush Game")
|
| 56 |
+
st.write("Match 3 or more candies to crush them! π¬π«π")
|
| 57 |
+
|
| 58 |
+
rows, cols = 6, 6 # Grid size
|
| 59 |
+
|
| 60 |
+
# Initialize or reset the grid
|
| 61 |
+
if "grid" not in st.session_state:
|
| 62 |
+
st.session_state.grid = initialize_grid(rows, cols)
|
| 63 |
+
|
| 64 |
+
# Display the current grid
|
| 65 |
+
st.write("### Current Grid")
|
| 66 |
+
display_grid(st.session_state.grid)
|
| 67 |
+
|
| 68 |
+
# Check for matches
|
| 69 |
+
if st.button("Check Matches"):
|
| 70 |
+
matches = check_matches(st.session_state.grid)
|
| 71 |
+
if matches:
|
| 72 |
+
st.success(f"Found {len(matches)} candies to crush!")
|
| 73 |
+
st.session_state.grid = remove_matches(st.session_state.grid, matches)
|
| 74 |
+
else:
|
| 75 |
+
st.warning("No matches found! Try again.")
|
| 76 |
+
|
| 77 |
+
st.write("### Updated Grid")
|
| 78 |
+
display_grid(st.session_state.grid)
|
| 79 |
+
|
| 80 |
+
# Reset the game
|
| 81 |
+
if st.button("Reset Game"):
|
| 82 |
+
st.session_state.grid = initialize_grid(rows, cols)
|
| 83 |
+
st.write("Game reset!")
|
| 84 |
+
display_grid(st.session_state.grid)
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
main()
|