Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package einsteinsriddle;
- import java.util.List;
- /**
- *
- * @author Joel
- */
- public class EinsteinsRiddle2 {
- /**
- * Imprime o resultado do enigma
- *
- * @param cor
- * @param nacionalidade
- * @param animal
- * @param bebida
- * @param cigarro
- */
- private static void printResult(List<Object> cor, List<Object> nacionalidade,
- List<Object> animal, List<Object> bebida, List<Object> cigarro, int cont) {
- System.out.format("%8s%14s%14s%14s%14s%n", "_", "_", "_", "_", "_");
- System.out.format("%9s%14s%14s%14s%14s%n", "/ \\", "/ \\", "/ \\", "/ \\", "/ \\");
- System.out.format("%6s%4s%10s%4s%10s%4s%10s%4s%10s%4s%n", "/", "\\", "/", "\\", "/", "\\", "/", "\\", "/", "\\");
- System.out.format("%5s%6s%8s%6s%8s%6s%8s%6s%8s%6s%n", "/", "\\", "/", "\\", "/", "\\", "/", "\\", "/", "\\");
- System.out.format("%4s%8s%6s%8s%6s%8s%6s%8s%6s%8s%n", "/", "\\", "/", "\\", "/", "\\", "/", "\\", "/", "\\");
- System.out.format("%3s%5s%5s%4s%5s%5s%4s%5s%5s%4s%5s%5s%4s%5s%5s%n", "/", "1", "\\", "/", "2", "\\", "/", "3", "\\", "/", "4", "\\", "/", "5", "\\");
- System.out.format("%2s%12s%2s%12s%2s%12s%2s%12s%2s%12s%n", "/", "\\", "/", "\\", "/", "\\", "/", "\\", "/", "\\");
- for (int i = 0; i < 71; i++) {
- System.out.print("-");
- }
- System.out.print("\n|");
- for (Object c : cor) {
- System.out.format("%13s|", c);
- }
- System.out.print("\n|");
- for (Object n : nacionalidade) {
- System.out.format("%13s|", n);
- }
- System.out.print("\n|");
- for (Object a : animal) {
- System.out.format("%13s|", a);
- }
- System.out.print("\n|");
- for (Object b : bebida) {
- System.out.format("%13s|", b);
- }
- System.out.print("\n|");
- for (Object c : cigarro) {
- System.out.format("%13s|", c);
- }
- System.out.print("");
- System.out.println();
- for (int i = 0; i < 71; i++) {
- System.out.print("-");
- }
- System.out.print("\n");
- int agua = 0;
- int zebra = 0;
- for (int i = 0; i < 5; i++) {
- if (bebida.get(i) == Bebida.AGUA) {
- agua = i;
- }
- if (animal.get(i) == Animal.ZEBRA) {
- zebra = i;
- }
- }
- System.out.println("Quem bebe água?");
- System.out.println(nacionalidade.get(agua));
- System.out.println("Quem é o dono da zebra?");
- System.out.println(nacionalidade.get(zebra));
- System.out.println("\nResultado Número: "+cont);
- }
- private enum Cor {
- VERMELHA,
- AZUL,
- VERDE,
- MARFIM,
- AMARELA
- }
- private enum Nacionalidade {
- NORUEGUES,
- INGLES,
- ESPANHOL,
- UCRANIANO,
- JAPONES
- }
- private enum Animal {
- CAO,
- CARACOIS,
- RAPOSA,
- CAVALO,
- ZEBRA
- }
- private enum Bebida {
- CAFE,
- CHA,
- LEITE,
- SUMO,
- AGUA
- }
- private enum Cigarro {
- OLDGOLD,
- KOOLS,
- CHESTERFIELDS,
- LUCKYSTRIKE,
- PARLIAMENTS
- }
- /**
- *
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- //Cria os objectos
- Object[] cores = new Object[]{Cor.AMARELA, Cor.AZUL, Cor.MARFIM, Cor.VERDE, Cor.VERMELHA};
- Object[] nacionalidades = new Object[]{Nacionalidade.ESPANHOL, Nacionalidade.INGLES, Nacionalidade.JAPONES, Nacionalidade.NORUEGUES, Nacionalidade.UCRANIANO};
- Object[] animais = new Object[]{Animal.CAO, Animal.CARACOIS, Animal.CAVALO, Animal.RAPOSA, Animal.ZEBRA};
- Object[] bebidas = new Object[]{Bebida.AGUA, Bebida.CAFE, Bebida.CHA, Bebida.LEITE, Bebida.SUMO};
- Object[] cigarros = new Object[]{Cigarro.CHESTERFIELDS, Cigarro.KOOLS, Cigarro.LUCKYSTRIKE, Cigarro.OLDGOLD, Cigarro.PARLIAMENTS};
- //Obtém todas as permutações
- List<List<Object>> permCores = new Permute2().getPermutation(cores);
- List<List<Object>> permNacionalidades = new Permute2().getPermutation(nacionalidades);
- List<List<Object>> permAnimais = new Permute2().getPermutation(animais);
- List<List<Object>> permBebidas = new Permute2().getPermutation(bebidas);
- List<List<Object>> permCigarros = new Permute2().getPermutation(cigarros);
- int cont = 1; // Conta o numero de respostas caso exista mais do que 1
- //10. O Norueguês vive na primeira casa.
- for (List<Object> n : permNacionalidades) {
- if (n.indexOf(Nacionalidade.NORUEGUES) == 0) {
- //9. Leite é bebido na casa do meio.
- //5. O Ucraniano bebe chá.
- for (List<Object> b : permBebidas) {
- if (b.indexOf(Bebida.LEITE) == 2
- && n.indexOf(Nacionalidade.UCRANIANO) == b.indexOf(Bebida.CHA)) {
- //6. A casa verde está imediatamente à direita da casa de marfim.
- //2. O Ingês mora na casa vermelha
- //4. O café é bebido na casa verde.
- //15. O Norueguês vive ao lado da casa azul.
- for (List<Object> co : permCores) {
- if (co.indexOf(Cor.VERDE) == co.indexOf(Cor.MARFIM) + 1
- && n.indexOf(Nacionalidade.INGLES) == co.indexOf(Cor.VERMELHA)
- && b.indexOf(Bebida.CAFE) == co.indexOf(Cor.VERDE)
- && (n.indexOf(Nacionalidade.NORUEGUES) == co.indexOf(Cor.AZUL) + 1
- || n.indexOf(Nacionalidade.NORUEGUES) == co.indexOf(Cor.AZUL) - 1)) {
- //3. O Espanhol tem um cão.
- for (List<Object> a : permAnimais) {
- if (n.indexOf(Nacionalidade.ESPANHOL) == a.indexOf(Animal.CAO)) {
- //7. O fumador de Old Gold é dono de caracóis.
- //8. Kools são fumados na casa amarela.
- //11. O homem que fuma Chesterfields vive na casa ao lado do homem com a raposa.
- //12. Kools são fumados na casa ao lado daquela onde o cavalo é guardado.
- //13. O fumador de Lucky Strike bebe sumo de laranja.
- //14. O Japonês fuma Parliaments.
- for (List<Object> ci : permCigarros) {
- if (ci.indexOf(Cigarro.OLDGOLD) == a.indexOf(Animal.CARACOIS)
- && ci.indexOf(Cigarro.KOOLS) == co.indexOf(Cor.AMARELA)
- && (ci.indexOf(Cigarro.CHESTERFIELDS) == a.indexOf(Animal.RAPOSA) + 1
- || ci.indexOf(Cigarro.CHESTERFIELDS) == a.indexOf(Animal.RAPOSA) - 1)
- && (ci.indexOf(Cigarro.KOOLS) == a.indexOf(Animal.CAVALO) + 1
- || ci.indexOf(Cigarro.KOOLS) == a.indexOf(Animal.CAVALO) - 1)
- && ci.indexOf(Cigarro.LUCKYSTRIKE) == b.indexOf(Bebida.SUMO)
- && n.indexOf(Nacionalidade.JAPONES) == ci.indexOf(Cigarro.PARLIAMENTS)) {
- //Imprime o resultado
- printResult(co, n, a, b, ci, cont);
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement