##============================================
## Basic Usage:
## explore the example
##============================================
data(conway)
plot(sim(conway))
## more interesting start conditions
m <- matrix(0, 40, 40)
m[5:35,19:21] <-1
init(conway) <- m
plot(sim(conway), col=c("white", "green"), axes=FALSE)
## change survival rules
parms(conway) <- list(srv=c(3,4), gen=c(3,4))
plot(sim(conway), col=c("white", "green"), axes=FALSE)
init(conway) <- matrix(0, 10, 10)
fixInit(conway) # enter some "1"
sim(conway, animate=TRUE, delay=100)
##============================================
## Implementation:
## The code of Conways Game of Life
##============================================
conway <- new("gridModel",
main = function(time, init, parms) {
x <- init
nb <- eightneighbours(x)
surviv <- (x > 0 & (nb gener <- (x == 0 & (nb x <- as.numeric((surviv + gener) > 0)
dim(x) <- dim(init)
return(x)
},
parms = list(srv = c(2, 3), gen = 3),
times = 1:17,
init = matrix(round(runif(1000)), ncol=40),
solver = "iteration"
)
Run the code above in your browser using DataLab