Created
July 15, 2019 14:13
-
-
Save saswata-dutta/bd2bd1b567c52e0787f9855847229c26 to your computer and use it in GitHub Desktop.
Scala Game of Life - Craftsmanship Guild Mini Coderetreat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package guild.gameoflife | |
import org.scalatest.matchers.ShouldMatchers | |
import org.scalatest.prop.Checkers | |
import org.scalatest.Spec | |
import org.scalatest.junit.JUnitRunner | |
import org.junit.runner.RunWith | |
@RunWith(classOf[JUnitRunner]) | |
class GOLTest extends Spec with ShouldMatchers with Checkers { | |
describe("a Generation") { | |
case class Coordinate(x: Int, y: Int) | |
class Generation(cells: Set[Coordinate]) { | |
def livingAt(location: Coordinate):Boolean = { | |
cells.contains( location ) | |
} | |
def evolve: Generation = { | |
new Generation(cells.filter { us => | |
val neighborhood = for { | |
x <- -1 to 1 | |
y <- -1 to 1 | |
} yield Coordinate(us.x + x, us.y + y) | |
val n = (cells & neighborhood.toSet).size - 1 | |
n == 3 | |
}) | |
} | |
} | |
describe("with live cells") { | |
val cells = Set(Coordinate(0, 0)) | |
val current = new Generation(cells) | |
it("has a living cell") { | |
current.livingAt(Coordinate(0,0)) should be (true) | |
} | |
it( "kills a cell with no neighbors") { | |
val next = current.evolve | |
next.livingAt(Coordinate(0,0)) should be (false) | |
} | |
it("cells in a 2x2 block will continue to live ") { | |
val cells = Set( Coordinate(0,0), Coordinate(1,0), Coordinate(0,1), Coordinate(1,1) ) | |
val next = new Generation( cells ).evolve | |
next.livingAt( Coordinate(0,0) ) should be (true) | |
next.livingAt( Coordinate(0,1) ) should be (true) | |
next.livingAt( Coordinate(1,0) ) should be (true) | |
next.livingAt( Coordinate(1,1) ) should be (true) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>guild</groupId> | |
<artifactId>gameoflife-i1</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>gol-i1</name> | |
<properties> | |
<scala.version>2.9.0</scala.version> | |
</properties> | |
<repositories> | |
<repository> | |
<id>scala-tools.org</id> | |
<name>Scala-Tools Maven2 Repository</name> | |
<url>http://scala-tools.org/repo-releases</url> | |
</repository> | |
</repositories> | |
<pluginRepositories> | |
<pluginRepository> | |
<id>scala-tools.org</id> | |
<name>Scala-Tools Maven2 Repository</name> | |
<url>http://scala-tools.org/repo-releases</url> | |
</pluginRepository> | |
</pluginRepositories> | |
<dependencies> | |
<dependency> | |
<groupId>org.scala-lang</groupId> | |
<artifactId>scala-library</artifactId> | |
<version>${scala.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.4</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.scalatest</groupId> | |
<artifactId>scalatest_2.9.0</artifactId> | |
<version>1.4.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.scala-tools.testing</groupId> | |
<artifactId>scalacheck_2.9.0</artifactId> | |
<version>1.8</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<sourceDirectory>src/main/scala</sourceDirectory> | |
<testSourceDirectory>src/test/scala</testSourceDirectory> | |
<plugins> | |
<plugin> | |
<groupId>org.scala-tools</groupId> | |
<artifactId>maven-scala-plugin</artifactId> | |
<executions> | |
<execution> | |
<goals> | |
<goal>compile</goal> | |
<goal>testCompile</goal> | |
</goals> | |
</execution> | |
</executions> | |
<configuration> | |
<scalaVersion>${scala.version}</scalaVersion> | |
<args> | |
<arg>-target:jvm-1.5</arg> | |
</args> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-eclipse-plugin</artifactId> | |
<configuration> | |
<downloadSources>true</downloadSources> | |
<buildcommands> | |
<buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand> | |
</buildcommands> | |
<additionalProjectnatures> | |
<projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature> | |
</additionalProjectnatures> | |
<classpathContainers> | |
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer> | |
<classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer> | |
</classpathContainers> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<reporting> | |
<plugins> | |
<plugin> | |
<groupId>org.scala-tools</groupId> | |
<artifactId>maven-scala-plugin</artifactId> | |
<configuration> | |
<scalaVersion>${scala.version}</scalaVersion> | |
</configuration> | |
</plugin> | |
</plugins> | |
</reporting> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment