Guest User

Untitled

a guest
Jan 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. /**
  2. * A functional Conway's game of life.
  3. */
  4. package object conway {
  5.  
  6. type Coord[A] = (Int, Int) => A
  7. type Calculator = Coord[Coord[Boolean] => Boolean]
  8. type Size = Int
  9.  
  10. val nextCell: Boolean => Int => Boolean = old => mates => if (mates > 3) false else if (mates == 3) true else (old && mates == 2)
  11.  
  12. val wrapper: Size => Int => Int = size => i => if (i < 0) size + i else if (i >= size) i % size else i
  13.  
  14. /** Function for working out the next generation's value for a board */
  15. val next: Size => Calculator = size => {
  16. val wrap = wrapper(size)
  17. (a, b) => {
  18. val (x, y) = (wrap(a), wrap(b))
  19. val (l, r, u, d) = (wrap(x - 1), wrap(x + 1), wrap(y - 1), wrap(y + 1))
  20. val mates = List((l, u), (l, y), (l, d), (x, u), (x, d), (r, u), (r, y), (r, d))
  21.  
  22. c => nextCell { c(x, y) } { mates filter { c.tupled } size }
  23. }
  24. }
  25.  
  26. implicit def asCoord[A](as: Array[Array[A]]): Coord[A] = (x, y) => as(x)(y)
  27.  
  28. val calculator: Size => Calculator = s => Array.tabulate(s, s) { next(s) }
  29.  
  30. val memo: Size => Coord[Boolean] => Coord[Boolean] = s => c => Array.tabulate(s, s) { c }
  31.  
  32. val generate: Calculator => Coord[Boolean] => Coord[Boolean] = c => old => (x, y) => c(x, y)(old)
  33.  
  34. val stream: Size => Coord[Boolean] => Stream[Coord[Boolean]] = size => init => {
  35. val next = generate(calculator(size)) andThen memo(size)
  36. def loop(old: Coord[Boolean]): Stream[Coord[Boolean]] = old #:: loop(next(old))
  37. loop(init)
  38. }
  39.  
  40. val render: Size => Coord[Boolean] => String = size => c => {
  41. val sb = new StringBuilder((size * size) + size, "")
  42. for (x <- 0 until size) {
  43. for (y <- 0 until size) sb.append(if (c(x, y)) "•" else " ")
  44. sb.append("\n")
  45. }
  46. sb toString
  47. }
  48.  
  49. val debug: Coord[Boolean] = (x, y) => { println("x: " + x + " y: " + y); ((x + y) & 1) == 0 }
  50. }
  51.  
  52. package conway {
  53.  
  54. class Board(size: Size) extends Coord[Boolean] {
  55. val board = Array.ofDim[Boolean](size, size)
  56. def apply(x: Int) = (y: Int) => board(x)(y)
  57. }
  58.  
  59. object Board {
  60. import util.Random._
  61. def random(s: Size) = asCoord(Array.tabulate(s, s) { (_, _) => nextBoolean })
  62. }
  63. }
Add Comment
Please, Sign In to add comment