Created
March 13, 2012 15:56
-
-
Save timriffe/2029553 to your computer and use it in GitHub Desktop.
Game of Life comments for Mazdaika
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Mazdaika | |
############################################################################### | |
## should be col*row, not col+row | |
# create matrix - initial life situation | |
crmtx <- function(col=5, row=5, st=0.4){ | |
x <- matrix(rbinom(col+row,1, st), ncol=col, nrow=row) | |
return(x) | |
} | |
# count neighbours for cells | |
neighbours <- function(mx) { | |
mt_c <- neighbours_c(mx) | |
mt_d <- neighbours_d(mx) | |
mt <- mt_c + mt_d | |
return(mt) | |
} | |
## 1) looks like you have a dead border implementation. No big deal, just be sure to state so | |
## certain neat formations, like gliders, etc, will have limited lifespans | |
## to have wrapped borders, instead of e.g. rbind(mx1[-1,],0) you could do rbind(mx1[-1,],mx1[,ncol(mx1)]) | |
## 2) you could eliminate calls to neighbours_c() and neighbours_d() by putting it all | |
## into neighbors() | |
# count neighbours in cross area | |
neighbours_c <- function(mx1){ | |
mt11 <- rbind(mx1[-1,],0) + rbind(0, mx1[-nrow(mx1),]) | |
mt22 <- cbind(mx1[,-1],0) + cbind(0, mx1[,-ncol(mx1)]) | |
mt1 <- mt11 + mt22 | |
return(mt1) | |
} | |
# count neighbours in diag area | |
neighbours_d <- function(mx){ | |
mx_up <- rbind(mx[-1,],0) | |
mx_up <- cbind(mx_up[,-1],0) + cbind(0, mx_up[,-ncol(mx_up)]) | |
mx_dw <- rbind(0,mx[-nrow(mx),]) | |
mx_dw <- cbind(mx_dw[,-1],0) + cbind(0, mx_dw[,-ncol(mx_dw)]) | |
mt22 <- mx_up + mx_dw | |
return(mt22) | |
} | |
## 1) switch has a special meaning in R, Like TRUE or FALSE. | |
## You should name the function something else | |
## 2) looks like you iterate down rows: really there's no need to iterate at all | |
## see if you can figure out how to eliminate the loop | |
# count next period of life | |
switch <- function(mxs,mts) { | |
mout <- matrix(0, ncol= ncol(mxs), nrow=nrow(mxs)) | |
for(i in 1:nrow(mxs)) | |
{ | |
a <- mxs[i,] | |
a <- ifelse((a==1 & (mts[i,] > 3 | mts[i,] < 2)) | (a==0 & mts[i,] < 3) ,0,1) | |
mout[i,] <- a | |
} | |
return(mout) | |
} | |
## recall that image() transposes the matrix, so you can do t(mx)- really makes no diff | |
## though since the system evolves the same either way | |
# run one iteration of life | |
iterate <- function(iters, rows, cols, prob, clrs, slp) { | |
mx <- crmtx(rows,cols, prob) | |
for (i in 1:iters) | |
{ | |
Sys.sleep(slp) | |
mt <- neighbours(mx) | |
mx <- switch(mx,mt) | |
image(mx, axes = FALSE, col = clrs) | |
} | |
} | |
# variables | |
sleep <- 0.1 | |
rows <- 100 | |
coloumns <- 100 | |
probability <- 0.4 | |
colours <- c("#0f0f0f", "#ffffff") | |
iterations <- 100 | |
#start | |
iterate(iterations, rows, coloumns, probability, colours, sleep) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment