Article
· Dec 20, 2018 2m read

Just for Fun. John Conway's Game of Life

Some weeks ago, I was reading a book by Stephen Hawking and Leonard Mlodinow, The Grand Design. At a certain point, trying to define why do we exist? , why do we use the models we use in physics?, ...those kind of things you know... they pointed at the Game of Life example invented by the mathematician John Coward in 1970... Basically he wanted to show that a system with really basic fundamental laws (Physics) could evolve and "live" to become a more complex system (Chemistry) in which "something" (humans) could work out its own model and complex rules to explain its reality… the rules for this deterministic model that he exposed were so basic that I thought it could be funny to implement them in ObjectScript when I had some spare time... there are others implementations in JavaScript and other languages... but not in ObjectScript... and that had to be corrected!!… so here you are!

Here you have a link to the class that simulate the Game of Life (OPNLib.Game.ConwayLifeGame). Just load, compile it and execute the Test() method from your terminal and you'll see how a world full of life arises and evolves in front of your eyes. Internally it creates a matrix using globals, where each node of level 1 is a row in which we'll have a bit string as long as the columns of the matrix we want to represent. Each bit will be the value of the cell: alive (1), dead (0)… but you have more details in the doc and the class itself in GitHub.

First of all, change your terminal settings to allow more columns (~132 or more) and rows (~48 or more).

If you want to set different initial settings you can do it. Just call the test method passing a JSON object as a parameter. Within the class you'll find more information, but for example, you could do:

set pTest = {
              "ID":1,
              "From":0, "To":200,  
              "Iterations":200,
              "InitialConfig":5,
              "Rows":80, "Columns":150, "Vector0":"1,1", "VectorN":"120,47",
              "AliveChar": ($c(4)), "DeadChar":"-"

}
do ##class(OPNLib.Game.ConwayLifeGame).Test(pTest)

And then, you can play with InitialConfig, changing its value from 1 to 5, to change the initial state of the universe…. 

Random - no predefined patterns - 0      

There is additional doc about this class in GitHub.

Enjoy! 

Discussion (10)2
Log in or sign up to continue

Great game!

Does not work, as object with that ID does not exist

zw ##class(OPNLib.Game.ConwayLifeGame).Test()
 
These are the settings for the test:
{"ID":1,"From":0,"To":200,"Iterations":200,"InitialConfig":5,"Rows":80,"Columns":150,"Vector0":"1,1","VectorN":"100,40"}
Press any key to continue... (Ctrl-C to stop)"0 "_$lb($lb(5002,"zCreateFromStatus+8^%Exception.StatusException.1 *tInfo(1,""code"")",,,,,,,,$lb(,"USER",$lb("^zCreateFromStatus+8^%Exception.StatusException.1^1","e^zThrowIfInterrupt+7^%Exception.StatusException.1^1","e^zTest+12^OPNLib.Game.ConwayLifeGame.1^2","e^^^0"))))/* ERROR #5002: Cache error: zCreateFromStatus+8^%Exception.StatusException.1 *tInfo(1,"code") [zTest+12^OPNLib.Game.ConwayLifeGame.1:USER] */

I added this code after read and it works:

If ..%ExistsId(pTest.ID) {
set g = ##class(OPNLib.Game.ConwayLifeGame).%OpenId(pTest.ID)
} else {
set g = ##class(OPNLib.Game.ConwayLifeGame).%New()
}