Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package grafica;
- import java.util.Random;
- public class Gioco2048 {
- private int numeroRighe = 10;
- private int numeroColonne = 10;
- private int[][] matriceGiocatore1 = new int[numeroRighe][numeroColonne];
- private int[][] matriceGiocatore2 = new int[numeroRighe][numeroColonne];;
- boolean turno;
- boolean giocoTerminato1;
- boolean giocoTerminato2;
- private String istruzione1;
- private String istruzione2;
- public static void main(String[] args) {
- Gioco2048 g = new Gioco2048();
- int[][] m1 = g.matriceGiocatore1;
- int[][] m2 = g.matriceGiocatore2;
- /*int[] v = {2,2,0,2,0,2,0,2,0,2,2,0,0};
- stampaRiga(g.spostamentoVersoDestra(v));
- stampaRiga(g.spostamentoVersoSinistra2(v));*/
- g.inizializzazioneMatrice(m1);
- stampaMatrice(m1);
- System.out.println("");
- g.slittamentoCelle(m1,"Destra");
- stampaMatrice(m1);
- System.out.println("");
- g.inizializzazioneMatrice(m2);
- stampaMatrice(m2);
- System.out.println("");
- g.slittamentoCelle(m2,"Sopra");
- stampaMatrice(m2);
- }
- public static void stampaRiga (int[] v) {
- System.out.print("[ ");
- for (int i = 0; i < v.length; i++ ) {
- if ( i == v.length - 1) {
- System.out.print(v[i]+" ");}
- else {
- System.out.print(v[i]+", ");} }
- System.out.println("]");}
- public static void stampaMatrice (int [][] m) {
- for(int i = 0; i < m.length; i++) {
- stampaRiga(m[i]);}}
- public void generaRandom2(int[][] m) {
- Random generatore = new Random();
- int indiceRiga;
- int indiceColonna;
- indiceRiga = generatore.nextInt(10);
- indiceColonna = generatore.nextInt(10);
- if(m[indiceRiga][indiceColonna] == 0) {
- m[indiceRiga][indiceColonna] = 2;
- }
- else {
- while(m[indiceRiga][indiceColonna] != 0) {
- indiceRiga = generatore.nextInt(10);
- indiceColonna = generatore.nextInt(10);
- }
- m[indiceRiga][indiceColonna] = 2;
- }
- }
- public void inizializzazioneMatrice(int[][] m) {
- for(int i = 0; i < 4; i++) {
- generaRandom2(m);
- }
- }
- private int[] spostamentoVersoDestra(int[] v) {
- int[] result = new int[v.length];
- int indiceResult = v.length - 1;
- int el_1 = v.length - 1;
- while(el_1 >= 0) {
- while(el_1 >=0 && v[el_1] == 0) {
- el_1 --;
- }
- if(el_1 == -1) {
- return v;
- }
- int el_2 = el_1 -1;
- while(el_2 >=0 && v[el_2] == 0) {
- el_2--;
- }
- if(el_2 == -1) {
- result[indiceResult] = v[el_1];
- indiceResult--;
- return result;
- }
- if(v[el_1] == v[el_2]) {
- result[indiceResult] = v[el_1]+v[el_2];
- indiceResult --;
- el_1 = el_2-1;
- }
- else {
- result[indiceResult] = v[el_1];
- indiceResult --;
- el_1 = el_2;
- }
- }
- return result;
- }
- private int[] spostamentoVersoSinistra(int[] v) {
- int[] result = new int[v.length];
- int indiceResult = 0;
- int el_1 = 0;
- while(el_1 < v.length) {
- while(el_1 < v.length && v[el_1] == 0) {
- el_1 ++;
- }
- if(el_1 == v.length) {
- return v;
- }
- int el_2 = el_1 + 1;
- while(el_2 < v.length && v[el_2] == 0) {
- el_2++;
- }
- if(el_2 == v.length) {
- result[indiceResult] = v[el_1];
- indiceResult++;
- return result;
- }
- if(v[el_1] == v[el_2]) {
- result[indiceResult] = v[el_1]+v[el_2];
- indiceResult ++;
- el_1 = el_2+1;
- }
- else {
- result[indiceResult] = v[el_1];
- indiceResult ++;
- el_1 = el_2;
- }
- }
- return result;
- }
- public void slittamentoCelle(int[][] m, String istruzione) {
- if(istruzione == "Destra") {
- for(int i = 0; i < m.length; i++) {
- m[i] = spostamentoVersoDestra(m[i]);
- }
- }
- if(istruzione == "Sinistra") {
- for(int i = 0; i < m.length; i++) {
- m[i] = spostamentoVersoSinistra(m[i]);
- }
- }
- if(istruzione == "Sopra") {
- int indiceColonna = 0;
- while(indiceColonna < m.length) {
- int[] colonna = new int[m.length];
- for(int i = 0; i < m.length; i++) {
- colonna[i] = m[i][indiceColonna];
- }
- colonna = spostamentoVersoSinistra(colonna);
- for(int j = 0; j < colonna.length; j++) {
- m[j][indiceColonna] = colonna[j];
- }
- indiceColonna++;
- }
- }
- if(istruzione == "Sotto") {
- int indiceColonna = 0;
- while(indiceColonna < m.length) {
- int[] colonna = new int[m.length];
- for(int i = 0; i < m.length; i++) {
- colonna[i] = m[i][indiceColonna];
- }
- colonna = spostamentoVersoDestra(colonna);
- for(int j = 0; j < colonna.length; j++) {
- m[j][indiceColonna] = colonna[j];
- }
- indiceColonna++;
- }
- }
- }
- public boolean mossaConsentita(int[][] m, String istruzione) {
- int[][] copiaDiM = copiaMatrice(m);
- Gioco2048 g = new Gioco2048();
- g.slittamentoCelle(m,istruzione);
- if(equivalenzaMatrici(m,copiaDiM)) {
- return false;
- }
- return true;
- }
- private int[][] copiaMatrice(int[][] m) {
- int[][] result = new int[m.length][m[0].length];
- for(int i = 0; i< m.length; i++) {
- for(int j = 0; j < m[0].length; j++) {
- result[i][j] = m[i][j];
- }
- }
- return result;
- }
- private boolean equivalenzaMatrici(int[][] m, int[][] n) {
- boolean result = true;
- for(int i = 0; i< m.length; i++) {
- for(int j = 0; j < m[0].length; j++) {
- if(m[i][j] != n[i][j]) {
- result = false;
- break;
- }
- }
- if(!result) {
- break;
- }
- }
- return result;
- }
- public boolean controlloSconfitta(int[][] m) {
- boolean result = true;
- String[] istruzioniPossibili = {"Sopra","Sotto","Destra","Sinistra"};
- for(int i = 0; i < istruzioniPossibili.length; i++) {
- if(mossaConsentita(m, istruzioniPossibili[i])) {
- result = false;
- break;
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement