Note that there are some explanatory texts on larger screens.

plurals
  1. POLooking for ideas how to refactor my algorithm
    text
    copied!<p>I am trying to write my own <a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" rel="nofollow noreferrer">Game of Life</a>, with my own set of rules. First 'concept', which I would like to apply, is socialization (which basicaly means if the cell wants to be alone or in a group with other cells). Data structure is 2-dimensional array (for now).</p> <p>In order to be able to move a cell to/away from a group of another cells, I need to determine where to move it. The idea is, that I evaluate all the cells in the area (neighbours) and get a vector, which tells me where to move the cell. Size of the vector is 0 or 1 (don't move or move) and the angle is array of directions (up, down, right, left).</p> <p>This is a image with representation of forces to a cell, like I imagined it (but reach could be more than 5):</p> <p><a href="http://img293.imageshack.us/img293/2852/29186643.png" rel="nofollow noreferrer">ForceAppliedToACell http://img293.imageshack.us/img293/2852/29186643.png</a></p> <p>Let's for example take this picture:</p> <p><a href="http://img683.imageshack.us/img683/7357/70002678.png" rel="nofollow noreferrer">Example http://img683.imageshack.us/img683/7357/70002678.png</a></p> <pre><code>Forces from lower left neighbour: down (0), up (2), right (2), left (0) Forces from right neighbour : down (0), up (0), right (0), left (2) sum : down (0), up (2), right (0), left (0) </code></pre> <p>So the cell should go up.</p> <p>I could write an algorithm with a lot of if statements and check all cells in the neighbourhood. Of course this algorithm would be easiest if the 'reach' parameter is set to 1 (first column on picture 1). But what if I change reach parameter to 10 for example? I would need to write an algorithm for each 'reach' parameter in advance... How can I avoid this (notice, that the force is growing potentialy (1, 2, 4, 8, 16, 32,...))? Can I use specific design pattern for this problem?</p> <p>Also: the most important thing is not speed, but to be able to extend initial logic.</p> <p>Things to take into consideration:</p> <ul> <li>reach should be passed as a parameter</li> <li>i would like to change function, which calculates force (potential, fibonacci)</li> <li>a cell can go to a new place only if this new place is not populated</li> <li>watch for corners (you can't evaluate right and top neighbours in top-right corner for example)</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload