Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.badlogic.gdx.utils.Array;
- /**
- * Created by serdjuk on 08.07.17.
- */
- /**
- * Fill algorithm
- */
- public class CalculateArea {
- public static final int MIN = 3; // минимальное кол-во ячеек для уничтожения
- private Array<Cell> cellsForDie; // массив будующих ячеек которые нужно уничтожить (3 и более)
- private int counter; // счетчик сохраненных ячеек для удаления
- private int passes; // общее количество проходов при поиске всех цветов (row*column)
- private int onePass; // количество проходов при поиске одного цвета
- private int r; // ряд
- private int c; // колонка
- private int[][] colorsArea; // в этот массив занести катру цветов
- private int currentColor; // текущий цвет для поиска рядом
- public CalculateArea() {
- cellsForDie = new Array<Cell>();
- }
- private void rePack() {
- colorsArea = new int[][]{
- {1, 1, 1, 7, 1, 1},
- {3, 1, 1, 5, 5, 1},
- {4, 5, 1, 4, 5, 1},
- {4, 1, 1, 1, 5, 5}
- };
- }
- public void update() {
- rePack();
- cellsForDie.clear();
- passes = 0;
- for (int r = 0; r < colorsArea.length; r++) {
- for (int c = 0; c < colorsArea[r].length; c++) {
- if (colorsArea[r][c] == 0) continue;
- cellsForDie.add(new Cell(r, c));
- counter = cellsForDie.size - 1;
- currentColor = colorsArea[r][c];
- colorsArea[r][c] = 0;
- onePass = 0;
- search();
- if (onePass < MIN)
- cellsForDie.removeRange(cellsForDie.size - onePass, cellsForDie.size - 1);
- }
- }
- text();
- }
- private void search() {
- r = cellsForDie.get(counter).row;
- c = cellsForDie.get(counter).column;
- up();
- right();
- left();
- down();
- passes++;
- onePass++;
- counter++;
- if (counter > cellsForDie.size - 1) return;
- search();
- }
- private void up() {
- if (r - 1 >= 0 && currentColor == colorsArea[r - 1][c]) {
- cellsForDie.add(new Cell(r - 1, c));
- colorsArea[r - 1][c] = 0;
- }
- }
- private void right() {
- if (c + 1 <= colorsArea[0].length - 1 && currentColor == colorsArea[r][c + 1]) {
- cellsForDie.add(new Cell(r, c + 1));
- colorsArea[r][c + 1] = 0;
- }
- }
- private void left() {
- if (c - 1 >= 0 && currentColor == colorsArea[r][c - 1]) {
- cellsForDie.add(new Cell(r, c - 1));
- colorsArea[r][c - 1] = 0;
- }
- }
- private void down() {
- if (r + 1 <= colorsArea.length - 1 && currentColor == colorsArea[r + 1][c]) {
- cellsForDie.add(new Cell(r + 1, c));
- colorsArea[r + 1][c] = 0;
- }
- }
- private class Cell {
- int row;
- int column;
- Cell(int row, int column) {
- this.row = row;
- this.column = column;
- }
- }
- private void text() {
- for (Cell cell : cellsForDie) {
- System.out.print(cell.row + ", " + cell.column + " | ");
- }
- System.out.println();
- System.out.println("сохранено ячеек для удаления: " + cellsForDie.size);
- System.out.println("совершено проходов: " + passes);
- System.out.println("Показать удаленные блоки:");
- for (Cell cell : cellsForDie) {
- colorsArea[cell.row][cell.column] = 1;
- }
- for (int[] aColorsArea : colorsArea) {
- for (int anAColorsArea : aColorsArea) {
- System.out.print(anAColorsArea);
- }
- System.out.println();
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment