Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package puzzle8;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- /**
- *
- * @author Kelvin
- */
- public class Tabuleiro implements Comparable<Tabuleiro> {
- public int[][] posicoes = new int[3][3];
- public Integer tabInicial() {
- List<Integer> numeros = new ArrayList<Integer>();
- int j = 0;
- for (int q = 0; q < 9; q++) {
- numeros.add(q);
- }
- Collections.shuffle(numeros);
- for (int i = 0; i < 3; i++) {
- for (int x = 0; x < 3; x++) {
- this.posicoes[i][x] = numeros.get(j);
- j++;
- }
- }
- return null;
- }
- public List<Tabuleiro> geraFilhos() {
- List<Tabuleiro> novoTab = new ArrayList<Tabuleiro>();
- int[][] novaPos = null;
- int[] posZero = achaZero();
- int x = posZero[0];
- int y = posZero[1];
- if (x == 0 && y == 0) {
- novaPos = new int[][]{{1, 0}, {0, 1}};
- } else if (x == 1 && y == 0) {
- novaPos = new int[][]{{0, 0}, {2, 0}, {1, 1}};
- } else if (x == 2 && y == 0) {
- novaPos = new int[][]{{1, 0}, {2, 1}};
- } else if (x == 0 && y == 1) {
- novaPos = new int[][]{{0, 0}, {1, 1}, {0, 2}};
- } else if (x == 1 && y == 1) {
- novaPos = new int[][]{{0, 1}, {1, 0}, {2, 1}, {1, 2}};
- } else if (x == 2 && y == 1) {
- novaPos = new int[][]{{1, 1}, {2, 0}, {2, 2}};
- } else if (x == 0 && y == 2) {
- novaPos = new int[][]{{0, 1}, {1, 2}};
- } else if (x == 1 && y == 2) {
- novaPos = new int[][]{{0, 2}, {1, 1}, {2, 2}};
- } else if (x == 2 && y == 2) {
- novaPos = new int[][]{{1, 2}, {2, 1}};
- }
- for (int z = 0; z < novaPos.length; z++) {
- Tabuleiro tabuNovo = this.copiaTabuleiro();
- tabuNovo.trocaPos(posZero, novaPos[z]);
- novoTab.add(tabuNovo);
- }
- return novoTab;
- }
- public void imprimiTab(int[][] tab) {
- for (int x = 0; x < 3; x++) {
- for (int i = 0; i < 3; i++) {
- System.out.print(tab[x][i]);
- }
- System.out.println();
- }
- }
- public int verificaAcertos(int[][] tab) {
- int cont = 0;
- int acertos = 0;
- int[] corretos = {1, 2, 3, 4, 5, 6, 7, 8, 0};
- for (int x = 0; x < 3; x++) {
- for (int y = 0; y < 3; y++) {
- if (tab[x][y] == corretos[cont]) {
- acertos++;
- }
- cont++;
- }
- }
- return acertos;
- }
- private Tabuleiro copiaTabuleiro() {
- Tabuleiro novoTab = new Tabuleiro();
- novoTab.posicoes = this.posicoes.clone();
- return novoTab;
- }
- private void trocaPos(int[] posZero, int[] destino) {
- int valorAtual = this.posicoes[posZero[0]][posZero[1]];
- int valorDestino = this.posicoes[destino[0]][destino[1]];
- this.posicoes[posZero[0]][posZero[1]] = valorDestino;
- this.posicoes[destino[0]][destino[1]] = valorAtual;
- }
- public int[] achaZero() {
- for (int x = 0; x < 3; x++) {
- for (int y = 0; y < 3; y++) {
- if (this.posicoes[x][y] == 0) {
- return new int[]{x + 1, y + 1};
- }
- }
- }
- return null;
- }
- public boolean igual(int tab[][]) {
- for (int i = 0; i < 3; i++) {
- for (int x = 0; x < 3; x++) {
- if (this.posicoes[i][x] != tab[i][x]) {
- return false;
- }
- }
- }
- return true;
- }
- @Override
- public int compareTo(Tabuleiro tabu) {
- if (verificaAcertos(this.posicoes) > verificaAcertos(tabu.posicoes)) {
- return -1;
- } else {
- return 1;
- }
- }
- }
Add Comment
Please, Sign In to add comment