Advertisement
Guest User

EinsteinsRiddle2

a guest
May 3rd, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.23 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package einsteinsriddle;
  6.  
  7. import java.util.List;
  8.  
  9. /**
  10.  *
  11.  * @author Joel
  12.  */
  13. public class EinsteinsRiddle2 {
  14.  
  15.     /**
  16.      * Imprime o resultado do enigma
  17.      *
  18.      * @param cor
  19.      * @param nacionalidade
  20.      * @param animal
  21.      * @param bebida
  22.      * @param cigarro
  23.      */
  24.     private static void printResult(List<Object> cor, List<Object> nacionalidade,
  25.             List<Object> animal, List<Object> bebida, List<Object> cigarro, int cont) {
  26.         System.out.format("%8s%14s%14s%14s%14s%n", "_", "_", "_", "_", "_");
  27.         System.out.format("%9s%14s%14s%14s%14s%n", "/ \\", "/ \\", "/ \\", "/ \\", "/ \\");
  28.         System.out.format("%6s%4s%10s%4s%10s%4s%10s%4s%10s%4s%n", "/", "\\", "/", "\\", "/", "\\", "/", "\\", "/", "\\");
  29.         System.out.format("%5s%6s%8s%6s%8s%6s%8s%6s%8s%6s%n", "/", "\\", "/", "\\", "/", "\\", "/", "\\", "/", "\\");
  30.         System.out.format("%4s%8s%6s%8s%6s%8s%6s%8s%6s%8s%n", "/", "\\", "/", "\\", "/", "\\", "/", "\\", "/", "\\");
  31.         System.out.format("%3s%5s%5s%4s%5s%5s%4s%5s%5s%4s%5s%5s%4s%5s%5s%n", "/", "1", "\\", "/", "2", "\\", "/", "3", "\\", "/", "4", "\\", "/", "5", "\\");
  32.         System.out.format("%2s%12s%2s%12s%2s%12s%2s%12s%2s%12s%n", "/", "\\", "/", "\\", "/", "\\", "/", "\\", "/", "\\");
  33.  
  34.         for (int i = 0; i < 71; i++) {
  35.             System.out.print("-");
  36.         }
  37.         System.out.print("\n|");
  38.         for (Object c : cor) {
  39.             System.out.format("%13s|", c);
  40.         }
  41.         System.out.print("\n|");
  42.         for (Object n : nacionalidade) {
  43.             System.out.format("%13s|", n);
  44.         }
  45.         System.out.print("\n|");
  46.         for (Object a : animal) {
  47.             System.out.format("%13s|", a);
  48.         }
  49.         System.out.print("\n|");
  50.         for (Object b : bebida) {
  51.             System.out.format("%13s|", b);
  52.         }
  53.         System.out.print("\n|");
  54.         for (Object c : cigarro) {
  55.             System.out.format("%13s|", c);
  56.         }
  57.         System.out.print("");
  58.         System.out.println();
  59.         for (int i = 0; i < 71; i++) {
  60.             System.out.print("-");
  61.         }
  62.         System.out.print("\n");
  63.  
  64.         int agua = 0;
  65.         int zebra = 0;
  66.  
  67.         for (int i = 0; i < 5; i++) {
  68.             if (bebida.get(i) == Bebida.AGUA) {
  69.                 agua = i;
  70.             }
  71.             if (animal.get(i) == Animal.ZEBRA) {
  72.                 zebra = i;
  73.             }
  74.         }
  75.  
  76.         System.out.println("Quem bebe água?");
  77.         System.out.println(nacionalidade.get(agua));
  78.         System.out.println("Quem é o dono da zebra?");
  79.         System.out.println(nacionalidade.get(zebra));
  80.        
  81.         System.out.println("\nResultado Número: "+cont);
  82.     }
  83.  
  84.     private enum Cor {
  85.  
  86.         VERMELHA,
  87.         AZUL,
  88.         VERDE,
  89.         MARFIM,
  90.         AMARELA
  91.     }
  92.  
  93.     private enum Nacionalidade {
  94.  
  95.         NORUEGUES,
  96.         INGLES,
  97.         ESPANHOL,
  98.         UCRANIANO,
  99.         JAPONES
  100.     }
  101.  
  102.     private enum Animal {
  103.  
  104.         CAO,
  105.         CARACOIS,
  106.         RAPOSA,
  107.         CAVALO,
  108.         ZEBRA
  109.     }
  110.  
  111.     private enum Bebida {
  112.  
  113.         CAFE,
  114.         CHA,
  115.         LEITE,
  116.         SUMO,
  117.         AGUA
  118.     }
  119.  
  120.     private enum Cigarro {
  121.  
  122.         OLDGOLD,
  123.         KOOLS,
  124.         CHESTERFIELDS,
  125.         LUCKYSTRIKE,
  126.         PARLIAMENTS
  127.     }
  128.  
  129.     /**
  130.      *
  131.      * @param args the command line arguments
  132.      */
  133.     public static void main(String[] args) {
  134.  
  135.         //Cria os objectos
  136.         Object[] cores = new Object[]{Cor.AMARELA, Cor.AZUL, Cor.MARFIM, Cor.VERDE, Cor.VERMELHA};
  137.         Object[] nacionalidades = new Object[]{Nacionalidade.ESPANHOL, Nacionalidade.INGLES, Nacionalidade.JAPONES, Nacionalidade.NORUEGUES, Nacionalidade.UCRANIANO};
  138.         Object[] animais = new Object[]{Animal.CAO, Animal.CARACOIS, Animal.CAVALO, Animal.RAPOSA, Animal.ZEBRA};
  139.         Object[] bebidas = new Object[]{Bebida.AGUA, Bebida.CAFE, Bebida.CHA, Bebida.LEITE, Bebida.SUMO};
  140.         Object[] cigarros = new Object[]{Cigarro.CHESTERFIELDS, Cigarro.KOOLS, Cigarro.LUCKYSTRIKE, Cigarro.OLDGOLD, Cigarro.PARLIAMENTS};
  141.  
  142.         //Obtém todas as permutações
  143.         List<List<Object>> permCores = new Permute2().getPermutation(cores);
  144.         List<List<Object>> permNacionalidades = new Permute2().getPermutation(nacionalidades);
  145.         List<List<Object>> permAnimais = new Permute2().getPermutation(animais);
  146.         List<List<Object>> permBebidas = new Permute2().getPermutation(bebidas);
  147.         List<List<Object>> permCigarros = new Permute2().getPermutation(cigarros);
  148.  
  149.         int cont = 1; // Conta o numero de respostas caso exista mais do que 1
  150.        
  151.         //10. O Norueguês vive na primeira casa.
  152.         for (List<Object> n : permNacionalidades) {
  153.             if (n.indexOf(Nacionalidade.NORUEGUES) == 0) {
  154.  
  155.                 //9. Leite é bebido na casa do meio.
  156.                 //5. O Ucraniano bebe chá.
  157.                 for (List<Object> b : permBebidas) {
  158.                     if (b.indexOf(Bebida.LEITE) == 2
  159.                             && n.indexOf(Nacionalidade.UCRANIANO) == b.indexOf(Bebida.CHA)) {
  160.  
  161.                         //6. A casa verde está imediatamente à direita da casa de marfim.
  162.                         //2. O Ingês mora na casa vermelha
  163.                         //4. O café é bebido na casa verde.
  164.                         //15. O Norueguês vive ao lado da casa azul.
  165.                         for (List<Object> co : permCores) {
  166.                             if (co.indexOf(Cor.VERDE) == co.indexOf(Cor.MARFIM) + 1
  167.                                     && n.indexOf(Nacionalidade.INGLES) == co.indexOf(Cor.VERMELHA)
  168.                                     && b.indexOf(Bebida.CAFE) == co.indexOf(Cor.VERDE)
  169.                                     && (n.indexOf(Nacionalidade.NORUEGUES) == co.indexOf(Cor.AZUL) + 1
  170.                                     || n.indexOf(Nacionalidade.NORUEGUES) == co.indexOf(Cor.AZUL) - 1)) {
  171.  
  172.                                 //3. O Espanhol tem um cão.
  173.                                 for (List<Object> a : permAnimais) {
  174.                                     if (n.indexOf(Nacionalidade.ESPANHOL) == a.indexOf(Animal.CAO)) {
  175.  
  176.                                         //7. O fumador de Old Gold é dono de caracóis.
  177.                                         //8. Kools são fumados na casa amarela.
  178.                                         //11. O homem que fuma Chesterfields vive na casa ao lado do homem com a raposa.
  179.                                         //12. Kools são fumados na casa ao lado daquela onde o cavalo é guardado.
  180.                                         //13. O fumador de Lucky Strike bebe sumo de laranja.
  181.                                         //14. O Japonês fuma Parliaments.
  182.                                         for (List<Object> ci : permCigarros) {
  183.                                             if (ci.indexOf(Cigarro.OLDGOLD) == a.indexOf(Animal.CARACOIS)
  184.                                                     && ci.indexOf(Cigarro.KOOLS) == co.indexOf(Cor.AMARELA)
  185.                                                     && (ci.indexOf(Cigarro.CHESTERFIELDS) == a.indexOf(Animal.RAPOSA) + 1
  186.                                                     || ci.indexOf(Cigarro.CHESTERFIELDS) == a.indexOf(Animal.RAPOSA) - 1)
  187.                                                     && (ci.indexOf(Cigarro.KOOLS) == a.indexOf(Animal.CAVALO) + 1
  188.                                                     || ci.indexOf(Cigarro.KOOLS) == a.indexOf(Animal.CAVALO) - 1)
  189.                                                     && ci.indexOf(Cigarro.LUCKYSTRIKE) == b.indexOf(Bebida.SUMO)
  190.                                                     && n.indexOf(Nacionalidade.JAPONES) == ci.indexOf(Cigarro.PARLIAMENTS)) {
  191.  
  192.                                                 //Imprime o resultado
  193.                                                 printResult(co, n, a, b, ci, cont);
  194.                                             }
  195.                                         }
  196.                                     }
  197.                                 }
  198.                             }
  199.                         }
  200.                     }
  201.  
  202.                 }
  203.             }
  204.         }
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement