vito-Z80

Fill algorithm

Jul 8th, 2017
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.62 KB | None | 0 0
  1. import com.badlogic.gdx.utils.Array;
  2.  
  3. /**
  4.  * Created by serdjuk on 08.07.17.
  5.  */
  6.  
  7. /**
  8.  * Fill algorithm
  9.  */
  10.  
  11. public class CalculateArea {
  12.  
  13.     public static final int MIN = 3;    //  минимальное кол-во ячеек для уничтожения
  14.     private Array<Cell> cellsForDie;    //  массив будующих ячеек которые нужно уничтожить (3 и более)
  15.     private int counter;    //  счетчик сохраненных ячеек для удаления
  16.     private int passes;     //  общее количество проходов при поиске всех цветов (row*column)
  17.     private int onePass;    //  количество проходов при поиске одного цвета
  18.  
  19.     private int r;  //  ряд
  20.     private int c;  //  колонка
  21.  
  22.     private int[][] colorsArea; //  в этот массив занести катру цветов
  23.     private int currentColor;   //  текущий цвет для поиска рядом
  24.  
  25.  
  26.     public CalculateArea() {
  27.         cellsForDie = new Array<Cell>();
  28.     }
  29.  
  30.     private void rePack() {
  31.             colorsArea = new int[][]{
  32.                 {1, 1, 1, 7, 1, 1},
  33.                 {3, 1, 1, 5, 5, 1},
  34.                 {4, 5, 1, 4, 5, 1},
  35.                 {4, 1, 1, 1, 5, 5}
  36.         };
  37.  
  38.     }
  39.  
  40.     public void update() {
  41.         rePack();
  42.         cellsForDie.clear();
  43.         passes = 0;
  44.         for (int r = 0; r < colorsArea.length; r++) {
  45.             for (int c = 0; c < colorsArea[r].length; c++) {
  46.                 if (colorsArea[r][c] == 0) continue;
  47.                 cellsForDie.add(new Cell(r, c));
  48.                 counter = cellsForDie.size - 1;
  49.                 currentColor = colorsArea[r][c];
  50.                 colorsArea[r][c] = 0;
  51.                 onePass = 0;
  52.                 search();
  53.                 if (onePass < MIN)
  54.                     cellsForDie.removeRange(cellsForDie.size - onePass, cellsForDie.size - 1);
  55.             }
  56.         }
  57.         text();
  58.     }
  59.  
  60.     private void search() {
  61.         r = cellsForDie.get(counter).row;
  62.         c = cellsForDie.get(counter).column;
  63.         up();
  64.         right();
  65.         left();
  66.         down();
  67.         passes++;
  68.         onePass++;
  69.         counter++;
  70.         if (counter > cellsForDie.size - 1) return;
  71.         search();
  72.     }
  73.  
  74.     private void up() {
  75.         if (r - 1 >= 0 && currentColor == colorsArea[r - 1][c]) {
  76.             cellsForDie.add(new Cell(r - 1, c));
  77.             colorsArea[r - 1][c] = 0;
  78.         }
  79.     }
  80.  
  81.     private void right() {
  82.         if (c + 1 <= colorsArea[0].length - 1 && currentColor == colorsArea[r][c + 1]) {
  83.             cellsForDie.add(new Cell(r, c + 1));
  84.             colorsArea[r][c + 1] = 0;
  85.         }
  86.     }
  87.  
  88.     private void left() {
  89.         if (c - 1 >= 0 && currentColor == colorsArea[r][c - 1]) {
  90.             cellsForDie.add(new Cell(r, c - 1));
  91.             colorsArea[r][c - 1] = 0;
  92.         }
  93.     }
  94.  
  95.     private void down() {
  96.         if (r + 1 <= colorsArea.length - 1 && currentColor == colorsArea[r + 1][c]) {
  97.             cellsForDie.add(new Cell(r + 1, c));
  98.             colorsArea[r + 1][c] = 0;
  99.         }
  100.     }
  101.  
  102.     private class Cell {
  103.         int row;
  104.         int column;
  105.  
  106.         Cell(int row, int column) {
  107.             this.row = row;
  108.             this.column = column;
  109.         }
  110.     }
  111.  
  112.     private void text() {
  113.         for (Cell cell : cellsForDie) {
  114.             System.out.print(cell.row + ", " + cell.column + " | ");
  115.         }
  116.         System.out.println();
  117.         System.out.println("сохранено ячеек для удаления: " + cellsForDie.size);
  118.         System.out.println("совершено проходов: " + passes);
  119.         System.out.println("Показать удаленные блоки:");
  120.         for (Cell cell : cellsForDie) {
  121.             colorsArea[cell.row][cell.column] = 1;
  122.         }
  123.         for (int[] aColorsArea : colorsArea) {
  124.             for (int anAColorsArea : aColorsArea) {
  125.                 System.out.print(anAColorsArea);
  126.             }
  127.             System.out.println();
  128.         }
  129.         System.out.println();
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment