Comway's game of life


#1

Good morning,

this R script is an implementation of Comway’s game of life. The game is computed in SciDB using AFL and the results are displayed using R.

Cheers!

library(scidb)
library(raster)
scidbconnect(host="myscidbserver",port=6666, username = "scidb", password = "xyzxyzxyzxyz")

# Run an AFL query
runquery <- function(q, ret){
	return(iquery(q, `return` = ret, afl = TRUE, iterative = FALSE, n = 400))
}

# Transform an array into a matrix
cw2matrix <- function(cw, size){
	matrix(data = as.vector(unlist(cw['val'], recursive = TRUE)), nrow = size, byrow = TRUE)
}
#############################################
# Game setup
#############################################
aname  = "myCOMWAY"
steps <- 50
size <- 20
qdel <- paste("remove(", aname, ")", sep = "")
qnew <- paste("store(build(<val:double NULL DEFAULT null>[i=0:", size - 1, ",", size, ",0,j=0:", size - 1, ",", size, ",0],random()%2), ", aname,")", sep = "")
qnext <- paste("insert(project(apply(apply(apply(join(", aname, ", window(", aname,", 1, 1, 1, 1, sum(val))), sum, val_sum - val),factor,iif((val = 0 and sum != 3), 0,iif((val = 0 and sum = 3), 1,iif(sum < 2, -1,iif(sum > 3,-1,0))))    ),newval, double(val + factor)), newval), ", aname,")", sep = "")

# Run the game
runquery(qdel, FALSE)
cw <- list()
for (i in 1:(steps)){
	if(i == 1){
		q <- qnew
	}else{
		q <- qnext
	}
	cw[[i]] <- cw2matrix(runquery(q, TRUE), size)
	plot(raster(cw[[i]]), main = paste("COMWAY t =", i-1, sep = " "))
}

#2

Very nice, thanks!