Explorar el Código

finished commit

Joshua Chung hace 6 años
padre
commit
31df0319a2

+ 70
- 71
.idea/workspace.xml Ver fichero

@@ -2,7 +2,6 @@
2 2
 <project version="4">
3 3
   <component name="ChangeListManager">
4 4
     <list default="true" id="e282cee1-bfe9-42a3-9794-9633eb4654d1" name="Default" comment="">
5
-      <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
6 5
       <change beforePath="$PROJECT_DIR$/src/main/java/com/zipcodeconway/ConwayGameOfLife.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/zipcodeconway/ConwayGameOfLife.java" afterDir="false" />
7 6
       <change beforePath="$PROJECT_DIR$/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java" afterDir="false" />
8 7
     </list>
@@ -21,8 +20,8 @@
21 20
           <file leaf-file-name="SimpleWindow.java" pinned="false" current-in-tab="false">
22 21
             <entry file="file://$PROJECT_DIR$/src/main/java/com/zipcodeconway/SimpleWindow.java">
23 22
               <provider selected="true" editor-type-id="text-editor">
24
-                <state relative-caret-position="255">
25
-                  <caret line="17" column="42" selection-start-line="17" selection-start-column="42" selection-end-line="17" selection-end-column="42" />
23
+                <state relative-caret-position="555">
24
+                  <caret line="37" selection-start-line="37" selection-end-line="37" />
26 25
                   <folding>
27 26
                     <element signature="imports" expanded="true" />
28 27
                   </folding>
@@ -33,17 +32,17 @@
33 32
           <file leaf-file-name="ConwayGameOfLife.java" pinned="false" current-in-tab="true">
34 33
             <entry file="file://$PROJECT_DIR$/src/main/java/com/zipcodeconway/ConwayGameOfLife.java">
35 34
               <provider selected="true" editor-type-id="text-editor">
36
-                <state relative-caret-position="90">
37
-                  <caret line="6" column="28" selection-start-line="6" selection-start-column="28" selection-end-line="6" selection-end-column="28" />
35
+                <state relative-caret-position="389">
36
+                  <caret line="54" lean-forward="true" selection-start-line="54" selection-end-line="54" />
38 37
                   <folding>
39
-                    <element signature="e#862#863#0" expanded="true" />
40
-                    <element signature="e#914#915#0" expanded="true" />
41
-                    <element signature="e#3134#3135#0" expanded="true" />
42
-                    <element signature="e#3167#3168#0" expanded="true" />
43
-                    <element signature="e#3211#3212#0" expanded="true" />
44
-                    <element signature="e#3243#3244#0" expanded="true" />
45
-                    <element signature="e#3285#3286#0" expanded="true" />
46
-                    <element signature="e#3317#3318#0" expanded="true" />
38
+                    <element signature="e#803#804#0" expanded="true" />
39
+                    <element signature="e#858#859#0" expanded="true" />
40
+                    <element signature="e#2720#2721#0" expanded="true" />
41
+                    <element signature="e#2753#2754#0" expanded="true" />
42
+                    <element signature="e#2797#2798#0" expanded="true" />
43
+                    <element signature="e#2829#2830#0" expanded="true" />
44
+                    <element signature="e#2871#2872#0" expanded="true" />
45
+                    <element signature="e#2903#2904#0" expanded="true" />
47 46
                   </folding>
48 47
                 </state>
49 48
               </provider>
@@ -56,8 +55,8 @@
56 55
           <file leaf-file-name="ConwayGameOfLifeTest.java" pinned="false" current-in-tab="true">
57 56
             <entry file="file://$PROJECT_DIR$/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java">
58 57
               <provider selected="true" editor-type-id="text-editor">
59
-                <state relative-caret-position="165">
60
-                  <caret line="15" column="24" lean-forward="true" selection-start-line="15" selection-start-column="24" selection-end-line="15" selection-end-column="24" />
58
+                <state relative-caret-position="575">
59
+                  <caret line="52" column="27" lean-forward="true" selection-start-line="52" selection-start-column="27" selection-end-line="52" selection-end-column="27" />
61 60
                 </state>
62 61
               </provider>
63 62
             </entry>
@@ -100,8 +99,8 @@
100 99
         <option value="$PROJECT_DIR$/src/main/java/Life.java" />
101 100
         <option value="$PROJECT_DIR$/src/main/java/com/zipcodeconway/SimpleWindow.java" />
102 101
         <option value="$PROJECT_DIR$/src/main/java/com/zipcodeconway/README.md" />
103
-        <option value="$PROJECT_DIR$/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java" />
104 102
         <option value="$PROJECT_DIR$/src/main/java/com/zipcodeconway/ConwayGameOfLife.java" />
103
+        <option value="$PROJECT_DIR$/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java" />
105 104
       </list>
106 105
     </option>
107 106
   </component>
@@ -232,7 +231,7 @@
232 231
       </list>
233 232
     </option>
234 233
   </component>
235
-  <component name="RunManager" selected="JUnit.ConwayGameOfLifeTest">
234
+  <component name="RunManager" selected="JUnit.ConwayGameOfLifeTest.randomTest">
236 235
     <configuration default="true" type="Applet" factoryName="Applet">
237 236
       <option name="POLICY_FILE" value="$APPLICATION_HOME_DIR$/bin/appletviewer.policy" />
238 237
     </configuration>
@@ -273,7 +272,7 @@
273 272
       </option>
274 273
       <patterns />
275 274
     </configuration>
276
-    <configuration name="ConwayGameOfLifeTest.runTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
275
+    <configuration name="ConwayGameOfLifeTest.randomTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
277 276
       <module name="Game-Of-Life-Java" />
278 277
       <extension name="coverage">
279 278
         <pattern>
@@ -285,7 +284,7 @@
285 284
       <option name="ALTERNATIVE_JRE_PATH" />
286 285
       <option name="PACKAGE_NAME" value="com.zipcodeconway" />
287 286
       <option name="MAIN_CLASS_NAME" value="com.zipcodeconway.ConwayGameOfLifeTest" />
288
-      <option name="METHOD_NAME" value="runTest" />
287
+      <option name="METHOD_NAME" value="randomTest" />
289 288
       <option name="TEST_OBJECT" value="method" />
290 289
       <option name="VM_PARAMETERS" value="-ea" />
291 290
       <option name="PARAMETERS" />
@@ -405,18 +404,18 @@
405 404
     <list>
406 405
       <item itemvalue="Maven.ConwayLife" />
407 406
       <item itemvalue="Application.ConwayGameOfLife" />
408
-      <item itemvalue="JUnit.ConwayGameOfLifeTest.runTest" />
409 407
       <item itemvalue="JUnit.ConwayGameOfLifeTest.runTest2" />
410 408
       <item itemvalue="JUnit.ConwayGameOfLifeTest" />
411 409
       <item itemvalue="JUnit.ConwayGameOfLifeTest.runTest1" />
410
+      <item itemvalue="JUnit.ConwayGameOfLifeTest.randomTest" />
412 411
     </list>
413 412
     <recent_temporary>
414 413
       <list>
414
+        <item itemvalue="JUnit.ConwayGameOfLifeTest.randomTest" />
415 415
         <item itemvalue="JUnit.ConwayGameOfLifeTest" />
416 416
         <item itemvalue="JUnit.ConwayGameOfLifeTest.runTest1" />
417 417
         <item itemvalue="JUnit.ConwayGameOfLifeTest.runTest2" />
418 418
         <item itemvalue="Application.ConwayGameOfLife" />
419
-        <item itemvalue="JUnit.ConwayGameOfLifeTest.runTest" />
420 419
       </list>
421 420
     </recent_temporary>
422 421
   </component>
@@ -431,44 +430,44 @@
431 430
       <option name="presentableId" value="Default" />
432 431
       <updated>1519668901598</updated>
433 432
       <workItem from="1519668902904" duration="14517000" />
434
-      <workItem from="1529872758253" duration="11170000" />
433
+      <workItem from="1529872758253" duration="12387000" />
435 434
     </task>
436 435
     <servers />
437 436
   </component>
438 437
   <component name="TestHistory">
439
-    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.06.24 at 19h 41m 42s.xml">
440
-      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
438
+    <history-entry file="ConwayGameOfLifeTest - 2018.06.24 at 20h 10m 59s.xml">
439
+      <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
441 440
     </history-entry>
442
-    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.06.24 at 19h 44m 04s.xml">
443
-      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
441
+    <history-entry file="ConwayGameOfLifeTest - 2018.06.24 at 20h 11m 11s.xml">
442
+      <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
444 443
     </history-entry>
445
-    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.06.24 at 19h 48m 24s.xml">
446
-      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
444
+    <history-entry file="ConwayGameOfLifeTest - 2018.06.24 at 20h 11m 29s.xml">
445
+      <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
447 446
     </history-entry>
448
-    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.06.24 at 19h 50m 04s.xml">
449
-      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
447
+    <history-entry file="ConwayGameOfLifeTest - 2018.06.24 at 20h 12m 22s.xml">
448
+      <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
450 449
     </history-entry>
451
-    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.06.24 at 19h 52m 16s.xml">
452
-      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
450
+    <history-entry file="ConwayGameOfLifeTest - 2018.06.24 at 20h 13m 42s.xml">
451
+      <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
453 452
     </history-entry>
454
-    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.06.24 at 19h 52m 49s.xml">
455
-      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
453
+    <history-entry file="ConwayGameOfLifeTest - 2018.06.24 at 20h 14m 32s.xml">
454
+      <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
456 455
     </history-entry>
457
-    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.06.24 at 19h 55m 01s.xml">
458
-      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
456
+    <history-entry file="ConwayGameOfLifeTest - 2018.06.24 at 20h 14m 44s.xml">
457
+      <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
459 458
     </history-entry>
460
-    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.06.24 at 19h 55m 28s.xml">
461
-      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
459
+    <history-entry file="ConwayGameOfLifeTest - 2018.06.24 at 20h 15m 33s.xml">
460
+      <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
462 461
     </history-entry>
463
-    <history-entry file="ConwayGameOfLifeTest_runTest1 - 2018.06.24 at 19h 57m 09s.xml">
464
-      <configuration name="ConwayGameOfLifeTest.runTest1" configurationId="JUnit" />
462
+    <history-entry file="ConwayGameOfLifeTest - 2018.06.24 at 20h 17m 19s.xml">
463
+      <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
465 464
     </history-entry>
466
-    <history-entry file="ConwayGameOfLifeTest - 2018.06.24 at 19h 57m 13s.xml">
465
+    <history-entry file="ConwayGameOfLifeTest - 2018.06.24 at 20h 18m 40s.xml">
467 466
       <configuration name="ConwayGameOfLifeTest" configurationId="JUnit" />
468 467
     </history-entry>
469 468
   </component>
470 469
   <component name="TimeTrackingManager">
471
-    <option name="totallyTimeSpent" value="25687000" />
470
+    <option name="totallyTimeSpent" value="26904000" />
472 471
   </component>
473 472
   <component name="ToolWindowManager">
474 473
     <frame x="-306" y="-1080" width="1920" height="1080" extended-state="0" />
@@ -483,7 +482,7 @@
483 482
       <window_info anchor="bottom" id="Event Log" order="7" side_tool="true" />
484 483
       <window_info anchor="right" id="Maven Projects" order="3" />
485 484
       <window_info anchor="bottom" id="Database Changes" show_stripe_button="false" />
486
-      <window_info anchor="bottom" id="Run" order="2" weight="0.3299389" />
485
+      <window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.22912423" />
487 486
       <window_info anchor="bottom" id="Version Control" order="7" />
488 487
       <window_info anchor="bottom" id="Terminal" order="7" weight="0.32970026" />
489 488
       <window_info id="Capture Tool" order="2" />
@@ -512,7 +511,7 @@
512 511
   </component>
513 512
   <component name="XDebuggerManager">
514 513
     <breakpoint-manager>
515
-      <option name="time" value="52" />
514
+      <option name="time" value="53" />
516 515
     </breakpoint-manager>
517 516
   </component>
518 517
   <component name="editorHistoryManager">
@@ -531,14 +530,14 @@
531 530
         <state relative-caret-position="300">
532 531
           <caret line="20" selection-start-line="20" selection-end-line="20" />
533 532
           <folding>
534
-            <element signature="e#862#863#0" expanded="true" />
535
-            <element signature="e#914#915#0" expanded="true" />
536
-            <element signature="e#3134#3135#0" expanded="true" />
537
-            <element signature="e#3167#3168#0" expanded="true" />
538
-            <element signature="e#3211#3212#0" expanded="true" />
539
-            <element signature="e#3243#3244#0" expanded="true" />
540
-            <element signature="e#3285#3286#0" expanded="true" />
541
-            <element signature="e#3317#3318#0" expanded="true" />
533
+            <element signature="e#803#804#0" expanded="true" />
534
+            <element signature="e#858#859#0" expanded="true" />
535
+            <element signature="e#2720#2721#0" expanded="true" />
536
+            <element signature="e#2753#2754#0" expanded="true" />
537
+            <element signature="e#2797#2798#0" expanded="true" />
538
+            <element signature="e#2829#2830#0" expanded="true" />
539
+            <element signature="e#2871#2872#0" expanded="true" />
540
+            <element signature="e#2903#2904#0" expanded="true" />
542 541
           </folding>
543 542
         </state>
544 543
       </provider>
@@ -584,17 +583,10 @@
584 583
     <entry file="file://$USER_HOME$/Downloads/ConwayLifeRandomat50.png" />
585 584
     <entry file="file://$USER_HOME$/Documents/gitea/gitea/custom/public/img/ConwayLifeUML.png" />
586 585
     <entry file="file://$PROJECT_DIR$/src/main/java/GameofLife.java" />
587
-    <entry file="file://$PROJECT_DIR$/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java">
588
-      <provider selected="true" editor-type-id="text-editor">
589
-        <state relative-caret-position="165">
590
-          <caret line="15" column="24" lean-forward="true" selection-start-line="15" selection-start-column="24" selection-end-line="15" selection-end-column="24" />
591
-        </state>
592
-      </provider>
593
-    </entry>
594 586
     <entry file="file://$PROJECT_DIR$/src/main/java/com/zipcodeconway/SimpleWindow.java">
595 587
       <provider selected="true" editor-type-id="text-editor">
596
-        <state relative-caret-position="255">
597
-          <caret line="17" column="42" selection-start-line="17" selection-start-column="42" selection-end-line="17" selection-end-column="42" />
588
+        <state relative-caret-position="555">
589
+          <caret line="37" selection-start-line="37" selection-end-line="37" />
598 590
           <folding>
599 591
             <element signature="imports" expanded="true" />
600 592
           </folding>
@@ -603,21 +595,28 @@
603 595
     </entry>
604 596
     <entry file="file://$PROJECT_DIR$/src/main/java/com/zipcodeconway/ConwayGameOfLife.java">
605 597
       <provider selected="true" editor-type-id="text-editor">
606
-        <state relative-caret-position="90">
607
-          <caret line="6" column="28" selection-start-line="6" selection-start-column="28" selection-end-line="6" selection-end-column="28" />
598
+        <state relative-caret-position="389">
599
+          <caret line="54" lean-forward="true" selection-start-line="54" selection-end-line="54" />
608 600
           <folding>
609
-            <element signature="e#862#863#0" expanded="true" />
610
-            <element signature="e#914#915#0" expanded="true" />
611
-            <element signature="e#3134#3135#0" expanded="true" />
612
-            <element signature="e#3167#3168#0" expanded="true" />
613
-            <element signature="e#3211#3212#0" expanded="true" />
614
-            <element signature="e#3243#3244#0" expanded="true" />
615
-            <element signature="e#3285#3286#0" expanded="true" />
616
-            <element signature="e#3317#3318#0" expanded="true" />
601
+            <element signature="e#803#804#0" expanded="true" />
602
+            <element signature="e#858#859#0" expanded="true" />
603
+            <element signature="e#2720#2721#0" expanded="true" />
604
+            <element signature="e#2753#2754#0" expanded="true" />
605
+            <element signature="e#2797#2798#0" expanded="true" />
606
+            <element signature="e#2829#2830#0" expanded="true" />
607
+            <element signature="e#2871#2872#0" expanded="true" />
608
+            <element signature="e#2903#2904#0" expanded="true" />
617 609
           </folding>
618 610
         </state>
619 611
       </provider>
620 612
     </entry>
613
+    <entry file="file://$PROJECT_DIR$/src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java">
614
+      <provider selected="true" editor-type-id="text-editor">
615
+        <state relative-caret-position="575">
616
+          <caret line="52" column="27" lean-forward="true" selection-start-line="52" selection-start-column="27" selection-end-line="52" selection-end-column="27" />
617
+        </state>
618
+      </provider>
619
+    </entry>
621 620
   </component>
622 621
   <component name="masterDetails">
623 622
     <states>

+ 22
- 22
src/main/java/com/zipcodeconway/ConwayGameOfLife.java Ver fichero

@@ -1,6 +1,5 @@
1 1
 package com.zipcodeconway;
2 2
 
3
-import java.util.Arrays;
4 3
 
5 4
 
6 5
 /**
@@ -11,33 +10,45 @@ public class ConwayGameOfLife {
11 10
     private int[][] world;
12 11
     private int[][] nextWorld;
13 12
     private int dimension;
13
+    private SimpleWindow display;
14 14
 
15 15
     public ConwayGameOfLife(Integer dimension) {
16
-        world = createRandomStart(dimension);
17 16
         this.dimension = dimension;
17
+        world = starterWorld();
18
+        nextWorld = new int[dimension][dimension];
19
+        display = new SimpleWindow(dimension);
18 20
      }
19 21
 
20 22
     public ConwayGameOfLife(Integer dimension, int[][] startMatrix) {
21 23
         world = startMatrix;
22
-        nextWorld = new int[dimension][dimension];
23 24
         this.dimension = dimension;
25
+        nextWorld = new int[dimension][dimension];
26
+        display = new SimpleWindow(dimension);
24 27
     }
25 28
 
26
-    public static void main(String[] args) {
27
-//        ConwayGameOfLife sim = new ConwayGameOfLife(50);
29
+    public int[][] starterWorld() {
30
+        int[][] starterWorld = new int[dimension][dimension];
31
+        for (int i = 0; i < dimension; i++) {
32
+            for (int j = 0; j < dimension; j++) {
33
+                starterWorld[i][j] = randomBinary();
34
+            }
35
+        }
36
+        return starterWorld;
28 37
     }
29 38
 
30
-    // Contains the logic for the starting scenario.
31
-    // Which cells are alive or dead in generation 0.
32
-    // allocates and returns the starting matrix of size 'dimension'
33
-    private int[][] createRandomStart(Integer dimension) {
34
-        return new int[dimension][dimension];
39
+    public int randomBinary() {
40
+        return (int)Math.round((Math.random()));
41
+    }
42
+
43
+    public static void main(String[] args) {
35 44
     }
36 45
 
37 46
     public int[][] simulate(Integer maxGenerations) {
38 47
         for (int i = 0; i <= maxGenerations; i++) {
48
+            this.display.display(world, i);
39 49
             gen();
40 50
             copyAndZeroOut();
51
+            this.display.sleep(500);
41 52
         }
42 53
         return world;
43 54
     }
@@ -50,21 +61,11 @@ public class ConwayGameOfLife {
50 61
         }
51 62
     }
52 63
 
53
-    // copy the values of 'next' matrix to 'current' matrix,
54
-    // and then zero out the contents of 'next' matrix
55 64
     public void copyAndZeroOut() {
56 65
         world = nextWorld;
57 66
         nextWorld = new int[dimension][dimension];
58 67
     }
59 68
 
60
-    // Calculate if an individual cell should be alive in the next generation.
61
-    // Based on the game logic:
62
-	/*
63
-		Any live cell with fewer than two live neighbours dies, as if by needs caused by underpopulation.
64
-		Any live cell with more than three live neighbours dies, as if by overcrowding.
65
-		Any live cell with two or three live neighbours lives, unchanged, to the next generation.
66
-		Any dead cell with exactly three live neighbours cells will come to life.
67
-	*/
68 69
     private int isAlive(int row, int col) {
69 70
         int count = countNeighbors(row, col);
70 71
         boolean alive;
@@ -76,7 +77,7 @@ public class ConwayGameOfLife {
76 77
         }
77 78
         return alive ? 1 : 0;
78 79
     }
79
-    //count works
80
+
80 81
     private int countNeighbors(int row, int col) {
81 82
         int count= 0;
82 83
         int column = col - 1;
@@ -99,7 +100,6 @@ public class ConwayGameOfLife {
99 100
             if (world[thisRow][x] == 1) count++;
100 101
             } catch (Exception e) {}
101 102
         }
102
-
103 103
         return count;
104 104
     }
105 105
 

+ 7
- 0
src/test/java/com/zipcodeconway/ConwayGameOfLifeTest.java Ver fichero

@@ -46,4 +46,11 @@ public class ConwayGameOfLifeTest {
46 46
         assertTrue(java.util.Arrays.deepEquals(results, expected));
47 47
     }
48 48
 
49
+    @Test
50
+    public void randomTest(){
51
+    ConwayGameOfLife sim = new ConwayGameOfLife(50);
52
+    int[][] results =  sim.simulate(50);
53
+    assertNotNull(results);
54
+    }
55
+
49 56
 }