{"paragraphs":[{"config":{"colWidth":12,"graph":{"mode":"table","height":300,"optionOpen":false,"keys":[],"values":[],"groups":[],"scatter":{}},"enabled":true,"editorMode":"ace/mode/markdown","editorHide":true},"settings":{"params":{},"forms":{}},"jobName":"paragraph_1472844603706_-1038344790","id":"20160902-193003_1338549382","dateCreated":"2016-09-02T19:30:03+0000","status":"FINISHED","progressUpdateIntervalMs":500,"focus":true,"$$hashKey":"object:352","text":"%md\n# Conway's Game of Life\nThe purpose of this task is to run a version of [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) using Kompics component for each cell.\n\nAll the Kompics relevant code is already implemented for you, however, and you only need to fill in the following 4 rules:\n1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.\n2. Any live cell with two or three live neighbours lives on to the next generation.\n3. Any live cell with more than three live neighbours dies, as if by over-population.\n4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.\n \nImplement these rules at the marked position in the code below.\n\nNote that while this is a graded assignment, there are no tests and the assignment always passes automatically.\nThe purpose is merely for you to experiment with the zeppelin environment and familiarise yourself with the assignment submission procedure.\n\nOnce you have the basic version working, feel free to experiment with different initial state generators, and numbers of generations.\n\nSimply submit the assignment whenever you feel ready.\n","dateUpdated":"2016-09-02T19:35:49+0000","dateFinished":"2016-09-02T19:35:27+0000","dateStarted":"2016-09-02T19:35:27+0000","result":{"code":"SUCCESS","type":"HTML","msg":"
The purpose of this task is to run a version of Conway's Game of Life using Kompics component for each cell.
\nAll the Kompics relevant code is already implemented for you, however, and you only need to fill in the following 4 rules:
\nImplement these rules at the marked position in the code below.
\nNote that while this is a graded assignment, there are no tests and the assignment always passes automatically.\n
The purpose is merely for you to experiment with the zeppelin environment and familiarise yourself with the assignment submission procedure.
Once you have the basic version working, feel free to experiment with different initial state generators, and numbers of generations.
\nSimply submit the assignment whenever you feel ready.
\n"}},{"text":"import se.kth.edx.id2203.tutorial.gameoflife._\nimport se.kth.edx.id2203.validation._\nimport se.kth.edx.id2203.core._\nimport java.util.Random;\nimport se.sics.kompics.sl._\nimport se.sics.kompics.{ Kompics }\n\ncase class CellCInit(x: Int, y: Int, init: State.Initializer) extends se.sics.kompics.Init[Cell]\n\nclass CellC(init: CellCInit) extends Cell { // class Cell is a KompicsComponent\n // this cell's location in the grid\n val xPos = init.x;\n val yPos = init.y;\n // get the initial state from the State.Initializer (see below this class)\n private var state: State = init.init.apply(xPos, yPos);\n // initialize our view of our neighbours\n private val view = scala.collection.mutable.Map.empty[Tuple2[Int, Int], State];\n // keep track of the generation we are currently communicating on to not mix up delayed messages\n private var lastGen = -1l;\n\n // declare our ports (the same one but in both directions for broadcasting to neighbours and receiving their broadcasts)\n val envIn = requires(EnvironmentPort);\n val envOut = provides(EnvironmentPort);\n // handle messages on incoming environment port\n envIn uponEvent {\n case Progress(generation) => handle {\n //println(s\"Cell($xPos, $yPos) starting generation $generation as $state\"); // uncomment if you want to see some printouts\n // for every generaton broadcast our current state\n trigger(BroadcastState(generation, xPos, yPos, state) -> envOut);\n // and prepare to receiver other's states\n if (lastGen < generation) {\n view.clear();\n lastGen = generation;\n }\n }\n case BroadcastState(generation, x, y, otherState) => handle {\n // same as above, just in case we get another component's state broadcast before the instruction to move to the next generation\n if (lastGen < generation) {\n view.clear();\n lastGen = generation;\n }\n // add the received state to our view\n view += ((x -> y) -> otherState);\n //println(s\"Cell($xPos, $yPos) got state $otherState from Cell($x, $y) leading to: $view\"); // uncomment if you want to see some printouts\n if (view.size == 8) { // once we get the last state broadcast in a generation (from everyone around us: 3x3-1)\n // count live cells in our neighbourhood\n val liveC = view.values.count {\n case Alive => true\n case _ => false\n };\n // apply game of life rules to decide our next state based on current state and live count\n // **********************************\n // ******* STUDENT CODE HERE ********\n // **********************************\n }\n }\n }\n}\n\n\nval rand = new Random(System.currentTimeMillis());\n// randomly generate the initial state (easy but boring...try to do something more interesting if you like)\nval defaultInit: State.Initializer = (x: Int, y: Int) => {\n if (rand.nextBoolean()) {\n Alive\n } else {\n Dead\n }\n}\n// just a way to generate a child init object from a method\nval toCellCInit: Cell.Initializer = {\n case (x: Int, y: Int, init: State.Initializer) => CellCInit(x, y, init).asInstanceOf[se.sics.kompics.Init[Cell]]\n};\n\nval cellclass = classOf[CellC].asInstanceOf[Class[Cell]] // nvm some ugly type magic...Java Kompics is sometimes a bit overspecific on what types it wants\nrunKompics[ParentC](GameOfLifeInit(defaultInit, cellclass, toCellCInit, numGenerations = 10)); // create the Kompics environment and wait for it to finish\n\ncheckExample[ParentC] // generate grading token\n \n ","dateUpdated":"2016-09-02T19:36:31+0000","config":{"enabled":true,"graph":{"mode":"table","height":300,"optionOpen":false,"keys":[],"values":[],"groups":[],"scatter":{}},"editorMode":"ace/mode/scala","colWidth":12},"settings":{"params":{},"forms":{}},"jobName":"paragraph_1472824178021_428466840","id":"20160615-191351_1691822922","dateCreated":"2016-09-02T13:49:38+0000","dateStarted":"2016-09-02T19:27:53+0000","dateFinished":"2016-09-02T19:28:08+0000","status":"FINISHED","progressUpdateIntervalMs":500,"$$hashKey":"object:182","errorMessage":"","focus":true},{"dateUpdated":"2016-09-02T14:58:01+0000","config":{"enabled":true,"graph":{"mode":"table","height":300,"optionOpen":false,"keys":[],"values":[],"groups":[],"scatter":{}},"editorMode":"ace/mode/scala","colWidth":12},"settings":{"params":{},"forms":{}},"jobName":"paragraph_1472824178023_429236338","id":"20160615-191928_480262104","dateCreated":"2016-09-02T13:49:38+0000","dateStarted":"2016-09-02T14:58:03+0000","dateFinished":"2016-09-02T14:58:15+0000","status":"FINISHED","errorMessage":"","progressUpdateIntervalMs":500,"$$hashKey":"object:183"}],"name":"Example - Game of Life","id":"2BVHH9RNP","angularObjects":{"2BKQCVH92:shared_process":[],"2BMXUXUJ6:shared_process":[]},"config":{"looknfeel":"default"},"info":{}}