Skip to content

Instantly share code, notes, and snippets.

@LucasAlfare
Created August 24, 2016 00:15
Show Gist options
  • Save LucasAlfare/d082af52e6352e9c1acef7539ffb1b28 to your computer and use it in GitHub Desktop.
Save LucasAlfare/d082af52e6352e9c1acef7539ffb1b28 to your computer and use it in GitHub Desktop.
Em portugues..
package main;
/**
* Created by lucas on 22/08/16.
*/
public class Main {
public static void main(String[] args) {
new Square().printSquare();
}
static long c = 0;
public static void permutation(String str) {
permutation("", str);
}
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) {
System.out.println(prefix);
}
else {
for (int i = 0; i < n; i++) {
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
}
}
}
}
package main;
/**
* Created by lucas on 22/08/16.
*/
public enum Peca {
A(Type.CANTO, "A"), B(Type.MEIO, "B"), C(Type.CANTO, "C"), D(Type.MEIO, "D"),
E(Type.CANTO, "E"), F(Type.MEIO, "F"), G(Type.CANTO, "G"), H(Type.MEIO, "H"),
I(Type.CANTO, "I"), J(Type.MEIO, "J"), K(Type.CANTO, "K"), L(Type.MEIO, "L"),
M(Type.CANTO, "M"), N(Type.MEIO, "N"), O(Type.CANTO, "O"), P(Type.MEIO, "P");
private Type tipo;
private String nome;
Peca(Type tipo, String name){
this.tipo = tipo;
this.nome = name;
}
public Type getTipo() {
return tipo;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
package main;
/**
* Created by lucas on 22/08/16.
*/
public class Posicao {
private Peca peca;
public Posicao(Peca peca){
this.peca = peca;
}
public Peca getPeca() {
return peca;
}
}
package main;
/**
* Created by lucas on 23/08/16.
*/
public class Square {
private Peca[] pecas;
private Posicao[] posicoes;
public Square() {
pecas = new Peca[16];
posicoes = new Posicao[24];
pecas[0] = Peca.A;/*--*/pecas[8] = Peca.I;
pecas[1] = Peca.B;/*--*/pecas[9] = Peca.J;
pecas[2] = Peca.C;/*--*/pecas[10] = Peca.K;
pecas[3] = Peca.D;/*--*/pecas[11] = Peca.L;
pecas[4] = Peca.E;/*--*/pecas[12] = Peca.M;
pecas[5] = Peca.F;/*--*/pecas[13] = Peca.N;
pecas[6] = Peca.G;/*--*/pecas[14] = Peca.O;
pecas[7] = Peca.H;/*--*/pecas[15] = Peca.P;
posicoes[0] = new Posicao(pecas[0]);/*--*/posicoes[12] = new Posicao(pecas[8]);
posicoes[1] = new Posicao(pecas[0]);/*--*/posicoes[13] = new Posicao(pecas[8]);
posicoes[2] = new Posicao(pecas[1]);/*--*/posicoes[14] = new Posicao(pecas[9]);
posicoes[3] = new Posicao(pecas[2]);/*--*/posicoes[15] = new Posicao(pecas[10]);
posicoes[4] = new Posicao(pecas[2]);/*--*/posicoes[16] = new Posicao(pecas[10]);
posicoes[5] = new Posicao(pecas[3]);/*--*/posicoes[17] = new Posicao(pecas[11]);
posicoes[6] = new Posicao(pecas[4]);/*--*/posicoes[18] = new Posicao(pecas[12]);
posicoes[7] = new Posicao(pecas[4]);/*--*/posicoes[19] = new Posicao(pecas[12]);
posicoes[8] = new Posicao(pecas[5]);/*--*/posicoes[20] = new Posicao(pecas[13]);
posicoes[9] = new Posicao(pecas[6]);/*--*/posicoes[21] = new Posicao(pecas[14]);
posicoes[10] = new Posicao(pecas[6]);/*--*/posicoes[22] = new Posicao(pecas[14]);
posicoes[11] = new Posicao(pecas[7]);/*--*/posicoes[23] = new Posicao(pecas[15]);
}
public void u(int movimento) {
for (int i = 0; i < 12 - movimento; i++) {
moverTopo();
}
}
public void d(int movimento) {
for (int i = 0; i < 12 - movimento; i++) {
moverBase();
}
}
public void s() {
Posicao[] x = posicoes;
//A "slash" permutes indexes 3-8 between
//14-19. The "indexOfStartPerms" represents
//the first index to start that permutations,
//then it will be permuted with his same
//(index + 11). This cycle of permutations needs
//to repeat for 6 times.
int indexDoInicioDasPerms = 3;
for (int i = indexDoInicioDasPerms; i < 6 + indexDoInicioDasPerms; i++) {
Posicao aux = x[i];
x[i] = x[i + 11];
x[i + 11] = aux;
}
posicoes = x;
}
public boolean isTravado() {
//Square is blocked to do a slash if any of this posicoes
// (indexes) [2,3], [8,9], [13,14] and [19,20] contains, at
// same time, the same Peca object. If not equals in all
// cases, this method will return false (not blocked).
return
posicoes[2].getPeca().equals(posicoes[3].getPeca()) ||
posicoes[8].getPeca().equals(posicoes[9].getPeca()) ||
posicoes[13].getPeca().equals(posicoes[14].getPeca()) ||
posicoes[19].getPeca().equals(posicoes[20].getPeca());
}
public boolean isQuadrado(){
//This method is able to say if the cube have his
//booth layers isQuadrado. To make this search, the
//loop look at the current piece and the next piece
//of Array. It will return false ("false" for not squared
//shape) if that pecas have the same type and be
//different.
for (int i = 0; i < posicoes.length; i++){
Peca p1 = posicoes[i].getPeca();
Peca p2 = posicoes[i+1].getPeca();
if (p1.getTipo().equals(p2.getTipo()) && !p1.equals(p2)){
return false;
}
if (i == (posicoes.length - 2)){
break;
}
}
return true;
}
private void moverTopo() {
Posicao[] x = posicoes;
//(x.length / 2) - 1 -> 11 (indexes 0-11);
for (int i = 0; i < (x.length / 2) - 1; i++) {
Posicao aux = x[i];
x[i] = x[i + 1];
x[i + 1] = aux;
}
posicoes = x;
}
private void moverBase() {
Posicao[] x = posicoes;
//i = 12 -> bottom pecas starts at index 12 and ends at 23 (x.length - 1);
for (int i = 12; i < (x.length - 1); i++) {
Posicao aux = x[i];
x[i] = x[i + 1];
x[i + 1] = aux;
}
posicoes = x;
}
public void printSquare(){
System.out.println(this);
}
@Override
public String toString() {
String r = "Topo:\n", r2 = "Topo:\n";
for (int i = 0; i < posicoes.length; i++) {
r += "A posicao " + i + " contem a peca: " + posicoes[i].getPeca().getNome() +
" (" + posicoes[i].getPeca().getTipo().getTypeName() + ")\n";
//r2 += posicoes[i].getPeca().getTipo().getTypeName() + ", ";
r2 += posicoes[i].getPeca().getTipo().getNameAsByte() + ", ";
if (i == (posicoes.length / 2) - 1) {
r += "\nBase:\n";
r2 += "\nBase:\n";
}
}
return r + "\n" + r2;
}
}
package main;
/**
* Created by lucas on 22/08/16.
*/
public class Square2 {
public int[] pecas = new int[]{0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1};
public void u(int movimento){
for (int i = 0; i < 12 - movimento; i++){
moveTop();
}
}
public void d(int move){
for (int i = 0; i < 12 - move; i++){
moverBase();
}
}
public void s(){
int[] x = pecas;
int startOfPerms = 3;
for (int i = startOfPerms; i < 6+startOfPerms; i++){
int aux = x[i];
x[i] = x[i+11];
x[i+11] = aux;
}
pecas = x;
}
public boolean isBlocked(){
//TODO functional and worker isTravado method here...
return false;
}
private void moveTop() {
int[] x = pecas;
for (int i = 0; i < (x.length / 2) - 1; i++) {
int aux = x[i];
x[i] = x[i + 1];
x[i + 1] = aux;
}
pecas = x;
}
private void moverBase(){
int[] x = pecas;
for (int i = 12; i < x.length; i++) {
int aux = x[i];
x[i] = x[i + 1];
x[i + 1] = aux;
}
pecas = x;
}
@Override
public String toString() {
String r = "";
for (int i = 0; i < pecas.length; i++){
r += pecas[i] + " ";
if (i == (pecas.length / 2) - 1){
r += "\n";
}
}
return r;
}
}
package main;
import java.util.ArrayList;
import java.util.Random;
/**
* Created by lucas on 23/08/16.
*/
public class SquareScrambler {
private Square square;
private Random r = new Random();
private ArrayList<int[]> pairs = new ArrayList<>();
private int[] moves = new int[12];
public SquareScrambler(Square square){
this.square = square;
for (int i = -5; i <= 6; i++){
moves[i + 5] = i;
}
}
public void teste1(){
int u, d;
do {
u = moves[r.nextInt(moves.length)];
d = moves[r.nextInt(moves.length)];
} while (u == 0 && d == 0);
square.u(u);
square.d(d);
if (square.isTravado()){
square.u(u * -1);
square.d(d * -1);
teste1();
} else {
square.s();
if (!square.isQuadrado()){
} else {
pairs.add(new int[]{u,d});
}
}
}
public void teste2(){
}
public String getSequence(){
for (int i = 0; i < 10; i++){
teste1();
}
String r = "";
for (int[] x : pairs){
r += "(" + x[0] + "," + x[1] + ")/";
}
return r;
}
}
package main;
/**
* Created by lucas on 23/08/16.
*/
public class SquareShapeSolver {
//TODO something...
}
package main;
/**
* Created by lucas on 22/08/16.
*/
public enum Type {
CANTO("canto", (byte)1), MEIO("meio", (byte)0);
private String typeName;
private byte nameAsByte;
Type(String typeName, byte nameAsByte){
this.typeName = typeName;
this.nameAsByte = nameAsByte;
}
public String getTypeName() {
return typeName;
}
public byte getNameAsByte() {
return nameAsByte;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment