public void AddTokensFromPile(TokenPile tokenPile) { foreach (int key in tokenPile._tokenCounts.Keys) { AddTokens(key, tokenPile[key]); } }
/// <summary> /// Advance a population to another generation by applying a generation rule. /// </summary> /// <param name="population">The input population.</param> /// <param name="rule">The generation rule.</param> /// <param name="nhbdSize">The size of the neighborhood.</param> /// <param name="popIndex"> </param> /// <returns>The result of applying the generation rule to the input population.</returns> public Population AdvancePopulation(Population population, ulong rule, int nhbdSize, int popIndex) { Contract.Requires(population != null); Contract.Requires(rule >= 0); Contract.Requires(nhbdSize > 0); Population retPop = new Population(population.Size); // The index into the population to select the cells around the current cell by the nhbd size. int indexMag = (int)Math.Floor((double)nhbdSize / 2); // Iterate through every cell in the population, and for each one determine if the new cell at // that index in the new population will be alive or dead. for (int i = 0; i < retPop.Size; i++) { int ruleIndex = 0; TokenPile contributingTokens = new TokenPile(); // Iterate through the cells around the current cell, dependant on the nhbd size. for (int n = -indexMag+i; n <= indexMag+i; n++) { // If the cell is alive (and thus should contribute), then add the power of two of the current // index to the rule index. if (population[n].Value == 0) continue; ruleIndex += (int)Math.Pow(2, n + indexMag - i); if (UseTokens) { contributingTokens.AddTokensFromPile(population[n].Tokens); } } // Index into the rule by all of the cells that contributed to this new cell. int cellValue = (rule & (1UL << ruleIndex)) > 0 ? 1 : 0; // Make the new cell, carrying over the tokens of those cells that contributed. Cell newCell = new Cell(cellValue) {Tokens = contributingTokens.Normalize()}; // Add the cell to the return population. retPop.Cells.Add(newCell); } return retPop; }
public Cell(int value) { Value = value; Tokens = new TokenPile(); }