Kelvineger

Tabuleiro

Mar 2nd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. package puzzle8;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6.  
  7. /**
  8.  *
  9.  * @author Kelvin
  10.  */
  11. public class Tabuleiro implements Comparable<Tabuleiro> {
  12.  
  13.     public int[][] posicoes = new int[3][3];
  14.  
  15.     public Integer tabInicial() {
  16.         List<Integer> numeros = new ArrayList<Integer>();
  17.         int j = 0;
  18.         for (int q = 0; q < 9; q++) {
  19.             numeros.add(q);
  20.         }
  21.         Collections.shuffle(numeros);
  22.         for (int i = 0; i < 3; i++) {
  23.             for (int x = 0; x < 3; x++) {
  24.                 this.posicoes[i][x] = numeros.get(j);
  25.                 j++;
  26.             }
  27.         }
  28.         return null;
  29.     }
  30.  
  31.     public List<Tabuleiro> geraFilhos() {
  32.         List<Tabuleiro> novoTab = new ArrayList<Tabuleiro>();
  33.         int[][] novaPos = null;
  34.         int[] posZero = achaZero();
  35.         int x = posZero[0];
  36.         int y = posZero[1];
  37.  
  38.         if (x == 0 && y == 0) {
  39.             novaPos = new int[][]{{1, 0}, {0, 1}};
  40.         } else if (x == 1 && y == 0) {
  41.             novaPos = new int[][]{{0, 0}, {2, 0}, {1, 1}};
  42.         } else if (x == 2 && y == 0) {
  43.             novaPos = new int[][]{{1, 0}, {2, 1}};
  44.         } else if (x == 0 && y == 1) {
  45.             novaPos = new int[][]{{0, 0}, {1, 1}, {0, 2}};
  46.         } else if (x == 1 && y == 1) {
  47.             novaPos = new int[][]{{0, 1}, {1, 0}, {2, 1}, {1, 2}};
  48.         } else if (x == 2 && y == 1) {
  49.             novaPos = new int[][]{{1, 1}, {2, 0}, {2, 2}};
  50.         } else if (x == 0 && y == 2) {
  51.             novaPos = new int[][]{{0, 1}, {1, 2}};
  52.         } else if (x == 1 && y == 2) {
  53.             novaPos = new int[][]{{0, 2}, {1, 1}, {2, 2}};
  54.         } else if (x == 2 && y == 2) {
  55.             novaPos = new int[][]{{1, 2}, {2, 1}};
  56.         }
  57.         for (int z = 0; z < novaPos.length; z++) {
  58.             Tabuleiro tabuNovo = this.copiaTabuleiro();
  59.             tabuNovo.trocaPos(posZero, novaPos[z]);
  60.             novoTab.add(tabuNovo);
  61.         }
  62.         return novoTab;
  63.     }
  64.  
  65.     public void imprimiTab(int[][] tab) {
  66.         for (int x = 0; x < 3; x++) {
  67.             for (int i = 0; i < 3; i++) {
  68.                 System.out.print(tab[x][i]);
  69.             }
  70.             System.out.println();
  71.         }
  72.     }
  73.  
  74.     public int verificaAcertos(int[][] tab) {
  75.         int cont = 0;
  76.         int acertos = 0;
  77.         int[] corretos = {1, 2, 3, 4, 5, 6, 7, 8, 0};
  78.  
  79.         for (int x = 0; x < 3; x++) {
  80.             for (int y = 0; y < 3; y++) {
  81.                 if (tab[x][y] == corretos[cont]) {
  82.                     acertos++;
  83.                 }
  84.                 cont++;
  85.             }
  86.         }
  87.         return acertos;
  88.     }
  89.  
  90.     private Tabuleiro copiaTabuleiro() {
  91.         Tabuleiro novoTab = new Tabuleiro();
  92.         novoTab.posicoes = this.posicoes.clone();
  93.         return novoTab;
  94.     }
  95.  
  96.     private void trocaPos(int[] posZero, int[] destino) {
  97.         int valorAtual = this.posicoes[posZero[0]][posZero[1]];
  98.         int valorDestino = this.posicoes[destino[0]][destino[1]];
  99.         this.posicoes[posZero[0]][posZero[1]] = valorDestino;
  100.         this.posicoes[destino[0]][destino[1]] = valorAtual;
  101.     }
  102.  
  103.     public int[] achaZero() {
  104.         for (int x = 0; x < 3; x++) {
  105.             for (int y = 0; y < 3; y++) {
  106.                 if (this.posicoes[x][y] == 0) {
  107.                     return new int[]{x + 1, y + 1};
  108.                 }
  109.             }
  110.         }
  111.         return null;
  112.     }
  113.    
  114.     public boolean igual(int tab[][]) {
  115.         for (int i = 0; i < 3; i++) {
  116.             for (int x = 0; x < 3; x++) {
  117.                 if (this.posicoes[i][x] != tab[i][x]) {
  118.                     return false;
  119.                 }
  120.             }
  121.         }
  122.         return true;
  123.     }
  124.  
  125.     @Override
  126.     public int compareTo(Tabuleiro tabu) {
  127.         if (verificaAcertos(this.posicoes) > verificaAcertos(tabu.posicoes)) {
  128.             return -1;
  129.         } else {
  130.             return 1;
  131.         }
  132.     }
  133. }
Add Comment
Please, Sign In to add comment