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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -36
app.py CHANGED
@@ -8,10 +8,18 @@ CANDY_TYPES = ["🍬", "🍫", "🍭", "🍪", "🍓"]
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 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):
@@ -51,15 +59,18 @@ def remove_matches(grid, matches):
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
@@ -72,38 +83,36 @@ def main():
72
  # Initialize or reset the grid
73
  if "grid" not in st.session_state:
74
  st.session_state.grid = initialize_grid(rows, cols)
 
75
 
76
- # Display the current grid
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  # Reset the game
103
  if st.button("Reset Game"):
104
  st.session_state.grid = initialize_grid(rows, cols)
 
105
  st.write("Game reset!")
106
- display_grid(st.session_state.grid)
107
 
108
  if __name__ == "__main__":
109
  main()
 
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 as buttons
12
+ def display_grid(grid, selected):
13
  for r, row in enumerate(grid):
14
+ cols = st.columns(len(row))
15
+ for c, candy in enumerate(row):
16
+ if (r, c) == selected:
17
+ button_label = f"**{candy}**" # Highlight selected candy
18
+ else:
19
+ button_label = candy
20
+ if cols[c].button(button_label):
21
+ return (r, c) # Return the coordinates of the clicked candy
22
+ return None
23
 
24
  # Check for matches (horizontal and vertical)
25
  def check_matches(grid):
 
59
  return grid
60
 
61
  # Swap candies in the grid
62
+ def swap_candies(grid, first, second):
63
+ if first and second:
64
+ r1, c1 = first
65
+ r2, c2 = second
66
+ if (
67
+ 0 <= r1 < len(grid)
68
+ and 0 <= c1 < len(grid[0])
69
+ and 0 <= r2 < len(grid)
70
+ and 0 <= c2 < len(grid[0])
71
+ ):
72
+ grid[r1][c1], grid[r2][c2] = grid[r2][c2], grid[r1][c1]
73
+ return True
74
  return False
75
 
76
  # Main Streamlit app
 
83
  # Initialize or reset the grid
84
  if "grid" not in st.session_state:
85
  st.session_state.grid = initialize_grid(rows, cols)
86
+ st.session_state.selected_candy = None
87
 
88
+ # Display the current grid as buttons
89
  st.write("### Current Grid")
90
+ selected_candy = display_grid(st.session_state.grid, st.session_state.selected_candy)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
+ # Handle candy selection
93
+ if selected_candy:
94
+ if st.session_state.selected_candy is None:
95
+ st.session_state.selected_candy = selected_candy
96
+ st.info(f"Selected candy at {selected_candy}")
97
+ else:
98
+ # Attempt to swap the two candies
99
+ if swap_candies(st.session_state.grid, st.session_state.selected_candy, selected_candy):
100
+ matches = check_matches(st.session_state.grid)
101
+ if matches:
102
+ st.success("Valid move! Candies matched!")
103
+ st.session_state.grid = remove_matches(st.session_state.grid, matches)
104
+ else:
105
+ st.warning("No match found. Swap reverted!")
106
+ swap_candies(st.session_state.grid, st.session_state.selected_candy, selected_candy) # Undo the swap
107
+ else:
108
+ st.error("Invalid swap!")
109
+ st.session_state.selected_candy = None # Reset selection
110
 
111
  # Reset the game
112
  if st.button("Reset Game"):
113
  st.session_state.grid = initialize_grid(rows, cols)
114
+ st.session_state.selected_candy = None
115
  st.write("Game reset!")
 
116
 
117
  if __name__ == "__main__":
118
  main()