Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 KB | None | 0 0
  1. //Implementation of Conway's Game of Life
  2. //https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
  3.  
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.Arrays;
  7. import java.lang.Integer;
  8. import java.lang.Boolean;
  9.  
  10. class GameOfLife{
  11. public static void main(String[] args){
  12. GameOfLife gameOfLife = new GameOfLife();
  13. gameOfLife.test();
  14. }
  15.  
  16. private boolean willBeAlive(boolean alive, int liveNeighborCt){
  17. return (alive && liveNeighborCt == 2) || liveNeighborCt == 3;
  18. }
  19.  
  20. private int[][] generateSignals(int[] cell){
  21. int x = cell[0];
  22. int y = cell[1];
  23. int[][] signals = {
  24. {x-1, y-1}, {x-1, y}, {x-1, y+1},
  25. {x, y-1}, {x, y+1},
  26. {x+1, y-1}, {x+1, y}, {x+1, y+1},
  27. };
  28. return signals;
  29. }
  30.  
  31. private HashMap<String, Integer> generateLiveNeighborCtMap(int[][] liveCells){
  32. HashMap<String, Integer> liveNeighborCtMap = new HashMap<String, Integer>();
  33. for(int i = 0; i < liveCells.length; i++){
  34.  
  35. String cellKey = intDuple2Str(liveCells[i]);
  36. if( !liveNeighborCtMap.containsKey( cellKey ) ){
  37. liveNeighborCtMap.put(cellKey, 0);
  38. }
  39.  
  40. int[][] signals = generateSignals(liveCells[i]);
  41. for(int j = 0; j < signals.length; j++){
  42. String signalKey = intDuple2Str(signals[j]);
  43. if( liveNeighborCtMap.containsKey(signalKey) ){
  44. liveNeighborCtMap.put(signalKey, liveNeighborCtMap.get(signalKey)+1);
  45. }
  46. else{
  47. liveNeighborCtMap.put(signalKey, 1);
  48. }
  49. }
  50. }
  51.  
  52. return liveNeighborCtMap;
  53. }
  54.  
  55. /*private int[][] calcNextState(int[][] liveCells){
  56. ArrayList<Integer[]> nextState = new ArrayList<Integer[]>();
  57. HashMap<String, Integer> liveNeighborCtMap = new HashMap<String, Integer>();
  58.  
  59. for(String cell : liveNeighborCtMap.keySet()){
  60. if(willBeAlive(cell))
  61. }
  62.  
  63. return nextState.toArray();
  64. }*/
  65.  
  66. //Helper Functions
  67. private String intDuple2Str(int[] duple){
  68. return "(" + duple[0] + "," + duple[1] + ")";
  69. }
  70.  
  71. private boolean contains(int[][] container, int[] target){
  72. for(int i = 0; i < container.length; i++){
  73. if(Arrays.equals(target, container[i])){
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79.  
  80.  
  81. //TESTING:
  82.  
  83. private HashMap<String, Boolean> getTests(){
  84. HashMap<String, Boolean> TESTS = new HashMap<String, Boolean>();
  85.  
  86. //Tests willBeAlive:
  87. TESTS.put("alive, 1 live neighbor", willBeAlive(true, 1) == false);
  88. TESTS.put("alive, 3 live neighbors", willBeAlive(true, 3) == true);
  89. TESTS.put("alive, 4 live neighbors", willBeAlive(true, 4) == false);
  90. TESTS.put("dead, 2 live neighbors", willBeAlive(false, 2) == false);
  91. TESTS.put("dead, 3 live neighbors", willBeAlive(false, 3) == true);
  92. TESTS.put("dead, 4 live neighbors", willBeAlive(false, 4) == false);
  93.  
  94. //Tests compare2dIntArrays:
  95. TESTS.put("Array Compare Equal",
  96. compare2dIntArrays(new int[][]{ {0, 0}, {2, 4} }, new int[][]{ {0, 0}, {2, 4} }) == true);
  97. TESTS.put("Array Compare Not Equal",
  98. compare2dIntArrays(new int[][]{ {1, 1} }, new int[][]{ {2, 2} }) == false);
  99.  
  100. //Tests generateSignals:
  101. int[][] signalsExpected;
  102. signalsExpected = new int[][]{
  103. {-1, -1}, {-1, 0}, {-1, 1},
  104. {0, -1}, {0, 1},
  105. {1, -1}, {1, 0}, {1, 1},
  106. };
  107. TESTS.put("Generate Signals for (0, 0)", compare2dIntArrays( generateSignals(new int[]{ 0, 0 }), signalsExpected ));
  108. signalsExpected = new int[][]{
  109. {4, 0}, {4, 1}, {4, 2},
  110. {5, 0}, {5, 2},
  111. {6, 0}, {6, 1}, {6, 2},
  112. };
  113. TESTS.put("Generate Signals for (5, 1)", compare2dIntArrays( generateSignals(new int[]{ 5, 1 }), signalsExpected ));
  114.  
  115. //Tests intDuple2Str:
  116. TESTS.put("duple2string (4,5)", intDuple2Str(new int[]{4, 5}).equals("(4,5)"));
  117. TESTS.put("duple2string (0,0)", intDuple2Str(new int[]{0, 0}).equals("(0,0)"));
  118.  
  119. //Tests generateLiveNeighborCtMap:
  120. HashMap<String, Integer> mapExpected = new HashMap<String, Integer>();
  121. TESTS.put("Live Neighbor Count Map for Empty Set", generateLiveNeighborCtMap(new int[][]{}).equals(mapExpected));
  122. mapExpected.put("(1,1)", 1); mapExpected.put("(0,1)", 1);
  123. mapExpected.put("(1,0)", 1); mapExpected.put("(0,0)", 0);
  124. mapExpected.put("(-1,1)", 1); mapExpected.put("(-1,0)", 1);
  125. mapExpected.put("(0,-1)", 1); mapExpected.put("(1,-1)", 1);
  126. mapExpected.put("(-1,-1)", 1);
  127. TESTS.put("Live Neighbor Count Map for One Cell", generateLiveNeighborCtMap(new int[][]{{0,0}}).equals(mapExpected));
  128. mapExpected.put("(-1,1)", 1); mapExpected.put("(-1,0)", 1);
  129. mapExpected.put("(1,-1)", 1); mapExpected.put("(-1,-1)", 1);
  130. mapExpected.put("(1,1)", 1); mapExpected.put("(0,1)", 1);
  131. mapExpected.put("(1,0)", 1); mapExpected.put("(0,0)", 0);
  132. mapExpected.put("(4,6)", 1); mapExpected.put("(5,4)", 1);
  133. mapExpected.put("(0,-1)", 1); mapExpected.put("(6,6)", 1);
  134. mapExpected.put("(4,4)", 1); mapExpected.put("(5,6)", 1);
  135. mapExpected.put("(6,5)", 1); mapExpected.put("(5,5)", 0);
  136. mapExpected.put("(4,5)", 1); mapExpected.put("(6,4)", 1);
  137. TESTS.put("Live Neighbor Count Map for Two Far Cells", generateLiveNeighborCtMap(new int[][]{{0,0},{5,5}}).equals(mapExpected));
  138. mapExpected.clear();
  139. mapExpected.put("(2,0)", 1); mapExpected.put("(1,1)", 2);
  140. mapExpected.put("(2,1)", 1); mapExpected.put("(2,-1)", 1);
  141. mapExpected.put("(0,1)", 2); mapExpected.put("(1,0)", 1);
  142. mapExpected.put("(0,0)", 1); mapExpected.put("(-1,1)", 1);
  143. mapExpected.put("(-1,0)", 1); mapExpected.put("(0,-1)", 2);
  144. mapExpected.put("(1,-1)", 2); mapExpected.put("(-1,-1)", 1);
  145. TESTS.put("Live Neighbor Count Map for Two Adjacent Cells", generateLiveNeighborCtMap(new int[][]{{0,0},{1,0}}).equals(mapExpected));
  146.  
  147. /*int[][] neighborCtMap = {{0,0}};
  148. printLiveNeighborCtMapCode(
  149. generateLiveNeighborCtMap( new int[][]{{0,0}} )
  150. );*/
  151.  
  152. //Tests contains:
  153. TESTS.put("[[2, 1], [3]] contains [2, 1]", contains(new int[][]{ {2, 1}, {3} }, new int[]{2, 1}) == true);
  154. TESTS.put("[[4], [3, 2]] contains [4]", contains(new int[][]{ {4}, {3, 2} }, new int[]{4}) == true);
  155. //Tests calcNextState:
  156.  
  157. return TESTS;
  158. }
  159.  
  160. //Helper Test Functions
  161. private void printTests(HashMap<String, Boolean> TESTS){
  162. System.out.println("STAT - TEST:");
  163. System.out.println("------------");
  164. for(String test : TESTS.keySet()){
  165. System.out.println((TESTS.get(test) ? "PASS" : "FAIL") + " - " + test);
  166. }
  167. }
  168.  
  169. private void test(){
  170. printTests(getTests());
  171. }
  172.  
  173. private boolean compare2dIntArrays(int[][] a1, int[][] a2){
  174. if(a1 == null) return a2 == null;
  175. if(a2 == null) return false;
  176. if(a1.length != a2.length) return false;
  177.  
  178. for(int i = 0; i < a1.length; i++){
  179. if(!Arrays.equals(a1[i], a2[i])){
  180. return false;
  181. }
  182. }
  183.  
  184. return true;
  185. }
  186.  
  187. public void printLiveNeighborCtMapCode(HashMap<String, Integer> liveNeighborCtMap){
  188. for(String cell : liveNeighborCtMap.keySet()){
  189. System.out.println( "mapExpected.put(\"" + cell + "\", " + liveNeighborCtMap.get(cell) + ");");
  190. }
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement