__rain1

check cages

Jun 12th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. for(Ruleset rule : rules){
  2.  
  3.             if(!rule.hasEmptyCells()){
  4.  
  5.                
  6.                 switch(rule.getOperation()){
  7.                     case '+' :
  8.                         int sum = 0;
  9.  
  10.                         for(Cell c : rule.getCells()){
  11.                             sum += c.getValue();
  12.                         }
  13.  
  14.  
  15.                         if(sum != rule.getTotal()){
  16.                             return false;
  17.                         }
  18.  
  19.                         break;
  20.  
  21.                     case '-' :
  22.                        
  23.                         int difference = 0;
  24.                         for(Cell c : rule.getCells()){
  25.                             difference = Math.abs(difference - c.getValue());
  26.                         }
  27.  
  28.                         if(difference != rule.getTotal()){
  29.                             return false;
  30.                         }      
  31.  
  32.                         break;
  33.  
  34.                     case '*' :
  35.  
  36.                         int product = 1;
  37.                         for(Cell c : rule.getCells()){
  38.                             product *= c.getValue();
  39.                         }
  40.  
  41.                         if(product != rule.getTotal()){
  42.                             return false;
  43.                         }  
  44.                        
  45.                         break;
  46.  
  47.                     case '/' :
  48.                        
  49.                         float total = 1;
  50.                         int temp = 0;
  51.                         for(Cell c : rule.getCells()){
  52.                              temp = c.getValue();
  53.                             if(temp > total){
  54.                                 total = (float)temp/total;
  55.                             }else if(temp < total){
  56.                                 total = total/temp;
  57.                             }
  58.                         }
  59.  
  60.                         if(total != rule.getTotal()){
  61.                             return false;
  62.                         }
  63.                         break;
  64.  
  65.  
  66.                 }//close switch
Add Comment
Please, Sign In to add comment