daniel_lawson9999

Dominoes

May 18th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1.  public static boolean hasChain(ArrayList<Domino> dominoes, int first, int second){//5,5
  2.         boolean match = false;
  3.         for(int i = 0; i < dominoes.size();i++){
  4.             if((dominoes.get(i).first() == first && dominoes.get(i).second() == second) ||
  5.                     (dominoes.get(i).first() == second && dominoes.get(i).second() == first)){
  6.                 match = true;
  7.             }
  8.         }
  9.         if (match){
  10.          return match;
  11.         }
  12.         else {
  13.           for(int i = 0;i<dominoes.size();i++){
  14.               if(dominoes.get(i).first()==first){
  15.                   int other = dominoes.get(i).second();
  16.                   ArrayList<Domino> copy = new ArrayList<>();
  17.                   copy.addAll(dominoes);
  18.                   copy.remove(copy.get(i));
  19.                   if(hasChain(copy,other,second)){
  20.                       return true;
  21.                   }
  22.               }
  23.               else if(dominoes.get(i).second()==first){
  24.                   int other = dominoes.get(i).first();
  25.                   ArrayList<Domino> copy = new ArrayList<>();
  26.                   copy.addAll(dominoes);
  27.                   copy.remove(copy.get(i));
  28.                   if(hasChain(copy,other,second)){
  29.                       return true;
  30.                   }
  31.               }
  32.           }
  33.           return false;
  34.         }
  35.  
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment