-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_limited_life_hex.nim
52 lines (45 loc) · 1.03 KB
/
game_of_limited_life_hex.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
## Bare-bones SDL2 example
import std/[random, sequtils]
import ca
import ca/neighborhoods
import ca/colormixer
randomize()
type MyCell = ref object of Cell
age*: int
proc `init.rand`(ratio: float): Init =
proc(meta: Metadata): Cell =
if rand(1.0) >= ratio: MyCell(age: 1)
else: MyCell(age: 0)
method draw(cell: MyCell): Color =
let grad {.global.} = gradient(
(0.0, colors.Black),
(0.5, colors.White),
(1.0, colors.Black),
)
grad.lerp(cell.age.min(16)/16)
method next(cell: MyCell; field: Field; meta: Metadata): Cell =
if cell.age == 16:
cell.age = 0
return cell
let alives = neighborhoods.Hex
.mapIt(MyCell field.delta(meta, it.dx, it.dy))
.filterIt(it.age > 0 )
.len
if cell.age > 0:
case alives
of 3, 4:
inc cell.age
else:
cell.age = 0
else:
case alives
of 2, 3:
inc cell.age
else:
cell.age = 0
cell
var
field = field("Game of Limited Life (HEX)", 64, 64,
init= `init.rand`(ratio= 0.6))
field.cellShape = Hex
simulate field