Guest User

Untitled

a guest
Jan 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1.  
  2.  
  3. public class Koordinatensystem {
  4.     public char[][] koordinaten;
  5.     private Faltung f;
  6.     private int currentX;
  7.     private int currentY;
  8.     private int connectionX;
  9.     private int connectionY;
  10.     private int currentDirection;
  11.    
  12.     public Koordinatensystem(Faltung f) {
  13.         this.f = f;
  14.         int dimension = f.getSaeuren().size()*2 + 1;
  15.        
  16.         koordinaten = new char[dimension][dimension];
  17.         init();
  18.         if(!fill())
  19.             System.out.println("OVERLAP DETECTED ! ");
  20.         else {
  21.             print();
  22.             System.out.println("Energy is: " + computeEnergy());
  23.         }
  24.     }
  25.    
  26.     private int computeEnergy() {
  27.         int energy = 0;
  28.         char c1;
  29.         char c2;
  30.         for(int i = 0; i < koordinaten.length; i++) {
  31.             for(int j = 2; j < koordinaten[i].length; j+=2) {
  32.                 boolean validChar = koordinaten[i][j-2] != 'x' && koordinaten[i][j] != 'x';
  33.                 if(validChar) {
  34.                     c1 = koordinaten[i][j-2];
  35.                     c2 = koordinaten[i][j-2];
  36.                     if(c1 == c2 && koordinaten[i][j-1] == 'x') {
  37.                         System.out.println("Match in row " + i + " Column " + j);
  38.                         energy++;
  39.                     }
  40.                 }
  41.             }
  42.         }
  43.        
  44.         return energy;
  45.     }
  46.    
  47.     private void init() {
  48.         for(int i = 0; i < koordinaten.length; i++) {
  49.             for(int j = 0; j < koordinaten[i].length; j++) {
  50.                 koordinaten[i][j] = 'x';
  51.             }
  52.         }
  53.     }
  54.    
  55.     public void print() {
  56.         for(int i = 0; i < koordinaten.length; i++) {
  57.             for(int j = 0; j < koordinaten[i].length; j++) {
  58.                 System.out.print(koordinaten[i][j] + " ");
  59.             }
  60.             System.out.println("\n");
  61.         }
  62.         System.out.println("----- ENDE ----");
  63.     }
  64.    
  65.     private boolean fill() {
  66.         currentX = f.getSaeuren().size();
  67.         currentY = currentX;
  68.         currentDirection = Direction.RIGHT;
  69.         boolean first = true;
  70.        
  71.         for(Aminosaeure s : f.getSaeuren()) {
  72.             print();
  73.            
  74.             if(first) {
  75.                 koordinaten[currentY][currentX] =  (char) (s.getColor() + 48);
  76.                 currentDirection = Direction.RIGHT;
  77.                 first = false;
  78.                 continue;
  79.             }
  80.                 switch(currentDirection) {
  81.                 case Direction.DOWN:
  82.                     if(s.getDirection() == Direction.RIGHT) {
  83.                         currentX = currentX - 1;
  84.                         currentDirection = Direction.LEFT;
  85.                     }
  86.                     else if(s.getDirection() == Direction.LEFT) {
  87.                         currentX = currentX + 1;
  88.                         currentDirection = Direction.RIGHT;
  89.                     }
  90.                     else if(s.getDirection() == Direction.STRAIGHT) {
  91.                         currentY = currentY + 1;
  92.                     }
  93.                     break;
  94.                 case Direction.LEFT:
  95.                     if(s.getDirection() == Direction.RIGHT) {
  96.                         currentY = currentY - 1;
  97.                         currentDirection = Direction.UP;
  98.                     }
  99.                     else if(s.getDirection() == Direction.LEFT) {
  100.                         currentY = currentY + 1;
  101.                         currentDirection = Direction.DOWN;
  102.                     }
  103.                     else if(s.getDirection() == Direction.STRAIGHT) {
  104.                         currentX = currentX - 1;
  105.                     }
  106.                     break;
  107.                 case Direction.RIGHT:
  108.                     if(s.getDirection() == Direction.RIGHT) {
  109.                         currentY = currentY + 1;
  110.                         currentDirection = Direction.DOWN;
  111.                     }
  112.                     else if(s.getDirection() == Direction.LEFT) {
  113.                         currentY = currentY - 1;
  114.                         currentDirection = Direction.UP;
  115.                     }
  116.                     else if(s.getDirection() == Direction.STRAIGHT)
  117.                         currentX = currentX + 1;
  118.                     break;
  119.                 case Direction.UP:
  120.                     if(s.getDirection() == Direction.RIGHT) {
  121.                         currentX = currentX + 1;
  122.                         currentDirection = Direction.LEFT;
  123.                     }
  124.                     else if(s.getDirection() == Direction.LEFT) {
  125.                         currentX = currentX - 1;
  126.                         currentDirection = Direction.RIGHT;
  127.                     }
  128.                     else if(s.getDirection() == Direction.STRAIGHT) {
  129.                         currentY = currentY - 1;
  130.                     }
  131.                     break;
  132.                 }
  133.                
  134.                 if(koordinaten[currentY][currentX] != 'x')
  135.                     return false;
  136.                
  137.                 koordinaten[currentY][currentX] = (char) (s.getColor() + 48);
  138.         }
  139.        
  140.         return true;
  141.     }
  142. }
Add Comment
Please, Sign In to add comment