Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

# =========================
# Operating System Files
# =========================

# OSX
# =========================

.DS_Store
.AppleDouble
.LSOverride

# Thumbnails
._*

# Files that might appear on external disk
.Spotlight-V100
.Trashes

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

\.metadata
\.recommenders
*.class
8 changes: 8 additions & 0 deletions com/laboon/GameOfLife/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Mockito"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions com/laboon/GameOfLife/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>GameOfLife</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions com/laboon/GameOfLife/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
2 changes: 0 additions & 2 deletions com/laboon/Cell.java → com/laboon/GameOfLife/src/Cell.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package com.laboon;

public class Cell {

private State _state = State.DEAD;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package com.laboon;

public class JavaLife {

/**
Expand All @@ -11,6 +9,8 @@ public class JavaLife {
*/

public JavaLife(int size, int seed, int percent, int maxIterations) {

long startTime = System.nanoTime();
World w = new World(size, seed, percent);
System.out.println("Initial Configuration:");
System.out.println(w.toString());
Expand All @@ -20,6 +20,10 @@ public JavaLife(int size, int seed, int percent, int maxIterations) {
System.out.println(w.toString());
}

long endTime = System.nanoTime();
long totalTime = endTime-startTime;

System.out.println("Total time to execute " + maxIterations + " iterations: " + totalTime/1000000000.0 + " seconds");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package com.laboon;

/**
* Possible states of a cell, either ALIVE or DEAD
*/
Expand Down
16 changes: 7 additions & 9 deletions com/laboon/World.java → com/laboon/GameOfLife/src/World.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package com.laboon;

import java.util.Random;

public class World {
Expand Down Expand Up @@ -115,19 +113,19 @@ public World iterate() {
*/

public String toString() {
String toReturn = " ";
StringBuilder toReturn = new StringBuilder(" ");
for (int j= 0; j < _size; j++) {
toReturn += String.valueOf(j % 10);
toReturn.append(String.valueOf(j % 10));
}
toReturn += "\n";
toReturn.append("\n");
for (int j = 0; j < _size; j++ ) {
toReturn += String.valueOf(j % 10) + " ";
toReturn.append(String.valueOf(j % 10) + " ");
for (int k = 0; k < _size; k++) {
toReturn += (_world[j][k].getStateRep());
toReturn.append((_world[j][k].getStateRep()));
}
toReturn += "\n";
toReturn.append("\n");
}
return toReturn;
return toReturn.toString();
}

/**
Expand Down
127 changes: 127 additions & 0 deletions com/laboon/GameOfLife/src/WorldTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import static org.junit.Assert.*;

import org.junit.*;

public class WorldTest {


/*
* Tests a specific seed. Both before and after the modification, inputting the same size, seed, and percentage
* should result in the same thing.
*/
@Test
public void testSeed(){
World testWorld = new World(10, 5, 50);

String result = testWorld.toString();
// System.out.println


assertEquals(result, " 0123456789\n"
+ "0 ...XXX..XX\n"
+ "1 XXX.X..XXX\n"
+ "2 X.X..X.XXX\n"
+ "3 ...XXXXX.X\n"
+ "4 ....X..X..\n"
+ "5 XXXXXX.XXX\n"
+ "6 ..XX..X.XX\n"
+ "7 XX.X....X.\n"
+ "8 .......X.X\n"
+ "9 XXXXX.X...\n");

}


/*
* This tests that the number of X's + number of .'s adds up to n*n, where n is the size of the board. It does
* this by grabbing these numbers from the String generated by the World toString method, and so should provide
* another level of certainty that the method is working properly.
*/
@Test
public void testAliveAndDeadTotals(){

int worldSize = 20;
World largeWorld = new World(worldSize, 5, 50);
String result = largeWorld.toString();

int numX = 0;
int numPeriod = 0;
for (int i=0; i<result.length(); i++){
if (result.charAt(i) == 'X'){
numX++;
}
else if (result.charAt(i) == '.'){
numPeriod++;
}
}

assertEquals(worldSize*worldSize, numX+numPeriod);
}

/*
* Tests whether the output of a size zero world is as we expect: two spaces and a newline
*/
@Test
public void testSizeZeroWorld(){
int worldSize = 0;
World largeWorld = new World(worldSize, 5, 50);
assertEquals(largeWorld.toString(), " \n");
}

/*
* Tests whether trying a negative world size can even reach the toString() statement
*/
@Test
public void testNegativeSizeWorld(){
int worldSize = -1;
World largeWorld = null;
try{
largeWorld = new World(worldSize, 5, 50);
assertEquals(largeWorld.toString(), "YOU SHOULD NOT BE HERE");
System.out.println("toString reached, failing test");
fail();
}
catch(Exception e){
assertEquals("java.lang.NegativeArraySizeException", e.toString());
}

}
/*
* Tests that a world with all alive cells has all X's in its toString matrix
*/
@Test
public void testAliveWorld(){
int worldSize = 20;
World largeWorld = new World(worldSize, 5, 100);
String result = largeWorld.toString();

int numX = 0;
for (int i=0; i<result.length(); i++){
if (result.charAt(i) == 'X'){
numX++;
}
}

assertEquals(worldSize*worldSize, numX);
}

/*
* Tests that a world with all dead cells has all .'s in its toString matrix
*/
@Test
public void testDeadWorld(){
int worldSize = 20;
World largeWorld = new World(worldSize, 5, 0);
String result = largeWorld.toString();

int numPeriod = 0;
for (int i=0; i<result.length(); i++){
if (result.charAt(i) == '.'){
numPeriod++;
}
}
assertEquals(worldSize*worldSize, numPeriod);
}


}