Conway's Game of Life/Scala: Difference between revisions

m
Fixed syntax highlighting.
(moved from Conway's Game of Li)
 
m (Fixed syntax highlighting.)
 
Line 22:
created, an exception will be thrown to indicate the board can no longer be correctly represented.
 
<langsyntaxhighlight lang="scala">class Coord private (val x: Int, val y: Int) {
private val offsets = List(-1, 0, 1)
private def offsetsOf(n: Int) = offsets map (_ + n)
Line 82:
*/
implicit def coordFromTuple(t: (Int, Int)) = apply(t._1, t._2)
}</langsyntaxhighlight>
 
'''Generation'''
Line 94:
with the pattern recentered.
 
<langsyntaxhighlight lang="scala">class Generation(val alive: Set[Coord]) extends Set[Coord] {
import Generation._
 
Line 201:
case _ => None
}
}</langsyntaxhighlight>
 
'''Conway's Game of Life Patterns'''
Line 215:
A few helper methods are provided to get these patterns into use.
 
<langsyntaxhighlight lang="scala">object ConwayPatterns {
// Lists for all patterns available
def stillLives = List(block, beehive, loaf, boat) map ((_, 1))
Line 328:
def moveTo(coords: Traversable[Coord], to: Coord) = coords map (_ + to)
}</langsyntaxhighlight>
 
'''Tester'''
Line 340:
It also prints three generations of a blinker, as requested.
 
<langsyntaxhighlight lang="scala">object ConwayTester {
import ConwayPatterns._
val MaxGenerations = 5500 // Give up at MaxGenerations to avoid spending too much time on errors
Line 429:
}
}
}</langsyntaxhighlight>
 
Output:
9,476

edits