Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. import { Component } from '@angular/core';
  2.  
  3. @Component({
  4. selector: 'cell-grid',
  5. template: `
  6. <div>
  7. <button class="btn btn-primary" (click)="updateGeneration()">See Next Generation</button>
  8. <hr/>
  9. <div class="row" *ngFor="let gridElemState of gridElemsState; let ind = index">
  10. <div class="col-md-2" *ngFor="let gridElemState of gridElemsState[ind]">
  11. <cell [event]="gridElemState"></cell>
  12. </div>
  13. </div>
  14. </div>
  15. `
  16. })
  17.  
  18. export class CellGridComponent {
  19.  
  20. gridElemsState = [
  21. [false, false, false, false, true, false],
  22. [false, false, false, true, false, false],
  23. [true, false, false, false, true, true],
  24. [false, true, true, true, true, false],
  25. [false, false, false, false, false, false],
  26. [false, false, false, true, true, true]
  27. ];
  28.  
  29. updateGeneration() {
  30.  
  31. let data = this.gridElemsState;
  32. let newStates = [];
  33.  
  34. let i = 0;
  35. while (i < data[0].length) {
  36.  
  37. let newStatesRow = [];
  38.  
  39. let j = 0;
  40. while (j < data[0].length) {
  41.  
  42. if (data[i][j] === false) {
  43.  
  44. let neighborsArrayDead = this.parseToroidalNeighbors(data, i, j);
  45. let repeatCountsDead = this.repeatsCounter(neighborsArrayDead);
  46.  
  47. if (repeatCountsDead["true"] === 3) {
  48. newStatesRow.push(true);
  49. } else {
  50. newStatesRow.push(data[i][j]);
  51. };
  52.  
  53. } else {
  54.  
  55. let neighborsArrayAlive = this.parseToroidalNeighbors(data, i, j);
  56. let repeatCountsAlive = this.repeatsCounter(neighborsArrayAlive);
  57.  
  58. if (repeatCountsAlive["true"] === 1 || repeatCountsAlive["true"] === 0) {
  59. newStatesRow.push(false);
  60. } else if (repeatCountsAlive["true"] > 3) {
  61. newStatesRow.push(false);
  62. } else {
  63. newStatesRow.push(data[i][j]);
  64. };
  65. };
  66. j++;
  67.  
  68. };
  69. newStates.push(newStatesRow);
  70. i++;
  71. };
  72.  
  73. let x = 0;
  74. while (x < newStates.length) {
  75. let y = 0;
  76. while (y < newStates.length) {
  77. data[x][y] = newStates[x][y];
  78. y++;
  79. };
  80. x++;
  81. };
  82. };
  83.  
  84. repeatsCounter(array) {
  85.  
  86. let counts = {};
  87.  
  88. array.forEach((x) => {
  89. counts[x] = (counts[x] || 0) + 1;
  90. });
  91.  
  92. if (!counts.hasOwnProperty("true")) {
  93. counts["true"] = 0;
  94. };
  95.  
  96. return counts;
  97. };
  98.  
  99. parseToroidalNeighbors(array2D, rowIndex, columnIndex) {
  100.  
  101. let neighbors;
  102. let maxIndex = array2D[0].length - 1;
  103.  
  104. if (rowIndex === 0 && columnIndex === 0) {
  105. neighbors = [
  106. array2D[rowIndex+maxIndex][columnIndex+maxIndex],
  107. array2D[rowIndex+maxIndex][columnIndex],
  108. array2D[rowIndex+maxIndex][columnIndex+1],
  109. array2D[rowIndex][columnIndex+maxIndex],
  110. array2D[rowIndex][columnIndex+1],
  111. array2D[rowIndex+1][columnIndex+maxIndex],
  112. array2D[rowIndex+1][columnIndex],
  113. array2D[rowIndex+1][columnIndex+1]
  114. ];
  115. } else if (rowIndex === 0 && 0 < columnIndex && columnIndex < maxIndex) {
  116. neighbors = [
  117. array2D[rowIndex+maxIndex][columnIndex-1],
  118. array2D[rowIndex+maxIndex][columnIndex],
  119. array2D[rowIndex+maxIndex][columnIndex+1],
  120. array2D[rowIndex][columnIndex-1],
  121. array2D[rowIndex][columnIndex+1],
  122. array2D[rowIndex+1][columnIndex-1],
  123. array2D[rowIndex+1][columnIndex],
  124. array2D[rowIndex+1][columnIndex+1]
  125. ];
  126. } else if (rowIndex === 0 && columnIndex === maxIndex) {
  127. neighbors = [
  128. array2D[rowIndex+maxIndex][columnIndex-1],
  129. array2D[rowIndex+maxIndex][columnIndex],
  130. array2D[rowIndex+maxIndex][columnIndex-maxIndex],
  131. array2D[rowIndex][columnIndex-1],
  132. array2D[rowIndex][columnIndex-maxIndex],
  133. array2D[rowIndex+1][columnIndex-1],
  134. array2D[rowIndex+1][columnIndex],
  135. array2D[rowIndex+1][columnIndex-maxIndex]
  136. ];
  137. } else if (0 < rowIndex && rowIndex < maxIndex && columnIndex === maxIndex) {
  138. neighbors = [
  139. array2D[rowIndex-1][columnIndex-1],
  140. array2D[rowIndex-1][columnIndex],
  141. array2D[rowIndex-1][columnIndex-maxIndex],
  142. array2D[rowIndex][columnIndex-1],
  143. array2D[rowIndex][columnIndex-maxIndex],
  144. array2D[rowIndex+1][columnIndex-1],
  145. array2D[rowIndex+1][columnIndex],
  146. array2D[rowIndex+1][columnIndex-maxIndex]
  147. ];
  148. } else if (rowIndex === maxIndex && columnIndex === maxIndex) {
  149. neighbors = [
  150. array2D[rowIndex-1][columnIndex-1],
  151. array2D[rowIndex-1][columnIndex],
  152. array2D[rowIndex-1][columnIndex-maxIndex],
  153. array2D[rowIndex][columnIndex-1],
  154. array2D[rowIndex][columnIndex-maxIndex],
  155. array2D[rowIndex-maxIndex][columnIndex-1],
  156. array2D[rowIndex-maxIndex][columnIndex],
  157. array2D[rowIndex-maxIndex][columnIndex-maxIndex]
  158. ];
  159. } else if (rowIndex === maxIndex && 0 < columnIndex && columnIndex < maxIndex) {
  160. neighbors = [
  161. array2D[rowIndex-1][columnIndex-1],
  162. array2D[rowIndex-1][columnIndex],
  163. array2D[rowIndex-1][columnIndex+1],
  164. array2D[rowIndex][columnIndex-1],
  165. array2D[rowIndex][columnIndex+1],
  166. array2D[rowIndex-maxIndex][columnIndex-1],
  167. array2D[rowIndex-maxIndex][columnIndex],
  168. array2D[rowIndex-maxIndex][columnIndex+1]
  169. ];
  170. } else if (rowIndex === maxIndex && columnIndex === 0) {
  171. neighbors = [
  172. array2D[rowIndex-1][columnIndex+maxIndex],
  173. array2D[rowIndex-1][columnIndex],
  174. array2D[rowIndex-1][columnIndex+1],
  175. array2D[rowIndex][columnIndex+maxIndex],
  176. array2D[rowIndex][columnIndex+1],
  177. array2D[rowIndex-maxIndex][columnIndex+maxIndex],
  178. array2D[rowIndex-maxIndex][columnIndex],
  179. array2D[rowIndex-maxIndex][columnIndex+1]
  180. ];
  181. } else if (0 < rowIndex && rowIndex < maxIndex && columnIndex === 0) {
  182. neighbors = [
  183. array2D[rowIndex-1][columnIndex+maxIndex],
  184. array2D[rowIndex-1][columnIndex],
  185. array2D[rowIndex-1][columnIndex+1],
  186. array2D[rowIndex][columnIndex+maxIndex],
  187. array2D[rowIndex][columnIndex+1],
  188. array2D[rowIndex+1][columnIndex+maxIndex],
  189. array2D[rowIndex+1][columnIndex],
  190. array2D[rowIndex+1][columnIndex+1]
  191. ];
  192. } else {
  193. neighbors = [
  194. array2D[rowIndex-1][columnIndex-1],
  195. array2D[rowIndex-1][columnIndex],
  196. array2D[rowIndex-1][columnIndex+1],
  197. array2D[rowIndex][columnIndex-1],
  198. array2D[rowIndex][columnIndex+1],
  199. array2D[rowIndex+1][columnIndex-1],
  200. array2D[rowIndex+1][columnIndex],
  201. array2D[rowIndex+1][columnIndex+1]
  202. ];
  203. };
  204.  
  205. return neighbors;
  206. };
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement