Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mc;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import Utilities.Ziarno;
- import automats.Automat;
- public class MCMoore implements MCAutomat {
- private final static String TAG = "MCMoore";
- @Override
- public void makeStep(Ziarno[][] first, int type) {
- int w = first.length;
- int h = first[0].length;
- List<Pair<Integer, Integer>> boundaryList = new ArrayList<>();
- for (int i = 0; i < w; i++) {
- for (int j = 0; j < h; j++) {
- if(!BoundaryHelper.isBorder(i,j,first))
- continue;
- boundaryList.add(Pair.create(i,j));
- }
- }
- while (!boundaryList.isEmpty()) {
- Random rand = new Random();
- int los1 = rand.nextInt(boundaryList.size());
- Pair<Integer, Integer> pair = boundaryList.remove(los1);
- int x = pair.first;
- int y = pair.second;
- Ziarno oldd = first[x][y];
- List<Ziarno> neibs = getNeib(first, x, y, type);
- int oldE = calculateEnergy(oldd, neibs);
- Ziarno neww;
- int vvv = 0;
- while (true) {
- vvv++;
- Ziarno los = neibs.get(rand.nextInt(neibs.size()));
- if (los.id != oldd.id) {
- neww = los;
- break;
- }
- if(vvv >= neibs.size()) {
- neww = los;
- break;
- }
- }
- int newE = calculateEnergy(neww, neibs);
- if (newE <= oldE) {
- first[x][y] = neww;
- } else {
- // int los = rand.nextInt(100);
- // if (los < 60)
- // first[x][y] = neww;
- }
- }
- }
- private List<Ziarno> getNeib(Ziarno[][] first, int i, int j, int type){
- List<Ziarno> neibs = new ArrayList<>(4);
- int wie_1;
- int wie_2;
- int wie_3;
- int kol_1;
- int kol_2;
- int kol_3;
- int w = first.length;
- int h = first[0].length;
- if (type == Automat.PERIOID) {
- wie_1 = i - 1;
- if (wie_1 < 0)
- wie_1 = w - 1;
- wie_2 = i;
- wie_3 = i + 1;
- if (wie_3 > w - 1)
- wie_3 = 0;
- kol_1 = j - 1;
- if (kol_1 < 0)
- kol_1 = h - 1;
- kol_2 = j;
- kol_3 = j + 1;
- if (kol_3 > h - 1)
- kol_3 = 0;
- } else {
- wie_1 = i - 1;
- if (wie_1 < 0)
- wie_1 = 0;
- wie_2 = i;
- wie_3 = i + 1;
- if (wie_3 > w - 1)
- wie_3 = w - 1;
- kol_1 = j - 1;
- if (kol_1 < 0)
- kol_1 = 0;
- kol_2 = j;
- kol_3 = j + 1;
- if (kol_3 > h - 1)
- kol_3 = h - 1;
- }
- neibs.add(first[wie_1][kol_1]);
- neibs.add(first[wie_2][kol_1]);
- neibs.add(first[wie_3][kol_1]);
- neibs.add(first[wie_1][kol_2]);
- neibs.add(first[wie_3][kol_2]);
- neibs.add(first[wie_1][kol_3]);
- neibs.add(first[wie_2][kol_3]);
- neibs.add(first[wie_3][kol_3]);
- return neibs;
- }
- private int calculateEnergy(Ziarno cell, List<Ziarno> neibs){
- int energy = 0;
- for (Ziarno c :
- neibs) {
- if(cell.id != c.id)
- energy++;
- }
- return energy;
- }
- private boolean isNeibsRec(Ziarno[][] first, int i, int j, int type) {
- List<Ziarno> neibs = getNeib(first, i, j, type);
- for (Ziarno c :
- neibs) {
- if (c.isNew)
- return true;
- }
- return false;
- }
- @Override
- public void makeRecStep(Ziarno[][] first, int type) {
- int w = first.length;
- int h = first[0].length;
- List<Pair<Integer, Integer>> boundaryList = new ArrayList<>();
- for (int i = 0; i < w; i++) {
- for (int j = 0; j < h; j++) {
- if (!BoundaryHelper.isBorder(i, j, first))
- continue;
- boundaryList.add(Pair.create(i, j));
- }
- }
- int basicSize = boundaryList.size();
- int finalSize = (int) (basicSize * 0.25);
- while (boundaryList.size() > finalSize) {
- //Log.e(TAG, "boundaryList size = " + boundaryList.size() + " final: " + finalSize);
- Random rand = new Random();
- int los1 = rand.nextInt(boundaryList.size());
- Pair<Integer, Integer> pair = boundaryList.remove(los1);
- int x = pair.first;
- int y = pair.second;
- //check if neibs recrystalized
- if(!isNeibsRec(first, x, y, type))
- continue;
- Ziarno oldd = first[x][y];
- List<Ziarno> neibs = getNeib(first, x, y, type);
- int oldE = calculateEnergy(oldd, neibs) + (int)oldd.points;
- Ziarno neww;
- int vvv = 0;
- while (true) {
- vvv++;
- Ziarno los = neibs.get(rand.nextInt(neibs.size()));
- if (los.id != oldd.id) {
- neww = los;
- break;
- }
- if (vvv >= neibs.size()) {
- neww = los;
- break;
- }
- }
- int newE = calculateEnergy(neww, neibs);
- if (newE <= oldE) {
- first[x][y] = neww;
- } else {
- // int los = rand.nextInt(100);
- // if (los < 60)
- // first[x][y] = neww;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement