Skip to content

Instantly share code, notes, and snippets.

@LucasAlfare
Created February 14, 2017 23:07
Show Gist options
  • Save LucasAlfare/ac3659edd8921f271a214aa764f1b34e to your computer and use it in GitHub Desktop.
Save LucasAlfare/ac3659edd8921f271a214aa764f1b34e to your computer and use it in GitHub Desktop.
Progresso de classes de square para serem usadas na construção de um CSP trainer.
package com.main;
/**
* Created by Lucas Sousa on 14/02/17.
*/
public class Canto extends Peca {
public Canto(String nome) {
super(nome);
}
@Override
public String toString() {
return this.getNome();
}
}
package com.main;
import java.util.ArrayList;
import java.util.Random;
/**
* Created by Lucas Sousa on 13/02/17.
*/
public class Embaralhador {
private Square cubo = new Square();
int[] movimentos = new int[12];
Random r = new Random();
private ArrayList<int[]> scramble = new ArrayList<>();
public Embaralhador(Square cubo){
this.cubo = cubo;
for (int i = -5; i < 7; i++) {
movimentos[i + 5] = i;
}
}
@Deprecated
public void getRandXYquadrado(){
int x;
int y;
do {
x = movimentos[r.nextInt(movimentos.length)];
y = movimentos[r.nextInt(movimentos.length)];
} while ((x == 0 && y == 0));
cubo.ud(x, y);
if (cubo.isBloqueado()){
cubo.ud(x * (-1), y * (-1));
getRandXYquadrado();
} else {
if (!cubo.isQuadrado()){
cubo.ud(x * (-1), y * (-1));
getRandXYquadrado();
} else {
scramble.add(new int[]{x, y});
cubo.barra();
}
}
}
public void getRandXY(){
int x;
int y;
do {
x = movimentos[r.nextInt(movimentos.length)];
y = movimentos[r.nextInt(movimentos.length)];
} while ((x == 0 && y == 0));
cubo.ud(x, y);
if (cubo.isBloqueado()){
cubo.ud(x * (-1), y * (-1));
getRandXY();
} else {
scramble.add(new int[]{x, y});
cubo.barra();
}
}
public String scramble(){
scramble.clear();
for (int i = 0; i < 20; i++){
getRandXY();
}
String r = "";
for (int[] x : scramble){
r += x[0] + "," + x[1] + "/";
}
return r;
}
private ArrayList<int[]> getScramble() {
return scramble;
}
private boolean contemOPar(ArrayList<int[]> sorteados, int x, int y){
for (int[] i : sorteados){
if (i[0] == x && i[1] == y){
return true;
}
}
return false;
}
}
package com.main;
/**
* Created by Lucas Sousa on 13/02/17.
*/
public class Main {
private Embaralhador embaralhador = new Embaralhador(new Square());
public Main(){
long x = System.currentTimeMillis();
for (int i = 0; i < 100; i++){
embaralhador.scramble(); // <- pra olhar um scramble gerado, basta printar isso ai
}
long r = System.currentTimeMillis() - x;
System.out.println("Demorou aproximadamente " + (r / 1000) + "s para gerar os scrambles");
}
public static void main(String[] args) {
new Main();
}
}
package com.main;
/**
* Created by Lucas Sousa on 14/02/17.
*/
public class Meio extends Peca {
public Meio(String nome) {
super(nome);
}
@Override
public String toString() {
return this.getNome();
}
}
package com.main;
/**
* Created by Lucas Sousa on 14/02/17.
*/
public class Peca {
private String nome;
public Peca(String nome) {
this.nome = nome;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Override
public String toString() {
return this.getNome();
}
}
package com.main;
import java.util.Arrays;
/**
* Created by Lucas Sousa on 13/02/17.
*/
public class Square {
private int[] u_args = {0,1,1,0,1,1,0,1,1,0,1,1};
private int[] d_args = {0,1,1,0,1,1,0,1,1,0,1,1};
private Peca[] u_pecas = new Peca[12];
private Peca[] d_pecas = new Peca[12];
public Square() {
for (int i = 0; i < 12; i++){
if (u_args[i] == 0){
u_pecas[i] = new Meio(i + "u");
} else if (u_args[i - 1] == 0 && u_args[i] == 1){
u_pecas[i] = new Canto(i + "u");
} else {
u_pecas[i] = u_pecas[i - 1];
}
if (d_args[i] == 0){
d_pecas[i] = new Meio(i + "d");
} else if (d_args[i - 1] == 0 && d_args[i] == 1){
d_pecas[i] = new Canto(i + "d");
} else {
d_pecas[i] = d_pecas[i - 1];
}
}
}
public void ud(int x, int y){
u(x); /**/ d(y);
}
public void u(int movimento){
for (int i = 0; i < 12 + movimento; i++){
up();
}
}
public void d(int movimento){
for (int i = 0; i < 12 + movimento; i++){
down();
}
}
private void up(){
Peca[] aux = new Peca[12];
aux[0] = u_pecas[u_pecas.length - 1];
System.arraycopy(u_pecas, 0, aux, 1, aux.length - 1);
u_pecas = aux;
}
private void down(){
Peca[] aux = new Peca[12];
aux[0] = d_pecas[d_pecas.length - 1];
System.arraycopy(d_pecas, 0, aux, 1, aux.length - 1);
d_pecas = aux;
}
public void barra(){
Peca[] u = Arrays.copyOfRange(u_pecas, 1, 7);
Peca[] d = Arrays.copyOfRange(d_pecas, 0, 6);
for (int i = 0; i < 6; i++){
u_pecas[i+1] = d[i];
d_pecas[i] = u[i];
}
}
public boolean isBloqueado(){
boolean p1 = u_pecas[0].equals(u_pecas[1]);
boolean p2 = u_pecas[6].equals(u_pecas[7]);
boolean p3 = d_pecas[11].equals(d_pecas[0]);
boolean p4 = d_pecas[5].equals(d_pecas[6]);
return p1 || p2 || p3 || p4;
}
public boolean isQuadrado(){
for (int i = 0; i < 12; i++){
if (!u_pecas[i].equals(u_pecas[i + 1]) && !d_pecas[i].equals(d_pecas[i + 1])){
if ((pecasMesmoTipo(u_pecas[i], u_pecas[i + 1])) || pecasMesmoTipo(d_pecas[i], d_pecas[i + 1])){
return false;
}
}
if (i == 12 - 2){
break;
}
}
return true;
}
private boolean pecasMesmoTipo(Peca x, Peca y){
boolean r = false;
if (x instanceof Meio && y instanceof Meio){
r = true;
}
if (x instanceof Canto && y instanceof Canto){
r = true;
}
return r;
}
@Override
public String toString() {
return "Topo: " + Arrays.toString(u_pecas) + "\n" +
"Base: " + Arrays.toString(d_pecas) + "\n\n" +
"Bloqueado? " + isBloqueado() + "\n\n" +
"Quadrado? " + isQuadrado();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment