Daniel Horowitz vor 6 Jahren
Ursprung
Commit
fcceb55d5c
2 geänderte Dateien mit 157 neuen und 60 gelöschten Zeilen
  1. 131
    33
      .idea/workspace.xml
  2. 26
    27
      src/main/java/com/zipcodeconway/ConwayGameOfLife.java

+ 131
- 33
.idea/workspace.xml Datei anzeigen

@@ -2,15 +2,7 @@
2 2
 <project version="4">
3 3
   <component name="ChangeListManager">
4 4
     <list default="true" id="a87c0587-ce49-4fef-855f-6e0ade4512ae" name="Default" comment="">
5
-      <change beforePath="" afterPath="$PROJECT_DIR$/Game-Of-Life-Java.iml" />
6
-      <change beforePath="$PROJECT_DIR$/.idea/Game-Of-Life-Java.iml" afterPath="" />
7
-      <change beforePath="$PROJECT_DIR$/.idea/misc.xml" afterPath="$PROJECT_DIR$/.idea/misc.xml" />
8
-      <change beforePath="$PROJECT_DIR$/.idea/modules.xml" afterPath="$PROJECT_DIR$/.idea/modules.xml" />
9
-      <change beforePath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterPath="" />
10
-      <change beforePath="$PROJECT_DIR$/.idea/vcs.xml" afterPath="$PROJECT_DIR$/.idea/vcs.xml" />
11
-      <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
12 5
       <change beforePath="$PROJECT_DIR$/src/main/java/com/zipcodeconway/ConwayGameOfLife.java" afterPath="$PROJECT_DIR$/src/main/java/com/zipcodeconway/ConwayGameOfLife.java" />
13
-      <change beforePath="$PROJECT_DIR$/src/main/java/com/zipcodeconway/SimpleWindow.java" afterPath="$PROJECT_DIR$/src/main/java/com/zipcodeconway/SimpleWindow.java" />
14 6
     </list>
15 7
     <ignored path="$PROJECT_DIR$/target/" />
16 8
     <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@@ -32,12 +24,14 @@
32 24
           </provider>
33 25
         </entry>
34 26
       </file>
35
-      <file leaf-file-name="ConwayGameOfLife.java" pinned="false" current-in-tab="false">
27
+      <file leaf-file-name="ConwayGameOfLife.java" pinned="false" current-in-tab="true">
36 28
         <entry file="file://$PROJECT_DIR$/src/main/java/com/zipcodeconway/ConwayGameOfLife.java">
37 29
           <provider selected="true" editor-type-id="text-editor">
38
-            <state relative-caret-position="293">
39
-              <caret line="59" column="54" lean-forward="true" selection-start-line="59" selection-start-column="54" selection-end-line="59" selection-end-column="54" />
40
-              <folding />
30
+            <state relative-caret-position="1660">
31
+              <caret line="93" column="9" lean-forward="true" selection-start-line="93" selection-start-column="9" selection-end-line="93" selection-end-column="9" />
32
+              <folding>
33
+                <element signature="method#main#0;class#ConwayGameOfLife#0" expanded="false" />
34
+              </folding>
41 35
             </state>
42 36
           </provider>
43 37
         </entry>
@@ -55,11 +49,11 @@
55 49
           </provider>
56 50
         </entry>
57 51
       </file>
58
-      <file leaf-file-name="ConwayGameOfLifeTest.java" pinned="false" current-in-tab="true">
52
+      <file leaf-file-name="ConwayGameOfLifeTest.java" pinned="false" current-in-tab="false">
59 53
         <entry file="file://$PROJECT_DIR$/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java">
60 54
           <provider selected="true" editor-type-id="text-editor">
61
-            <state relative-caret-position="100">
62
-              <caret line="6" column="13" lean-forward="false" selection-start-line="6" selection-start-column="13" selection-end-line="6" selection-end-column="13" />
55
+            <state relative-caret-position="550">
56
+              <caret line="24" column="0" lean-forward="false" selection-start-line="24" selection-start-column="0" selection-end-line="24" selection-end-column="0" />
63 57
               <folding />
64 58
             </state>
65 59
           </provider>
@@ -212,7 +206,7 @@
212 206
       </list>
213 207
     </option>
214 208
   </component>
215
-  <component name="RunManager">
209
+  <component name="RunManager" selected="Application.ConwayGameOfLife">
216 210
     <configuration default="true" type="Applet" factoryName="Applet">
217 211
       <option name="HTML_USED" value="false" />
218 212
       <option name="WIDTH" value="400" />
@@ -220,6 +214,25 @@
220 214
       <option name="POLICY_FILE" value="$APPLICATION_HOME_DIR$/bin/appletviewer.policy" />
221 215
       <module />
222 216
     </configuration>
217
+    <configuration name="ConwayGameOfLife" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
218
+      <extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea">
219
+        <pattern>
220
+          <option name="PATTERN" value="com.zipcodeconway.*" />
221
+          <option name="ENABLED" value="true" />
222
+        </pattern>
223
+      </extension>
224
+      <option name="MAIN_CLASS_NAME" value="com.zipcodeconway.ConwayGameOfLife" />
225
+      <option name="VM_PARAMETERS" />
226
+      <option name="PROGRAM_PARAMETERS" />
227
+      <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
228
+      <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
229
+      <option name="ALTERNATIVE_JRE_PATH" />
230
+      <option name="ENABLE_SWING_INSPECTOR" value="false" />
231
+      <option name="ENV_VARIABLES" />
232
+      <option name="PASS_PARENT_ENVS" value="true" />
233
+      <module name="Game-Of-Life-Java" />
234
+      <envs />
235
+    </configuration>
223 236
     <configuration default="true" type="Application" factoryName="Application">
224 237
       <extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
225 238
       <option name="MAIN_CLASS_NAME" />
@@ -259,6 +272,56 @@
259 272
       <envs />
260 273
       <patterns />
261 274
     </configuration>
275
+    <configuration name="ConwayGameOfLifeTest.runTest1" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
276
+      <extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea">
277
+        <pattern>
278
+          <option name="PATTERN" value="com.zipcodeconway.*" />
279
+          <option name="ENABLED" value="true" />
280
+        </pattern>
281
+      </extension>
282
+      <module name="Game-Of-Life-Java" />
283
+      <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
284
+      <option name="ALTERNATIVE_JRE_PATH" />
285
+      <option name="PACKAGE_NAME" value="com.zipcodeconway" />
286
+      <option name="MAIN_CLASS_NAME" value="com.zipcodeconway.ConwayGameOfLifeTest" />
287
+      <option name="METHOD_NAME" value="runTest1" />
288
+      <option name="TEST_OBJECT" value="method" />
289
+      <option name="VM_PARAMETERS" value="-ea" />
290
+      <option name="PARAMETERS" />
291
+      <option name="WORKING_DIRECTORY" value="%MODULE_WORKING_DIR%" />
292
+      <option name="ENV_VARIABLES" />
293
+      <option name="PASS_PARENT_ENVS" value="true" />
294
+      <option name="TEST_SEARCH_SCOPE">
295
+        <value defaultName="singleModule" />
296
+      </option>
297
+      <envs />
298
+      <patterns />
299
+    </configuration>
300
+    <configuration name="ConwayGameOfLifeTest.runTest2" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
301
+      <extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea">
302
+        <pattern>
303
+          <option name="PATTERN" value="com.zipcodeconway.*" />
304
+          <option name="ENABLED" value="true" />
305
+        </pattern>
306
+      </extension>
307
+      <module name="Game-Of-Life-Java" />
308
+      <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
309
+      <option name="ALTERNATIVE_JRE_PATH" />
310
+      <option name="PACKAGE_NAME" value="com.zipcodeconway" />
311
+      <option name="MAIN_CLASS_NAME" value="com.zipcodeconway.ConwayGameOfLifeTest" />
312
+      <option name="METHOD_NAME" value="runTest2" />
313
+      <option name="TEST_OBJECT" value="method" />
314
+      <option name="VM_PARAMETERS" value="-ea" />
315
+      <option name="PARAMETERS" />
316
+      <option name="WORKING_DIRECTORY" value="%MODULE_WORKING_DIR%" />
317
+      <option name="ENV_VARIABLES" />
318
+      <option name="PASS_PARENT_ENVS" value="true" />
319
+      <option name="TEST_SEARCH_SCOPE">
320
+        <value defaultName="singleModule" />
321
+      </option>
322
+      <envs />
323
+      <patterns />
324
+    </configuration>
262 325
     <configuration default="true" type="JUnit" factoryName="JUnit">
263 326
       <extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
264 327
       <module name="" />
@@ -319,9 +382,18 @@
319 382
       <option name="PROGRAM_PARAMETERS" />
320 383
       <predefined_log_file id="idea.log" enabled="true" />
321 384
     </configuration>
385
+    <list size="4">
386
+      <item index="0" class="java.lang.String" itemvalue="JUnit.ConwayGameOfLifeTest" />
387
+      <item index="1" class="java.lang.String" itemvalue="JUnit.ConwayGameOfLifeTest.runTest1" />
388
+      <item index="2" class="java.lang.String" itemvalue="JUnit.ConwayGameOfLifeTest.runTest2" />
389
+      <item index="3" class="java.lang.String" itemvalue="Application.ConwayGameOfLife" />
390
+    </list>
322 391
     <recent_temporary>
323
-      <list size="1">
324
-        <item index="0" class="java.lang.String" itemvalue="JUnit.ConwayGameOfLifeTest" />
392
+      <list size="4">
393
+        <item index="0" class="java.lang.String" itemvalue="Application.ConwayGameOfLife" />
394
+        <item index="1" class="java.lang.String" itemvalue="JUnit.ConwayGameOfLifeTest" />
395
+        <item index="2" class="java.lang.String" itemvalue="JUnit.ConwayGameOfLifeTest.runTest1" />
396
+        <item index="3" class="java.lang.String" itemvalue="JUnit.ConwayGameOfLifeTest.runTest2" />
325 397
       </list>
326 398
     </recent_temporary>
327 399
   </component>
@@ -338,20 +410,44 @@
338 410
       <option name="number" value="Default" />
339 411
       <option name="presentableId" value="Default" />
340 412
       <updated>1520876843046</updated>
341
-      <workItem from="1520876844362" duration="8081000" />
413
+      <workItem from="1520876844362" duration="9791000" />
342 414
     </task>
343 415
     <servers />
344 416
   </component>
345 417
   <component name="TestHistory">
346
-    <history-entry file="ConwayGameOfLifeTest - 2018.03.12 at 16h 07m 12s.xml">
418
+    <history-entry file="ConwayGameOfLifeTest - 2018.03.12 at 16h 17m 39s.xml">
347 419
       <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
348 420
     </history-entry>
349
-    <history-entry file="ConwayGameOfLifeTest - 2018.03.12 at 16h 09m 24s.xml">
421
+    <history-entry file="ConwayGameOfLifeTest - 2018.03.12 at 16h 24m 21s.xml">
350 422
       <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
351 423
     </history-entry>
424
+    <history-entry file="ConwayGameOfLifeTest - 2018.03.12 at 16h 25m 07s.xml">
425
+      <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
426
+    </history-entry>
427
+    <history-entry file="ConwayGameOfLifeTest - 2018.03.12 at 16h 45m 52s.xml">
428
+      <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
429
+    </history-entry>
430
+    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.03.12 at 16h 34m 01s.xml">
431
+      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
432
+    </history-entry>
433
+    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.03.12 at 16h 37m 34s.xml">
434
+      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
435
+    </history-entry>
436
+    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.03.12 at 16h 39m 28s.xml">
437
+      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
438
+    </history-entry>
439
+    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.03.12 at 16h 43m 17s.xml">
440
+      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
441
+    </history-entry>
442
+    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.03.12 at 16h 45m 44s.xml">
443
+      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
444
+    </history-entry>
445
+    <history-entry file="ConwayGameOfLifeTest_runTest2 - 2018.03.12 at 16h 34m 11s.xml">
446
+      <configuration name="ConwayGameOfLifeTest.runTest2" configurationId="JUnit" />
447
+    </history-entry>
352 448
   </component>
353 449
   <component name="TimeTrackingManager">
354
-    <option name="totallyTimeSpent" value="8081000" />
450
+    <option name="totallyTimeSpent" value="9791000" />
355 451
   </component>
356 452
   <component name="ToolWindowManager">
357 453
     <frame x="9" y="33" width="1843" height="997" extended-state="0" />
@@ -367,7 +463,7 @@
367 463
       <window_info id="Maven Projects" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
368 464
       <window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
369 465
       <window_info id="Run" active="true" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.29920363" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
370
-      <window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
466
+      <window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32992035" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
371 467
       <window_info id="Capture Tool" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
372 468
       <window_info id="Designer" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
373 469
       <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.086063296" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
@@ -408,27 +504,29 @@
408 504
         </state>
409 505
       </provider>
410 506
     </entry>
411
-    <entry file="file://$PROJECT_DIR$/src/main/java/com/zipcodeconway/SimpleWindow.java">
507
+    <entry file="file://$PROJECT_DIR$/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java">
412 508
       <provider selected="true" editor-type-id="text-editor">
413
-        <state relative-caret-position="660">
414
-          <caret line="45" column="41" lean-forward="false" selection-start-line="45" selection-start-column="41" selection-end-line="45" selection-end-column="41" />
509
+        <state relative-caret-position="550">
510
+          <caret line="24" column="0" lean-forward="false" selection-start-line="24" selection-start-column="0" selection-end-line="24" selection-end-column="0" />
415 511
           <folding />
416 512
         </state>
417 513
       </provider>
418 514
     </entry>
419
-    <entry file="file://$PROJECT_DIR$/src/main/java/com/zipcodeconway/ConwayGameOfLife.java">
515
+    <entry file="file://$PROJECT_DIR$/src/main/java/com/zipcodeconway/SimpleWindow.java">
420 516
       <provider selected="true" editor-type-id="text-editor">
421
-        <state relative-caret-position="293">
422
-          <caret line="59" column="54" lean-forward="true" selection-start-line="59" selection-start-column="54" selection-end-line="59" selection-end-column="54" />
517
+        <state relative-caret-position="660">
518
+          <caret line="45" column="41" lean-forward="false" selection-start-line="45" selection-start-column="41" selection-end-line="45" selection-end-column="41" />
423 519
           <folding />
424 520
         </state>
425 521
       </provider>
426 522
     </entry>
427
-    <entry file="file://$PROJECT_DIR$/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java">
523
+    <entry file="file://$PROJECT_DIR$/src/main/java/com/zipcodeconway/ConwayGameOfLife.java">
428 524
       <provider selected="true" editor-type-id="text-editor">
429
-        <state relative-caret-position="100">
430
-          <caret line="6" column="13" lean-forward="false" selection-start-line="6" selection-start-column="13" selection-end-line="6" selection-end-column="13" />
431
-          <folding />
525
+        <state relative-caret-position="1660">
526
+          <caret line="93" column="9" lean-forward="true" selection-start-line="93" selection-start-column="9" selection-end-line="93" selection-end-column="9" />
527
+          <folding>
528
+            <element signature="method#main#0;class#ConwayGameOfLife#0" expanded="false" />
529
+          </folding>
432 530
         </state>
433 531
       </provider>
434 532
     </entry>

+ 26
- 27
src/main/java/com/zipcodeconway/ConwayGameOfLife.java Datei anzeigen

@@ -11,8 +11,8 @@ public class ConwayGameOfLife {
11 11
     public ConwayGameOfLife(Integer dimension) {
12 12
         this.displayWindow = new SimpleWindow(dimension);
13 13
         currentGen = createRandomStart(dimension);
14
-       nextGen = new int[dimension][dimension];
15
-     }
14
+        nextGen = new int[dimension][dimension];
15
+    }
16 16
 
17 17
     public ConwayGameOfLife(Integer dimension, int[][] startmatrix) {
18 18
         this.displayWindow = new SimpleWindow(dimension);
@@ -30,12 +30,14 @@ public class ConwayGameOfLife {
30 30
     // Which cells are alive or dead in generation 0.
31 31
     // allocates and returns the starting matrix of size 'dimension'
32 32
     private int[][] createRandomStart(Integer dimension) {
33
-        for (int i = 0; i < dimension; i++) {
34
-            for (int j = 0; j < dimension; i++) {
35
-                currentGen[i][j] = (int) Math.round(Math.random());
33
+        int[][] randomStart = new int[dimension][dimension];
34
+
35
+        for (int i = 0; i < randomStart.length; i++) {
36
+            for (int j = 0; j < randomStart.length; j++) {
37
+                randomStart[i][j] = (int) Math.round(Math.random());
36 38
             }
37 39
         }
38
-        return currentGen;
40
+        return randomStart;
39 41
     }
40 42
 
41 43
     public int[][] simulate(Integer maxGenerations) {
@@ -58,9 +60,9 @@ public class ConwayGameOfLife {
58 60
 
59 61
     // copy the values of 'next' matrix to 'current' matrix,
60 62
     // and then zero out the contents of 'next' matrix
61
-    public void copyAndZeroOut(int [][] next, int[][] current) {
62
-        for(int i = 0; i < next.length; i++) {
63
-            for(int j = 0; j < next.length; j++) {
63
+    public void copyAndZeroOut(int[][] next, int[][] current) {
64
+        for (int i = 0; i < next.length; i++) {
65
+            for (int j = 0; j < next.length; j++) {
64 66
                 current[i][j] = next[i][j];
65 67
                 next[i][j] = 0;
66 68
             }
@@ -76,37 +78,34 @@ public class ConwayGameOfLife {
76 78
 		Any dead cell with exactly three live neighbours cells will come to life.
77 79
 	*/
78 80
     private int isAlive(int row, int col, int[][] world) {
79
-        int north = col -1;
80
-        int south = col +1;
81
-        int west = row -1;
82
-        int east = row +1;
81
+        int north = col - 1;
82
+        int south = col + 1;
83
+        int west = row - 1;
84
+        int east = row + 1;
83 85
 
84
-        if(north < 0) {
85
-            north = world[row].length -1;
86
+        if (north == -1) {
87
+            north = world[row].length - 1;
86 88
         }
87 89
         if (south == world[row].length) {
88 90
             south = 0;
89 91
         }
90
-        if (west < 0) {
91
-            west = world[col].length -1;
92
+        if (west == -1) {
93
+            west = world.length - 1;
92 94
         }
93
-        if (east == world[col].length) {
95
+        if (east == world.length) {
94 96
             east = 0;
95 97
         }
96 98
 
97 99
         int numOfLiveNeighbors = world[row][north] + world[east][north] + world[west][north] +
98 100
                 world[row][south] + world[east][south] + world[west][south]
99
-                + world[row][west] + world[row][east];
101
+                + world[east][col] + world[west][col];
100 102
 
101
-        if (numOfLiveNeighbors < 2 || numOfLiveNeighbors > 3)
103
+        if (numOfLiveNeighbors < 2 || numOfLiveNeighbors > 3) {
102 104
             return 0;
103
-
104
-        if (world[row][col] == 0 && numOfLiveNeighbors == 3)
105
-            return 1;
106
-
107
-        // if this cell we are looking at is alive
108
-        // and my live neighbors = 2 or three
109
-        if (world[row][col] == 1 && ((numOfLiveNeighbors == 2) || (numOfLiveNeighbors == 3)));
105
+        } else if (numOfLiveNeighbors == 3)
110 106
             return 1;
107
+        else {
108
+            return world[row][col];
109
+        }
111 110
     }
112 111
 }