Created
September 5, 2014 05:37
-
-
Save NekoTashi/409a9c5c164c1f9150b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) | |
public class Tablero extends World | |
{ | |
private static int TAM = 3; | |
private int tablero_gato[][] = new int[TAM][TAM]; | |
private int player_ganador = -1; | |
public Tablero() { | |
super(3, 3, 60); | |
empezar_juego(); | |
} | |
/* | |
* Rellena el el array del tablero con -1 que | |
* significa que el tablero está vacio. | |
*/ | |
public void empezar_juego() { | |
for(int n = 0; n < TAM; n++) | |
for(int m = 0; m < TAM; m++) | |
tablero_gato[n][m] = -1; | |
player_ganador = -1; | |
} | |
public void act() { | |
// Mapea el tablero visual a un array de dígitos dígitos procesables fácilmente. | |
for(int n = 0; n < TAM; n++) { | |
for(int m = 0; m < TAM; m++) { | |
if(!getObjectsAt(n, m, Diamante.class).isEmpty()) { | |
tablero_gato[n][m] = 0; | |
} else if (!getObjectsAt(n, m, Trebol.class).isEmpty()) { | |
tablero_gato[n][m] = 1; | |
} | |
} | |
} | |
if(player_ganador == -1) { // Si no hay ganador, hacer... | |
player_ganador = gana_partida(); | |
poner_ficha_maquina(); | |
} | |
} | |
/* | |
* Este método verifica si player o la maquina ganó. | |
* Retorna la ficha que ganó, si nadie ha ganado aún retorna -1. | |
*/ | |
public int gana_partida() { | |
if(tablero_gato[0][0] != -1 && tablero_gato[0][0] == tablero_gato[1][1] | |
&& tablero_gato[0][0] == tablero_gato[2][2]) | |
return tablero_gato[0][0]; | |
if(tablero_gato[0][2] != -1 && tablero_gato[0][2] == tablero_gato[1][1] | |
&& tablero_gato[0][2] == tablero_gato[2][0]) | |
return tablero_gato[0][2]; | |
for (int n=0;n<TAM;n++){ | |
if(tablero_gato[n][0] != -1 && tablero_gato[n][0] == tablero_gato[n][1] | |
&& tablero_gato[n][0] == tablero_gato[n][2]) | |
return tablero_gato[n][0]; | |
if(tablero_gato[0][n] != -1 && tablero_gato[0][n] == tablero_gato[1][n] | |
&& tablero_gato[0][n] == tablero_gato[2][n]) | |
return tablero_gato[0][n]; | |
} | |
return -1; | |
} | |
/* | |
* Verifica si el tablero está completo. | |
* Retorna true si aún no está completo, | |
* retorna false cuando está completo. | |
*/ | |
private boolean tablero_completo() { | |
for (int n = 0; n < TAM; n++) | |
for (int m = 0; m < TAM; m++) | |
if (tablero_gato[n][m] == -1) | |
return false; | |
return true; | |
} | |
/* | |
* Verifica si la partida terminó. | |
* Retorna true si el tablero está completo o si | |
* algún player ganó; de lo contrario, retorna false. | |
*/ | |
private boolean fin_partida() { | |
return tablero_completo() || gana_partida() != -1; | |
} | |
/* | |
* Comienza con el algoritmo minimax. | |
* Retorna la ficha más óptima para ganar el juego. | |
*/ | |
private void poner_ficha_maquina() { | |
if(!fin_partida()) { | |
int f = 0, c = 0; | |
int v = Integer.MIN_VALUE; // Infinito negativo | |
int aux; | |
for(int n = 0; n < TAM; n++) { | |
for(int m = 0; m < TAM; m++) { | |
if(tablero_gato[n][m] == -1) { | |
tablero_gato[n][m] = 1; // Se coloca la ficha en el tablero para probar | |
aux = algoritmo_min(); | |
if(aux > v) { // Maximizamos | |
v = aux; // Cambiamos el valor de la variable v por el que nos retornó el algoritmo | |
f = n; // Posicion en X donde se pondra la ficha | |
c = m; // Posicion en Y donde se pondra la ficha | |
} | |
tablero_gato[n][m] = -1; // Luego, sacamos la ficha | |
} | |
} | |
} | |
// La maquina coloca la ficha que encuentra que es la óptima | |
System.out.println(v); | |
tablero_gato[f][c] = 1; | |
addObject(new Trebol(), f, c); | |
} | |
player_ganador = gana_partida(); | |
} | |
/* | |
* Maximiza | |
* | |
*/ | |
private int algoritmo_max(){ | |
if(fin_partida()){ | |
if(gana_partida() != -1) return -1; | |
else return 0; | |
} | |
int v = Integer.MIN_VALUE; // Infinito negativo | |
int aux; | |
for(int n = 0; n < TAM; n++) { | |
for(int m = 0; m < TAM; m++) { | |
if(tablero_gato[n][m] == -1) { | |
tablero_gato[n][m] = 1; // Se coloca la ficha en el tablero para hacer pruebas | |
aux = algoritmo_min(); | |
if(aux > v) v = aux; // Se cambia el valor V que es la variable que se irá retornando | |
tablero_gato[n][m] = -1; // Luego, sacamos la ficha | |
} | |
} | |
} | |
return v; | |
} | |
/* | |
* Minimiza | |
* | |
*/ | |
private int algoritmo_min() { | |
if(fin_partida()) { | |
if (gana_partida() != -1) return 1; | |
else return 0; | |
} | |
int v = Integer.MAX_VALUE; // Infinito positivo | |
int aux; | |
for(int n = 0; n < TAM; n++) { | |
for(int m = 0; m < TAM; m++) { | |
if (tablero_gato[n][m] == -1){ | |
tablero_gato[n][m] = 0; // Se coloca la ficha en el tablero para probar | |
aux = algoritmo_max(); // Maximizamos | |
if(aux < v) v = aux; | |
tablero_gato[n][m] = -1; // Luego, se saca la ficha | |
} | |
} | |
} | |
return v; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment