![]() ![]() To use PackedLong to store the state of the world, a mapping from cell location on the game board to the bit location in the variable is required. Similarly, the state of bit i of the variable v of type long can be retrieved and stored in variable b of type boolean using the method call: Note in particular the use of the assignment operator to update the value stored in the variable v. You may recall that, given a variable v of type long, the set method of PackedLong class is able to set bit i of v to the value val with the method call: Given this size restriction, this particular implementation of Conway's Game of Life is called TinyLife in this workbook. The limit of 64 boolean values restricts the size of the game board to an eight-by-eight world. We will use these boolean values to represent the liveness of a cell in other words, if the cell is alive then true is stored, and if the cell is dead then false is recorded. The PackedLong class is capable of storing at most 64 boolean values in a variable of type long. You might think that dividn up a 10,000*10,000 spae into subspaces of 8*8 is a lot os tasks - but accumulating their states values is in fact much, much less computing work than performing the GoL algo to each cell plus their 8 neighbours plus comparing the number and storing the new state for the net iteration somewhere.īut like i said above, for a random init state with 30% population this wont make much sense, as there will be not many completely dead 8*8 subspaces to find (leave alone dead 256*256 subpaces)Īnd of course, the way of perfect optimisation will last but not least depend on your language.In this workbook you will build an implementation of Conway's Game of Life using the PackedLong class you wrote last week. When you are finished assigning the "has life" attribute to all possible subspaces, you end up with a list of subspaces which you now simply extend by +1 to each direction - with empty cells - and perform the regular (or modified) game of life rules to them. Only spaces which "have life" need to be divided into smaller subspaces - the empty ones can be skipped. Whenever you find life, you mark these subspaces as "has life". You could go down to subframes of 8*8 or 10*10 (not sure what makes the most sense here) now. If its sum of states is >0, you will now divide up the second quarter into 4 pieces again - and repeat this check for life for each of these subspaces. So if you have a field of 10,000 * 10,000, you´d first accumulate the states of the upper left quarter of 5,000 * 5,000.Īnd if the sum of states is zero in the first quarter, you can ignore this first quarter completely now and check the upper right 5,000 * 5,000 for life next. ![]() I would just divide the matrix up into halves and start checking the bigger ones first. Im my opinion it can make sense to check for completely dead spaces first, when your initial state is something like "random, but with chance for life lower than 5%." If the majority of cells is dead, you could save a lot of CPU time by skipping empty parts and not calculating stuff cell by cell. What is the most efficient algo mainly depends on the initial state. ![]()
0 Comments
Leave a Reply. |