marvinisjarvis commited on
Commit
2308956
·
verified ·
1 Parent(s): 5b6f290

Update app.py

Browse files

Added a snippet that describes the app's action. Updated app.py with new code.

Files changed (1) hide show
  1. app.py +134 -0
app.py CHANGED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+
4
+ # === Game Configuration ===
5
+ BOARD_WIDTH = 10
6
+ BOARD_HEIGHT = 10
7
+ NUM_MINES = 15
8
+
9
+ # === Global Game State ===
10
+ game_state = {
11
+ "board": [],
12
+ "game_over": False,
13
+ "win": False
14
+ }
15
+
16
+ # === Board Initialization ===
17
+ def initialize_board(width, height, num_mines):
18
+ board = [[{'mine': False, 'revealed': False, 'flagged': False, 'adjacent': 0}
19
+ for _ in range(width)] for _ in range(height)]
20
+
21
+ # Random mine placement
22
+ mines = set()
23
+ while len(mines) < num_mines:
24
+ r = random.randint(0, height - 1)
25
+ c = random.randint(0, width - 1)
26
+ if not board[r][c]['mine']:
27
+ board[r][c]['mine'] = True
28
+ mines.add((r, c))
29
+
30
+ # Increment adjacent mine counts
31
+ for i in range(max(0, r - 1), min(height, r + 2)):
32
+ for j in range(max(0, c - 1), min(width, c + 2)):
33
+ if (i, j) != (r, c):
34
+ board[i][j]['adjacent'] += 1
35
+ return board
36
+
37
+ # === Flood Fill Reveal Logic ===
38
+ def reveal_tile(board, row, col):
39
+ if board[row][col]['revealed'] or board[row][col]['flagged']:
40
+ return
41
+
42
+ board[row][col]['revealed'] = True
43
+
44
+ # Reveal neighboring tiles recursively if no adjacent mines
45
+ if board[row][col]['adjacent'] == 0 and not board[row][col]['mine']:
46
+ for i in range(max(0, row - 1), min(len(board), row + 2)):
47
+ for j in range(max(0, col - 1), min(len(board[0]), col + 2)):
48
+ if not board[i][j]['revealed']:
49
+ reveal_tile(board, i, j)
50
+
51
+ # === Flagging ===
52
+ def toggle_flag(board, row, col):
53
+ if not board[row][col]['revealed']:
54
+ board[row][col]['flagged'] = not board[row][col]['flagged']
55
+
56
+ # === Win Condition ===
57
+ def check_win(board):
58
+ for row in board:
59
+ for tile in row:
60
+ if not tile['mine'] and not tile['revealed']:
61
+ return False
62
+ return True
63
+
64
+ # === Display Conversion ===
65
+ def cell_display(cell):
66
+ if cell['flagged']:
67
+ return "🚩"
68
+ if not cell['revealed']:
69
+ return ""
70
+ if cell['mine']:
71
+ return "💣"
72
+ return str(cell['adjacent']) if cell['adjacent'] > 0 else ""
73
+
74
+ def update_display():
75
+ return [[cell_display(cell) for cell in row] for row in game_state["board"]]
76
+
77
+ # === User Action Handler ===
78
+ def handle_click(x, y, action):
79
+ if game_state["game_over"]:
80
+ return update_display(), "Game over! Please reset."
81
+
82
+ if not (0 <= x < BOARD_WIDTH and 0 <= y < BOARD_HEIGHT):
83
+ return update_display(), "Invalid coordinates."
84
+
85
+ board = game_state["board"]
86
+
87
+ if action == "Reveal":
88
+ if board[y][x]['mine']:
89
+ board[y][x]['revealed'] = True
90
+ game_state["game_over"] = True
91
+ return update_display(), "💥 You hit a mine! Game over."
92
+ else:
93
+ reveal_tile(board, y, x)
94
+ elif action == "Flag":
95
+ toggle_flag(board, y, x)
96
+
97
+ if check_win(board):
98
+ game_state["game_over"] = True
99
+ game_state["win"] = True
100
+ return update_display(), "🎉 Congratulations! You won!"
101
+
102
+ return update_display(), "Continue playing..."
103
+
104
+ # === Game Reset ===
105
+ def reset_game():
106
+ game_state["board"] = initialize_board(BOARD_WIDTH, BOARD_HEIGHT, NUM_MINES)
107
+ game_state["game_over"] = False
108
+ game_state["win"] = False
109
+ return update_display(), "Game reset!"
110
+
111
+ # === Gradio UI ===
112
+ with gr.Blocks() as demo:
113
+ gr.Markdown("## 🧨 Minesweeper")
114
+ gr.Markdown("Reveal tiles and flag the mines. Good luck!")
115
+
116
+ status = gr.Textbox(label="Game Status", value="Game initialized!", interactive=False)
117
+ board_display = gr.Dataframe(value=update_display(), interactive=False, headers=None)
118
+
119
+ with gr.Row():
120
+ x_input = gr.Number(label="X (Column)", value=0, precision=0)
121
+ y_input = gr.Number(label="Y (Row)", value=0, precision=0)
122
+ action = gr.Radio(["Reveal", "Flag"], value="Reveal", label="Action")
123
+
124
+ with gr.Row():
125
+ move_btn = gr.Button("Make Move")
126
+ reset_btn = gr.Button("Reset Game")
127
+
128
+ move_btn.click(fn=handle_click, inputs=[x_input, y_input, action], outputs=[board_display, status])
129
+ reset_btn.click(fn=reset_game, outputs=[board_display, status])
130
+
131
+ # Initialize game on startup
132
+ reset_game()
133
+
134
+ demo.launch()