Advertisement
Guest User

MCMoore

a guest
Jan 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. package mc;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import Utilities.Ziarno;
  8. import automats.Automat;
  9.  
  10. public class MCMoore implements MCAutomat {
  11.  
  12. private final static String TAG = "MCMoore";
  13.  
  14. @Override
  15. public void makeStep(Ziarno[][] first, int type) {
  16.  
  17.  
  18. int w = first.length;
  19. int h = first[0].length;
  20.  
  21. List<Pair<Integer, Integer>> boundaryList = new ArrayList<>();
  22.  
  23. for (int i = 0; i < w; i++) {
  24. for (int j = 0; j < h; j++) {
  25. if(!BoundaryHelper.isBorder(i,j,first))
  26. continue;
  27. boundaryList.add(Pair.create(i,j));
  28. }
  29. }
  30.  
  31. while (!boundaryList.isEmpty()) {
  32.  
  33. Random rand = new Random();
  34.  
  35. int los1 = rand.nextInt(boundaryList.size());
  36. Pair<Integer, Integer> pair = boundaryList.remove(los1);
  37. int x = pair.first;
  38. int y = pair.second;
  39.  
  40.  
  41. Ziarno oldd = first[x][y];
  42.  
  43. List<Ziarno> neibs = getNeib(first, x, y, type);
  44.  
  45. int oldE = calculateEnergy(oldd, neibs);
  46.  
  47. Ziarno neww;
  48. int vvv = 0;
  49. while (true) {
  50. vvv++;
  51. Ziarno los = neibs.get(rand.nextInt(neibs.size()));
  52. if (los.id != oldd.id) {
  53. neww = los;
  54. break;
  55. }
  56.  
  57. if(vvv >= neibs.size()) {
  58. neww = los;
  59. break;
  60. }
  61. }
  62.  
  63. int newE = calculateEnergy(neww, neibs);
  64.  
  65. if (newE <= oldE) {
  66. first[x][y] = neww;
  67. } else {
  68. // int los = rand.nextInt(100);
  69. // if (los < 60)
  70. // first[x][y] = neww;
  71. }
  72. }
  73.  
  74. }
  75.  
  76.  
  77. private List<Ziarno> getNeib(Ziarno[][] first, int i, int j, int type){
  78.  
  79. List<Ziarno> neibs = new ArrayList<>(4);
  80.  
  81.  
  82. int wie_1;
  83. int wie_2;
  84. int wie_3;
  85. int kol_1;
  86. int kol_2;
  87. int kol_3;
  88.  
  89. int w = first.length;
  90. int h = first[0].length;
  91.  
  92. if (type == Automat.PERIOID) {
  93. wie_1 = i - 1;
  94. if (wie_1 < 0)
  95. wie_1 = w - 1;
  96. wie_2 = i;
  97. wie_3 = i + 1;
  98. if (wie_3 > w - 1)
  99. wie_3 = 0;
  100.  
  101. kol_1 = j - 1;
  102. if (kol_1 < 0)
  103. kol_1 = h - 1;
  104. kol_2 = j;
  105. kol_3 = j + 1;
  106. if (kol_3 > h - 1)
  107. kol_3 = 0;
  108. } else {
  109. wie_1 = i - 1;
  110. if (wie_1 < 0)
  111. wie_1 = 0;
  112. wie_2 = i;
  113. wie_3 = i + 1;
  114. if (wie_3 > w - 1)
  115. wie_3 = w - 1;
  116.  
  117. kol_1 = j - 1;
  118. if (kol_1 < 0)
  119. kol_1 = 0;
  120. kol_2 = j;
  121. kol_3 = j + 1;
  122. if (kol_3 > h - 1)
  123. kol_3 = h - 1;
  124. }
  125.  
  126.  
  127. neibs.add(first[wie_1][kol_1]);
  128. neibs.add(first[wie_2][kol_1]);
  129. neibs.add(first[wie_3][kol_1]);
  130.  
  131. neibs.add(first[wie_1][kol_2]);
  132. neibs.add(first[wie_3][kol_2]);
  133.  
  134. neibs.add(first[wie_1][kol_3]);
  135. neibs.add(first[wie_2][kol_3]);
  136. neibs.add(first[wie_3][kol_3]);
  137.  
  138. return neibs;
  139. }
  140.  
  141. private int calculateEnergy(Ziarno cell, List<Ziarno> neibs){
  142. int energy = 0;
  143.  
  144. for (Ziarno c :
  145. neibs) {
  146. if(cell.id != c.id)
  147. energy++;
  148. }
  149.  
  150. return energy;
  151. }
  152.  
  153. private boolean isNeibsRec(Ziarno[][] first, int i, int j, int type) {
  154.  
  155. List<Ziarno> neibs = getNeib(first, i, j, type);
  156.  
  157. for (Ziarno c :
  158. neibs) {
  159. if (c.isNew)
  160. return true;
  161. }
  162.  
  163. return false;
  164. }
  165.  
  166.  
  167. @Override
  168. public void makeRecStep(Ziarno[][] first, int type) {
  169.  
  170.  
  171. int w = first.length;
  172. int h = first[0].length;
  173.  
  174. List<Pair<Integer, Integer>> boundaryList = new ArrayList<>();
  175.  
  176. for (int i = 0; i < w; i++) {
  177. for (int j = 0; j < h; j++) {
  178. if (!BoundaryHelper.isBorder(i, j, first))
  179. continue;
  180. boundaryList.add(Pair.create(i, j));
  181. }
  182. }
  183.  
  184. int basicSize = boundaryList.size();
  185. int finalSize = (int) (basicSize * 0.25);
  186. while (boundaryList.size() > finalSize) {
  187. //Log.e(TAG, "boundaryList size = " + boundaryList.size() + " final: " + finalSize);
  188.  
  189. Random rand = new Random();
  190. int los1 = rand.nextInt(boundaryList.size());
  191. Pair<Integer, Integer> pair = boundaryList.remove(los1);
  192. int x = pair.first;
  193. int y = pair.second;
  194.  
  195. //check if neibs recrystalized
  196. if(!isNeibsRec(first, x, y, type))
  197. continue;
  198.  
  199.  
  200. Ziarno oldd = first[x][y];
  201.  
  202. List<Ziarno> neibs = getNeib(first, x, y, type);
  203.  
  204. int oldE = calculateEnergy(oldd, neibs) + (int)oldd.points;
  205.  
  206. Ziarno neww;
  207. int vvv = 0;
  208. while (true) {
  209. vvv++;
  210. Ziarno los = neibs.get(rand.nextInt(neibs.size()));
  211. if (los.id != oldd.id) {
  212. neww = los;
  213. break;
  214. }
  215.  
  216. if (vvv >= neibs.size()) {
  217. neww = los;
  218. break;
  219. }
  220. }
  221.  
  222. int newE = calculateEnergy(neww, neibs);
  223.  
  224. if (newE <= oldE) {
  225. first[x][y] = neww;
  226. } else {
  227. // int los = rand.nextInt(100);
  228. // if (los < 60)
  229. // first[x][y] = neww;
  230. }
  231. }
  232.  
  233. }
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement