Dua Rajper commited on
Commit
0d631fb
·
verified ·
1 Parent(s): 9b1f8ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -9
app.py CHANGED
@@ -10,8 +10,8 @@ def initialize_grid(rows, cols):
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):
@@ -50,6 +50,18 @@ def remove_matches(grid, matches):
50
 
51
  return grid
52
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  # Main Streamlit app
54
  def main():
55
  st.title("Candy Crush Game")
@@ -65,14 +77,24 @@ def main():
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)
 
10
 
11
  # Display the grid
12
  def display_grid(grid):
13
+ for r, row in enumerate(grid):
14
+ st.write(" ".join([f"{c}({r},{col})" for col, c in enumerate(row)]))
15
 
16
  # Check for matches (horizontal and vertical)
17
  def check_matches(grid):
 
50
 
51
  return grid
52
 
53
+ # Swap candies in the grid
54
+ def swap_candies(grid, r1, c1, r2, c2):
55
+ if (
56
+ 0 <= r1 < len(grid)
57
+ and 0 <= c1 < len(grid[0])
58
+ and 0 <= r2 < len(grid)
59
+ and 0 <= c2 < len(grid[0])
60
+ ):
61
+ grid[r1][c1], grid[r2][c2] = grid[r2][c2], grid[r1][c1]
62
+ return True
63
+ return False
64
+
65
  # Main Streamlit app
66
  def main():
67
  st.title("Candy Crush Game")
 
77
  st.write("### Current Grid")
78
  display_grid(st.session_state.grid)
79
 
80
+ # Input to select candies to swap
81
+ st.write("### Swap Candies")
82
+ r1 = st.number_input("Row of first candy (0-5):", min_value=0, max_value=rows - 1, step=1)
83
+ c1 = st.number_input("Column of first candy (0-5):", min_value=0, max_value=cols - 1, step=1)
84
+ r2 = st.number_input("Row of second candy (0-5):", min_value=0, max_value=rows - 1, step=1)
85
+ c2 = st.number_input("Column of second candy (0-5):", min_value=0, max_value=cols - 1, step=1)
86
+
87
+ if st.button("Swap Candies"):
88
+ if swap_candies(st.session_state.grid, r1, c1, r2, c2):
89
+ matches = check_matches(st.session_state.grid)
90
+ if matches:
91
+ st.success("Valid move! Candies matched!")
92
+ st.session_state.grid = remove_matches(st.session_state.grid, matches)
93
+ else:
94
+ st.warning("No match found. Swap reverted!")
95
+ swap_candies(st.session_state.grid, r1, c1, r2, c2) # Undo the swap
96
  else:
97
+ st.error("Invalid swap! Please select valid coordinates.")
98
 
99
  st.write("### Updated Grid")
100
  display_grid(st.session_state.grid)