Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,13 +14,10 @@ class Minesweeper:
|
|
14 |
self.initialize_board()
|
15 |
|
16 |
def initialize_board(self):
|
17 |
-
# Tabuleiro com as minas (-1 para minas, 0-8 para números)
|
18 |
self.board = np.zeros((self.height, self.width), dtype=int)
|
19 |
-
# Tabuleiro visível para o jogador (-2 coberto, -1 bandeira, números para revelados)
|
20 |
self.visible_board = np.full((self.height, self.width), -2, dtype=int)
|
21 |
|
22 |
def place_mines(self, first_x, first_y):
|
23 |
-
# Coloca minas aleatoriamente, evitando a primeira posição clicada
|
24 |
positions = [(x, y) for x in range(self.height) for y in range(self.width)]
|
25 |
positions.remove((first_x, first_y))
|
26 |
mine_positions = random.sample(positions, self.num_mines)
|
@@ -28,7 +25,6 @@ class Minesweeper:
|
|
28 |
for x, y in mine_positions:
|
29 |
self.board[x, y] = -1
|
30 |
|
31 |
-
# Calcula números para células adjacentes às minas
|
32 |
for x in range(self.height):
|
33 |
for y in range(self.width):
|
34 |
if self.board[x, y] != -1:
|
@@ -55,15 +51,12 @@ class Minesweeper:
|
|
55 |
self.place_mines(x, y)
|
56 |
self.first_move = False
|
57 |
|
58 |
-
# Se clicou em uma mina
|
59 |
if self.board[x, y] == -1:
|
60 |
self.game_over = True
|
61 |
return
|
62 |
|
63 |
-
# Revela a célula clicada
|
64 |
self.flood_fill(x, y)
|
65 |
|
66 |
-
# Verifica vitória
|
67 |
if np.count_nonzero(self.visible_board == -2) == self.num_mines:
|
68 |
self.won = True
|
69 |
|
@@ -91,7 +84,6 @@ class Minesweeper:
|
|
91 |
def create_board_image(game):
|
92 |
fig, ax = plt.subplots(figsize=(8, 8))
|
93 |
|
94 |
-
# Cores para diferentes números
|
95 |
colors = {
|
96 |
-2: '#CCCCCC', # Coberto
|
97 |
-1: '#FF0000', # Bandeira/Mina
|
@@ -106,13 +98,11 @@ def create_board_image(game):
|
|
106 |
8: '#808080' # Cinza
|
107 |
}
|
108 |
|
109 |
-
# Desenha células
|
110 |
for i in range(game.height):
|
111 |
for j in range(game.width):
|
112 |
color = colors[game.visible_board[i, j]]
|
113 |
ax.add_patch(plt.Rectangle((j, game.height-1-i), 1, 1, facecolor=color, edgecolor='black'))
|
114 |
|
115 |
-
# Adiciona números
|
116 |
if game.visible_board[i, j] > 0:
|
117 |
plt.text(j+0.5, game.height-1-i+0.5, str(game.visible_board[i, j]),
|
118 |
horizontalalignment='center',
|
@@ -124,7 +114,6 @@ def create_board_image(game):
|
|
124 |
verticalalignment='center',
|
125 |
color='black')
|
126 |
|
127 |
-
# Mostra todas as minas se o jogo acabou
|
128 |
if game.game_over:
|
129 |
for i in range(game.height):
|
130 |
for j in range(game.width):
|
@@ -150,48 +139,68 @@ def create_board_image(game):
|
|
150 |
plt.title(f'Campo Minado - {status}')
|
151 |
return fig
|
152 |
|
153 |
-
def
|
154 |
-
if not
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
game.__dict__ = game_state
|
160 |
|
161 |
-
if
|
162 |
game.reveal(x, y)
|
163 |
else:
|
164 |
game.toggle_flag(x, y)
|
165 |
|
166 |
fig = create_board_image(game)
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
def new_game():
|
170 |
game = Minesweeper(8, 8, 10)
|
171 |
fig = create_board_image(game)
|
172 |
-
return fig, game.__dict__
|
173 |
|
174 |
with gr.Blocks(title="Campo Minado") as demo:
|
175 |
gr.Markdown("""
|
176 |
# Campo Minado
|
177 |
|
178 |
Instruções:
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
- Descubra todas as células sem minas para vencer
|
183 |
""")
|
184 |
|
185 |
game_state = gr.State()
|
186 |
board = gr.Plot(label="Tabuleiro")
|
|
|
187 |
|
188 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
new_game_btn = gr.Button("Novo Jogo")
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
-
|
|
|
196 |
|
197 |
demo.launch()
|
|
|
14 |
self.initialize_board()
|
15 |
|
16 |
def initialize_board(self):
|
|
|
17 |
self.board = np.zeros((self.height, self.width), dtype=int)
|
|
|
18 |
self.visible_board = np.full((self.height, self.width), -2, dtype=int)
|
19 |
|
20 |
def place_mines(self, first_x, first_y):
|
|
|
21 |
positions = [(x, y) for x in range(self.height) for y in range(self.width)]
|
22 |
positions.remove((first_x, first_y))
|
23 |
mine_positions = random.sample(positions, self.num_mines)
|
|
|
25 |
for x, y in mine_positions:
|
26 |
self.board[x, y] = -1
|
27 |
|
|
|
28 |
for x in range(self.height):
|
29 |
for y in range(self.width):
|
30 |
if self.board[x, y] != -1:
|
|
|
51 |
self.place_mines(x, y)
|
52 |
self.first_move = False
|
53 |
|
|
|
54 |
if self.board[x, y] == -1:
|
55 |
self.game_over = True
|
56 |
return
|
57 |
|
|
|
58 |
self.flood_fill(x, y)
|
59 |
|
|
|
60 |
if np.count_nonzero(self.visible_board == -2) == self.num_mines:
|
61 |
self.won = True
|
62 |
|
|
|
84 |
def create_board_image(game):
|
85 |
fig, ax = plt.subplots(figsize=(8, 8))
|
86 |
|
|
|
87 |
colors = {
|
88 |
-2: '#CCCCCC', # Coberto
|
89 |
-1: '#FF0000', # Bandeira/Mina
|
|
|
98 |
8: '#808080' # Cinza
|
99 |
}
|
100 |
|
|
|
101 |
for i in range(game.height):
|
102 |
for j in range(game.width):
|
103 |
color = colors[game.visible_board[i, j]]
|
104 |
ax.add_patch(plt.Rectangle((j, game.height-1-i), 1, 1, facecolor=color, edgecolor='black'))
|
105 |
|
|
|
106 |
if game.visible_board[i, j] > 0:
|
107 |
plt.text(j+0.5, game.height-1-i+0.5, str(game.visible_board[i, j]),
|
108 |
horizontalalignment='center',
|
|
|
114 |
verticalalignment='center',
|
115 |
color='black')
|
116 |
|
|
|
117 |
if game.game_over:
|
118 |
for i in range(game.height):
|
119 |
for j in range(game.width):
|
|
|
139 |
plt.title(f'Campo Minado - {status}')
|
140 |
return fig
|
141 |
|
142 |
+
def make_move(x, y, action, game_state):
|
143 |
+
if not game_state:
|
144 |
+
game = Minesweeper(8, 8, 10)
|
145 |
+
else:
|
146 |
+
game = Minesweeper(8, 8, 10)
|
147 |
+
game.__dict__ = game_state
|
|
|
148 |
|
149 |
+
if action == "reveal":
|
150 |
game.reveal(x, y)
|
151 |
else:
|
152 |
game.toggle_flag(x, y)
|
153 |
|
154 |
fig = create_board_image(game)
|
155 |
+
status = "Jogando"
|
156 |
+
if game.game_over:
|
157 |
+
status = "Game Over!"
|
158 |
+
elif game.won:
|
159 |
+
status = "Você Venceu!"
|
160 |
+
|
161 |
+
return fig, game.__dict__, status
|
162 |
|
163 |
def new_game():
|
164 |
game = Minesweeper(8, 8, 10)
|
165 |
fig = create_board_image(game)
|
166 |
+
return fig, game.__dict__, "Novo jogo iniciado!"
|
167 |
|
168 |
with gr.Blocks(title="Campo Minado") as demo:
|
169 |
gr.Markdown("""
|
170 |
# Campo Minado
|
171 |
|
172 |
Instruções:
|
173 |
+
1. Digite as coordenadas (0-7) para linha e coluna
|
174 |
+
2. Escolha "Revelar" para abrir uma célula ou "Bandeira" para marcar uma mina
|
175 |
+
3. Evite as minas e revele todas as células seguras!
|
|
|
176 |
""")
|
177 |
|
178 |
game_state = gr.State()
|
179 |
board = gr.Plot(label="Tabuleiro")
|
180 |
+
status = gr.Textbox(label="Status do Jogo", interactive=False)
|
181 |
|
182 |
with gr.Row():
|
183 |
+
x_coord = gr.Number(label="Linha (0-7)", minimum=0, maximum=7, step=1)
|
184 |
+
y_coord = gr.Number(label="Coluna (0-7)", minimum=0, maximum=7, step=1)
|
185 |
+
|
186 |
+
with gr.Row():
|
187 |
+
reveal_btn = gr.Button("Revelar")
|
188 |
+
flag_btn = gr.Button("Bandeira")
|
189 |
new_game_btn = gr.Button("Novo Jogo")
|
190 |
|
191 |
+
reveal_btn.click(
|
192 |
+
make_move,
|
193 |
+
inputs=[x_coord, y_coord, gr.Textbox(value="reveal", visible=False), game_state],
|
194 |
+
outputs=[board, game_state, status]
|
195 |
+
)
|
196 |
+
|
197 |
+
flag_btn.click(
|
198 |
+
make_move,
|
199 |
+
inputs=[x_coord, y_coord, gr.Textbox(value="flag", visible=False), game_state],
|
200 |
+
outputs=[board, game_state, status]
|
201 |
+
)
|
202 |
|
203 |
+
new_game_btn.click(new_game, outputs=[board, game_state, status])
|
204 |
+
demo.load(new_game, outputs=[board, game_state, status])
|
205 |
|
206 |
demo.launch()
|