Java - Buscaminas recorrer 0

 
Vista:

Buscaminas recorrer 0

Publicado por Alex (1 intervención) el 25/11/2019 09:55:05
Buenas necesito ayuda con el buscaminas.
Lo que quiero es que al pulsar sobre una casilla vacia se invoque un metodo recursivo que me lo recorra y destape a los demas 0.
Gracias aqui os dejo parte del codigo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
public void onClick(View v) {
 
    ImageButton b = (ImageButton) v;
    int posicion = v.getId();
    int poxx= 0;
    int poxy = 0;
    int numero = saberPosicion(posicion,TAM,poxx,poxy);
    if(minasActivas==TAM){
        hasGanado();
    }
    switch(numero){
 
       case -1:
           v.setBackgroundResource(R.drawable.minas);
 
           break;
       case 0:
           funcionRecursiva();
           v.setBackgroundResource(R.drawable.numero0);
           break;
       case 1:
           v.setBackgroundResource(R.drawable.numero1);
           break;
       case 2:
           v.setBackgroundResource(R.drawable.numero2);
           break;
       case 3:
           v.setBackgroundResource(R.drawable.numero3);
           break;
       case 5:
           v.setBackgroundResource(R.drawable.numero4);
           break;
       case 6:
           v.setBackgroundResource(R.drawable.numero3);
           break;
       case 7:
           v.setBackgroundResource(R.drawable.numero3);
           break;
       case 8:
           v.setBackgroundResource(R.drawable.numero3);
           break;
 
    }
 
}
 
 
 
 
@Override
public boolean onLongClick(View v) {
    ponerBanderas(v);
    return true;
}
private int saberPosicion(int posicion , int filas , int poxx, int poxy){
 
 
   poxx = posicion%filas;
   poxy = posicion/filas;
 
    return tablero[poxx][poxy];
 
 
}
 
public void hasGanado(){
    if(minasActivas == TAM){
        Toast toast1 =
                Toast.makeText(getApplicationContext(),
                        "Has ganado", Toast.LENGTH_SHORT);
 
        toast1.show();
 
 
    }
}
 
public void funcionRecursiva(){
   for(int i = TAM-1 ; i <= TAM+1 ; i++ ){
       if(!(i<0 || i >= TAM)){
 
 
 
   for(int j = TAM-1 ; j <= TAM+1 ; j++ ){
       if(!(j<0 || j>=TAM)){
 
            if(tablero[i][j]==0){
                //return 0;
            }
       }
   }
   }
    }
 
}
public void setTablero() {
    for (int f = 0; f < TAM; f++) {
        for (int c = 0; c < TAM; c++) {
            tablero[f][c] = 0;
            visible[f][c] = 0;
        }
    }
 
 
    //Pone diez minas
    for (int mina = 0; mina < 10; mina++) {
        //Busca una posición aleatoria donde no haya otra bomba
        int f, c;
        do {
            f = (int) (Math.random() * TAM);
            c = (int) (Math.random() * TAM);
        } while (tablero[f][c] == -1);
        //Pone la bomba
        tablero[f][c] = -1;
        //Recorre el contorno de la bomba e incrementa los contadores
        for (int f2 = max(0, f - 1); f2 < min(TAM, f + 2); f2++) {
            for (int c2 = max(0, c - 1); c2 < min(TAM, c + 2); c2++) {
                if (tablero[f2][c2] != -1) { //Si no es bomba
                    tablero[f2][c2]++; //Incrementa el contador
                }
            }
        }
    }
    for (int f = 0; f < TAM; f++) {
        for (int c = 0; c < TAM; c++) {
            System.out.println(tablero[f][c]);
            System.out.println("---------------------------------------------------------------------");
        }
    }
 
}
 
 
 
 
public void ponerBanderas(View v) {
    ImageButton b = (ImageButton) v;
    g = (GridLayout) findViewById(R.id.Grid);
    for (int f = 0; f < TAM; f++) {
        for (int c = 0; c < TAM; c++) {
            if (tablero[f][c] != -1) {
 
                v.setBackgroundResource(R.drawable.bandera);
            minasActivas++;
            } else {
 
                Toast toast1 =
                        Toast.makeText(getApplicationContext(),
                                "Has perdido", Toast.LENGTH_SHORT);
 
                toast1.show();
 
            g.setEnabled(false);
 
 
            }
 
        }
 
 
    }
 
 
}
public void resetButon(View v){
    Intent intent = getIntent();
    finish();
    startActivity(intent);
    overridePendingTransition(0, 0);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    Toast toast1 =
            Toast.makeText(getApplicationContext(),
                    "Nuevo juego", Toast.LENGTH_SHORT);
 
    toast1.show();
}
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder