{
   "ancestor_list" : [],
   "description" : "a work in progress implementation of conway's game of life. some stuff still not working properly. Used the 1d cellular automaton by the berlin sc-group as reference.",
   "name" : "game of life",
   "author" : "Raver_Ale",
   "code" : "(\r\nvar xSize = 60;\r\nvar ySize = 60;\r\nvar scale = 10;\r\nvar waitDur = 0.3;\r\nvar win = Window.new(\"game of lyf\", Rect(0, 0, xSize*scale, ySize*scale));\r\nvar uview;\r\nvar arr, buf, initFunc, iterFunc, startIter, finalize, task, iter;\r\nvar xPos, yPos;\r\n\r\n\r\ninitFunc = {\r\n\tarr = Array.fill2D(xSize,ySize,{ [0, 1, 1, 0, 0].choose });\r\n\r\n\t~arr = arr;\r\n};\r\n\r\nuview = UserView.new(win, Rect(0, 0, xSize*scale, ySize*scale));\r\nuview.clearOnRefresh = true;\r\nuview.drawFunc = { |uview|\r\n\txSize.do { |x|\r\n\t\tySize.do { |y|\r\n\t\t\tPen.fillColor = if(~arr[x][y]>0, {Color.black},{Color.white});\r\n\t\t\tPen.fillRect(Rect(x * scale,y * scale, scale, scale));\r\n\t\t\tPen.fill;\r\n\t\t}\r\n\t}\r\n};\r\n\r\n// TODO:\r\n// __borders not working correctly\r\n// __rules slightly off ...\r\niterFunc = {\r\n\tvar state, localArr, border, xP, yP;\r\n\r\n\tborder = arr.size - 1;\r\n\r\n\tySize.do{ |y|\r\n\t\txSize.do{ |x|\r\n\t\t\txP = x;\r\n\t\t\tyP = y;\r\n\r\n\t\t\tlocalArr = Array.fill2D(3, 3, { |y, x|\r\n\t\t\t\tvar xPos, yPos;\r\n\t\t\t\txPos = x + xP - 1;\r\n\t\t\t\tyPos = y + yP - 1;\r\n\r\n\t\t\t\tif( yPos == -1, { 0 }, {\r\n\t\t\t\t\tif( yPos >= border, { 0 }, {\r\n\t\t\t\t\t\tif( xPos == -1, { 0 }, {\r\n\t\t\t\t\t\t\tif ( xPos >= border, { 0 }, {\r\n\t\t\t\t\t\t\t\t~arr[xPos][yPos]\r\n\t\t\t\t\t\t\t})\r\n\t\t\t\t\t\t})\r\n\t\t\t\t\t})\r\n\t\t\t\t});\r\n\r\n\t\t\t});\r\n\r\n\t\t\tstate = localArr.sum.sum;\r\n\t\t\t//localArr.postln;\r\n\t\t\t//state.postln;\r\n\r\n\t\t\tif(~arr[xP][yP] == 1, {\r\n\t\t\t\tstate = state - 1;\r\n\t\t\t\tif(state < 2, { ~arr[xP][yP] = 0 });\r\n\t\t\t\tif(state > 3, { ~arr[xP][yP] = 0 });\r\n\t\t\t});\r\n\t\t\tif(~arr[xP][yP] == 0, {\r\n\t\t\t\tif(state == 3, { ~arr[xP][yP] = 1 });\r\n\t\t\t});\r\n\t\t}\r\n\t};\r\n};\r\n\r\nstartIter = {\r\n\tinitFunc.value;\r\n\ttask = iter.fork(AppClock);\r\n};\r\n\r\niter = {\r\n\tinf.do{\r\n\t\t//\"---------------------------------------------------\".postln;\r\n\t\tuview.refresh;\r\n\t\twaitDur.wait;\r\n\t\titerFunc.value;\r\n\r\n\t}\r\n};\r\n\r\nfinalize = {\r\n\ttask.stop;\r\n};\r\n\r\nwin.front;\r\nwin.onClose = finalize.value;\r\nstartIter.value;\r\n)",
   "id" : "1-4WC",
   "is_private" : null,
   "labels" : [
      "generative",
      "conway",
      "game of life",
      "visual",
      "work in progress"
   ]
}
