Gioco del 15! - Ground.java
Cerca
 











Ground.java

Caricato da: Paoloricciuti
Scarica il programma completo

  1. package game;
  2.  
  3. /**
  4.  *
  5.  * @author paolo
  6.  */
  7. public class Ground {
  8.  
  9.     private int[][] tavolo;
  10.  
  11.     public Ground() {
  12.         this.initTavolo();
  13.     }
  14.  
  15.     public void initTavolo() {
  16.         this.tavolo = new int[4][4];
  17.         int cont = 1;
  18.         for (int i = 0; i < 4; i++) {
  19.             for (int j = 0; j < 4; j++) {
  20.                 this.tavolo[i][j] = cont;
  21.                 cont++;
  22.             }
  23.         }
  24.     }
  25.  
  26.     public int[][] toMatrix(){
  27.         return this.tavolo;
  28.     }
  29.  
  30.     public void shuffle() {
  31.         boolean[][] control = new boolean[4][4];
  32.         int[][] mischia = new int[4][4];
  33.         for (int i = 0; i < 4; i++) {
  34.             for (int j = 0; j < 4; j++) {
  35.                 control[i][j] = false;
  36.                 mischia[i][j] = 0;
  37.             }
  38.         }
  39.         for (int i = 0; i < 4; i++) {
  40.             for (int j = 0; j < 4; j++) {
  41.                 double random = Math.random() * 4;
  42.                 double random1 = Math.random() * 4;
  43.                 int rand = (int) random;
  44.                 int rand1 = (int) random1;
  45.                 while (control[rand][rand1]) {
  46.                     random = Math.random() * 4;
  47.                     random1 = Math.random() * 4;
  48.                     rand = (int) random;
  49.                     rand1 = (int) random1;
  50.                 }
  51.                 mischia[i][j] = this.tavolo[rand][rand1];
  52.                 control[rand][rand1] = true;
  53.             }
  54.         }
  55.         for (int i = 0; i < 4; i++) {
  56.             for (int j = 0; j < 4; j++) {
  57.                 this.tavolo[i][j] = mischia[i][j];
  58.             }
  59.         }
  60.     }
  61.  
  62.     public boolean isWinner(){
  63.         boolean control=true;
  64.         int cont=0;
  65.         for(int i=0; i<4; i++){
  66.             for(int j=0; j<4; j++){
  67.                 control=control&&this.tavolo[i][j]==(cont+1);
  68.                 cont++;
  69.             }
  70.         }
  71.         return control;
  72.     }
  73.  
  74.     public void sposta(int pI, int pJ) {
  75.         if (this.check(pI, pJ)) {
  76.             int spostaI = 0;
  77.             int spostaJ = 0;
  78.             for (int id = 0; id < 4; id++) {
  79.                 for (int jd = 0; jd < 4; jd++) {
  80.                     if (this.tavolo[id][jd] == 16) {
  81.                         spostaI = id;
  82.                         spostaJ = jd;
  83.                     }
  84.                 }
  85.             }
  86.             int swap = this.tavolo[pI][pJ];
  87.             this.tavolo[pI][pJ] = this.tavolo[spostaI][spostaJ];
  88.             this.tavolo[spostaI][spostaJ] = swap;
  89.         }
  90.     }
  91.  
  92.     private boolean check(int pI, int pJ) {
  93.         int[] intorno = {0,0,0,0};
  94.         if (pI - 1 >= 0) {
  95.             intorno[0] = this.tavolo[pI - 1][pJ];
  96.         }
  97.         if (pJ - 1 >= 0) {
  98.             intorno[1] = this.tavolo[pI][pJ - 1];
  99.         }
  100.         if (pI + 1 <= 3) {
  101.             intorno[2] = this.tavolo[pI + 1][pJ];
  102.         }
  103.         if (pJ + 1 <= 3) {
  104.             intorno[3] = this.tavolo[pI][pJ + 1];
  105.         }
  106.         for(int i=0; i<4; i++){
  107.             if(intorno[i]==16){
  108.                 return true;
  109.             }
  110.         }
  111.         return false;
  112.     }
  113. }
 

Creative Commons License
Il layout di questo sito è concesso sotto licenza Creative Commons.
Per maggiori informazioni sulle licenze dei contenuti del sito, clicca.