-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTablero.java
More file actions
88 lines (76 loc) · 2.74 KB
/
Tablero.java
File metadata and controls
88 lines (76 loc) · 2.74 KB
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
package tercerparcial;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Tablero {
private Tarjeta[][] tarjetas;
private int paresConstruidos;
public Tablero() {
tarjetas = new Tarjeta[4][4];
paresConstruidos = 0;
inicializarTablero();
}
private void inicializarTablero() {
// Crear las 8 parejas de tarjetas
List<Tarjeta> listaTarjetas = new ArrayList<>();
String[] nombres = {"Gato", "Perro", "León", "Águila", "Delfín", "Tigre", "Jirafa", "Elefante"};
// Crear 2 tarjetas por cada nombre
for (String nombre : nombres) {
for (int i = 0; i < 2; i++) {
listaTarjetas.add(new Tarjeta(nombre, "imagen_" + nombre, 0));
}
}
// Mezclar solo los nombres de las tarjetas
Collections.shuffle(listaTarjetas);
// Asignar tarjetas al tablero con numeración secuencial
int contador = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
Tarjeta tarjeta = listaTarjetas.get(i * 4 + j);
tarjeta.setPosicion(contador);
tarjetas[i][j] = tarjeta;
contador++;
}
}
}
public boolean compararTarjetas(int posicion1, int posicion2) {
Tarjeta tarjeta1 = obtenerTarjeta(posicion1);
Tarjeta tarjeta2 = obtenerTarjeta(posicion2);
if (tarjeta1.getNombre().equals(tarjeta2.getNombre())) {
tarjeta1.setEncontrada(true);
tarjeta2.setEncontrada(true);
paresConstruidos++;
return true;
}
return false;
}
public Tarjeta obtenerTarjeta(int posicion) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (tarjetas[i][j].getPosicion() == posicion) {
return tarjetas[i][j];
}
}
}
return null;
}
public boolean juegoTerminado() {
return paresConstruidos == 8;
}
public void mostrarTablero() {
System.out.println("Tablero actual:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
Tarjeta tarjeta = tarjetas[i][j];
if (tarjeta.isEncontrada()) {
System.out.print("[" + tarjeta.getPosicion() + ":" + tarjeta.getNombre() + "]\t");
} else if (tarjeta.isVolteada()) {
System.out.print("[" + tarjeta.getPosicion() + ":" + tarjeta.getNombre() + "]\t");
} else {
System.out.print("[" + tarjeta.getPosicion() + ":X]\t");
}
}
System.out.println();
}
}
}