Skip to content

Commit 46977a3

Browse files
committed
Projecto finalizado
1 parent 92b3fd9 commit 46977a3

File tree

4 files changed

+228
-29
lines changed

4 files changed

+228
-29
lines changed

src/GameOfLife.java

+113-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1+
import java.awt.event.KeyEvent;
2+
import java.awt.event.KeyListener;
13
import java.util.HashMap;
4+
import java.util.Scanner;
25

3-
// todo: Implementar un metodo que verifique que datos son necesarios para iniciar las generaciones
6+
// todo: Implementar un metodo que verifique que datos son necesarios para iniciar las generaciones (Hecho)
47
// todo: Implementar un metodo que verifique que los datos sean validos (Hecho)
58

69

7-
public class GameOfLife {
10+
public class GameOfLife implements KeyListener {
811
private final int width;
912
private final int height;
1013
private final int generations;
1114
private final int speed;
15+
private volatile boolean stop = false;
1216
private int n = 3;
1317
private final String population;
1418
private Tablero tablero;
1519

1620

21+
/**
22+
* Constructor de la clase
23+
* @param args Arreglo de Strings
24+
*/
25+
1726
public GameOfLife(String[] args) {
1827
HashMap<String, String> argumentos = VerificacionDatos.argumentosSplit(args);
1928

@@ -36,24 +45,100 @@ public GameOfLife(String[] args) {
3645
this.tablero = new Tablero(this.population, this.height, this.width);
3746
}
3847

48+
/**
49+
* Metodo que verifica si los datos son validos
50+
* @return {@code true} si los datos son validos, {@code false} si no son validos
51+
*/
52+
private boolean readyToGo() {
53+
return this.width != -1 && this.height != -1 && this.generations != -1 && this.speed != -1
54+
&& tablero.getMatrizGOL().length != 0;
55+
}
56+
57+
58+
/**
59+
* Metodo que inicia el juego
60+
*/
61+
public void start() {
62+
Scanner scanner = new Scanner(System.in);
63+
if (readyToGo()) {
64+
mostrarInfo();
65+
System.out.println("Presione enter para iniciar el juego");
66+
scanner.nextLine();
3967

40-
public Tablero getTablero() {
41-
return tablero;
68+
69+
70+
if (this.generations > 0) {
71+
generaciones();
72+
}else {
73+
generacionsInfinitas();
74+
}
75+
} else {
76+
System.out.println("No se puede iniciar el juego");
77+
}
4278
}
4379

44-
public int getGenerations() {
45-
return generations;
80+
/**
81+
* Metodo que genera generaciones finitas
82+
*/
83+
private void generaciones() {
84+
85+
//for (int i = 0; (i < this.generations) && !tablero.isExtincto(); i++) {
86+
// Si se quiere que el juego se detenga cuando la poblacion se extinga
87+
for (int i = 0; i < this.generations; i++) {
88+
clearScreen();
89+
tablero.vecindario(this.n);
90+
tablero.imprimirMatriz();
91+
System.out.print("Generacion: " + (i + 1));
92+
try {
93+
Thread.sleep(this.speed);
94+
} catch (InterruptedException e) {
95+
e.printStackTrace();
96+
}
97+
}
4698
}
4799

48-
public int getN() {
49-
return n;
100+
/**
101+
* Metodo que genera generaciones infinitas
102+
* Se ejecuta hasta que el usuario presione enter
103+
*/
104+
105+
private void generacionsInfinitas() {
106+
int i = 0;
107+
while (!stop && !tablero.isExtincto()) {
108+
clearScreen();
109+
tablero.vecindario(this.n);
110+
tablero.imprimirMatriz();
111+
System.out.print("Generacion: " + (i + 1));
112+
try {
113+
Thread.sleep(this.speed);
114+
} catch (InterruptedException e) {
115+
e.printStackTrace();
116+
}
117+
i++;
118+
}
50119
}
51120

52-
public boolean readyToGo() {
53-
return this.width != -1 && this.height != -1 && this.generations != -1 && this.speed != -1
54-
&& tablero.getMatrizGOL().length != 0;
121+
/**
122+
* Metodo que limpia la pantalla
123+
*/
124+
125+
private void clearScreen() {
126+
try {
127+
if (System.getProperty("os.name").contains("Windows")) {
128+
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
129+
} else {
130+
System.out.print("\033[H\033[2J");
131+
System.out.flush();
132+
}
133+
} catch (Exception e) {
134+
e.printStackTrace();
135+
}
55136
}
56137

138+
/**
139+
* Metodo que muestra la informacion de los datos ingresados
140+
*/
141+
57142
public void mostrarInfo() {
58143
System.out.println(this.width==0 ? "Width=[No presente]" :this.width==-1?"Width=[Invalido]" : "width: " + this.width);
59144
System.out.println(this.height==0 ? "Height=[No presente]" :this.height==-1?"Height=[Invalido]" : "height: " + this.height);
@@ -63,4 +148,21 @@ public void mostrarInfo() {
63148
System.out.println("n: " + this.n);
64149
tablero.imprimirMatriz();
65150
}
151+
152+
@Override
153+
public void keyTyped(KeyEvent e) {
154+
155+
}
156+
157+
@Override
158+
public void keyPressed(KeyEvent e) {
159+
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
160+
stop = true;
161+
}
162+
}
163+
164+
@Override
165+
public void keyReleased(KeyEvent e) {
166+
167+
}
66168
}

src/Main.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
21
public class Main {
32
public static void main(String[] args) {
4-
53
GameOfLife gol = new GameOfLife(args);
6-
gol.mostrarInfo();
7-
for (int i = 0; i < gol.getGenerations(); i++) {
8-
gol.getTablero().vecindario(gol.getN());
9-
gol.getTablero().imprimirMatriz();
10-
}
4+
5+
gol.start();
116
}
127
}

src/Tablero.java

+81-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
// todo: Implementar los 5 tipos de vecindarios que existen en el juego de la vida
1+
// todo: Implementar los 5 tipos de vecindarios que existen en el juego de la vida (Hecho)
22

33
public class Tablero {
44
private int[][] matrizGOL;
55

6+
/**
7+
* Constructor de la clase
8+
* @param population String con la matriz
9+
* @param height Altura de la matriz
10+
* @param width Ancho de la matriz
11+
*/
12+
613
public Tablero(String population, int height, int width) {
714
this.matrizGOL = convMatriz(population, height, width);
815
}
916

17+
/**
18+
* Convierte el formato de String de la matriz en un arreglo de enteros
19+
* @param stringMatriz String con la matriz
20+
* @param height Altura de la matriz
21+
* @param width Ancho de la matriz
22+
* @return Matriz de enteros
23+
*/
24+
1025
private int[][] convMatriz(String stringMatriz, int height, int width) {
1126
if ("rnd".equals(stringMatriz)) {
1227

@@ -51,9 +66,9 @@ private int[][] convMatriz(String stringMatriz, int height, int width) {
5166
}
5267
}
5368

54-
/*
55-
* Imprime la matriz en consola
56-
*/
69+
/**
70+
* Imprime la matriz
71+
*/
5772
public void imprimirMatriz() {
5873
System.out.println("Matriz: ");
5974
for (int[] ints : this.matrizGOL) {
@@ -64,8 +79,9 @@ public void imprimirMatriz() {
6479
}
6580
}
6681

67-
/*
68-
* Retorna la matriz con las nuevas generaciones aplicando las reglas del GOL
82+
/**
83+
* Calcula el vecindario de la proxima generacion de la matriz
84+
* @param n
6985
*/
7086

7187
public void vecindario(int n) {
@@ -98,10 +114,13 @@ public void vecindario(int n) {
98114
}
99115

100116

101-
/*
102-
* Devuelve una matriz de 3x3 con los vecinos de la celda i,j
103-
* Si la celda esta en el borde de la matriz, se rellena con 0 los espacios vacios
117+
/**
118+
* Copia el vecindario de la celda actual
119+
* @param i Fila de la celda
120+
* @param j Columna de la celda
121+
* @return Matriz de 3x3 con el vecindario de la celda
104122
*/
123+
105124
private int[][] vecinadrioCopiado(int i, int j) {
106125
int[][] vecinos = new int[3][3];
107126
for (int k = i - 1; k <= i + 1; k++) {
@@ -115,6 +134,12 @@ private int[][] vecinadrioCopiado(int i, int j) {
115134
}
116135

117136

137+
/**
138+
* Calcula el vecindario 1 de la celda actual
139+
* @param i Fila de la celda
140+
* @param j Columna de la celda
141+
* @return Numero de vecinos
142+
*/
118143

119144
private int vecindario1(int i, int j) {
120145
int vecinos = 0;
@@ -133,6 +158,13 @@ private int vecindario1(int i, int j) {
133158
return vecinos;
134159
}
135160

161+
/**
162+
* Calcula el vecindario 2 de la celda actual
163+
* @param i Fila de la celda
164+
* @param j Columna de la celda
165+
* @return Numero de vecinos
166+
*/
167+
136168
private int vecindario2(int i, int j) {
137169
int vecinos = 0;
138170
int[][] vecinosCopiados = vecinadrioCopiado(i, j);
@@ -150,6 +182,12 @@ private int vecindario2(int i, int j) {
150182
return vecinos;
151183
}
152184

185+
/**
186+
* Calcula el vecindario 3 de la celda actual
187+
* @param i Fila de la celda
188+
* @param j Columna de la celda
189+
* @return Numero de vecinos
190+
*/
153191

154192
private int vecindario3(int i, int j) {
155193
int vecinos = 0;
@@ -167,6 +205,13 @@ private int vecindario3(int i, int j) {
167205
return vecinos - this.matrizGOL[i][j];
168206
}
169207

208+
/**
209+
* Calcula el vecindario 4 de la celda actual
210+
* @param i Fila de la celda
211+
* @param j Columna de la celda
212+
* @return Numero de vecinos
213+
*/
214+
170215
private int vecindario4(int i, int j) {
171216
int vecinos = 0;
172217
int[][] vecinosCopiados = vecinadrioCopiado(i, j);
@@ -184,6 +229,13 @@ private int vecindario4(int i, int j) {
184229
return vecinos - this.matrizGOL[i][j];
185230
}
186231

232+
/**
233+
* Calcula el vecindario 5 de la celda actual
234+
* @param i Fila de la celda
235+
* @param j Columna de la celda
236+
* @return Numero de vecinos
237+
*/
238+
187239
private int vecindario5(int i, int j) {
188240
int vecinos = 0;
189241
int[][] vecinosCopiados = vecinadrioCopiado(i, j);
@@ -201,8 +253,28 @@ private int vecindario5(int i, int j) {
201253
return vecinos;
202254
}
203255

256+
/**
257+
* Devuelve la matriz del juego de la vida
258+
* @return Matriz del juego de la vida
259+
*/
204260

205261
public int[][] getMatrizGOL() {
206262
return matrizGOL;
207263
}
264+
265+
/**
266+
* Verifica si la matriz esta extinta
267+
* @return {@code true} si la matriz esta extinta, {@code false} si no esta extinta
268+
*/
269+
270+
public boolean isExtincto() {
271+
for (int[] ints : this.matrizGOL) {
272+
for (int anInt : ints) {
273+
if (anInt == 1) {
274+
return false;
275+
}
276+
}
277+
}
278+
return true;
279+
}
208280
}

0 commit comments

Comments
 (0)