life.cmn 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # simple game of life implementation, wrapping space
  2. WIDTH: 40 .
  3. HEIGHT: 30 .
  4. ~8
  5. ~state:2000 # holds world state (increase sizes if board size increases)
  6. ~tmp:0
  7. ~secondRow:0 # points to start of second row
  8. ~lastRow:0 # points to start of last row
  9. ~lastCell:0 # points to last cell
  10. ~cellT:0 # cell top (above current cell)
  11. ~cellM:0 # cell middle (current cell)
  12. ~cellB:0 # cell bottom (under current cell)
  13. drawAndShift:
  14. $state>tmp
  15. ~0
  16. WIDTH HEIGHT * @'
  17. $0 WIDTH % 0 = ?
  18. 10 -> # newline after each row
  19. .
  20. ~8
  21. $tmp
  22. $0 1 & ?
  23. 93 91
  24. ;
  25. ". "
  26. .
  27. -> ->
  28. 1 |< $:tmp # we shift old state one left (it's still needed for step)
  29. $>tmp
  30. ~0
  31. --
  32. .
  33. ^
  34. ~8
  35. .
  36. step:
  37. $<state
  38. $lastCell $:state
  39. $>state
  40. $lastRow>cellT
  41. $state>cellM
  42. $secondRow>cellB
  43. ~0
  44. WIDTH HEIGHT * @'
  45. ~8
  46. $cellM=secondRow !! ?
  47. $state>cellT
  48. ;
  49. $cellM=lastRow !! ?
  50. $state>cellB
  51. . .
  52. $<cellT $<cellM $<cellB
  53. $cellT 2 & $cellM 2 & $cellB 2 &
  54. $>cellT $>cellM $>cellB
  55. $cellT 2 & $cellB 2 &
  56. $>cellT $>cellM $>cellB
  57. $cellT 2 & $cellM 2 & $cellB 2 &
  58. + + + + + + + 1 |>
  59. $<cellM
  60. $cellM 2 & ?
  61. # is alive
  62. $0 3 = >< 2 = |
  63. ;
  64. # is dead
  65. 3 =
  66. .
  67. $cellM | $:cellM
  68. $>cellM
  69. ~0
  70. --
  71. .
  72. ^
  73. ~8
  74. .
  75. $0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 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0
  102. 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
  103. 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
  106. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  107. $cellT>0
  108. $>state # due to push we need to shift one right
  109. # set up helper poitners now:
  110. $state>secondRow
  111. ~0 WIDTH @' ~8 $>secondRow ~0 -- . ^ ~8
  112. $secondRow>lastRow
  113. ~0 WIDTH HEIGHT -- -- * @' ~8 $>lastRow ~0 -- . ^ ~8
  114. $lastRow>lastCell
  115. ~0 WIDTH -- @' ~8 $>lastCell ~0 -- . ^ ~8
  116. 1 @
  117. drawAndShift
  118. 10 ->
  119. 0 10 "press key to step (q to quit)" -->
  120. step
  121. <- "q" !=
  122. .
  123. ~0