Conway Game of Life - How to fake the "infinite" plane 2d

I use a 2d array to represent a grid of cells. When a cell on the edge or in the corner checks its neighbors that it will be outside the borders, it perceives them as constantly dead.

function getCell(row, column) { if(row === -1 || row === cellMatrix.length || column === -1 || column === cellMatrix[0].length) { return 0; } else return cellMatrixCopy[row][column]; } 

I would just like to get rid of behavior like gliders, stopping and turning into blocks when they reach the edge of the grid. How would you get rid of the edges of an array?

You can check out the full implementation here . Thanks in advance.

+4
source share
3 answers

How to fake the “infinite” 2d plane?

To create an infinite dimensional game panel, use a sparse matrix representation, where the row and column pointers are integers of arbitrary precision. Approximately as follows:

 map<pair<BigInt,BigInt>, Cell> matrix; Cell& get_cell(BigInt x, BigInt y) { return matrix[make_pair[x,y]]; } 
+4
source

The game of life cannot be faked endlessly. Say that the structure you created is trying to expand the last borders, which should stop its infinite expansion. Like MitchWheat, you can wrap the edges that are best suited (for a more natural behavior), but since this completion is complete, you cannot fake it endless in any situation without endless memory.

Since the game of life ends in full, this means that if something "leaves the edge", it is impossible to say whether it will return to the general case (related to the problem of stopping), which means any heuristic that you use to decide when it will go away from the edge, it will have a degree of inaccuracy, if this is acceptable, then you should consider this approach, although IMO imitating the game of life does not defeat the target correctly.

Another way would be to deliberately simulate a larger area than you show that objects seem to go off the edge

0
source

This question is also here with good answers.

The simplest modification of your simulation (and not mentioned in another question) is to triple the length and width of the area, but only show what you are showing now. The only catch would be if something went beyond the screen, hit the wall, and then returned. You can try playing with the rules of the game on the border to increase the likelihood that the cell will die (so that the glider can get a little further before becoming a block that can damage things). Perhaps a living cell with three neighbors would die if it were a stone's throw from the side.

One of the other answers (and our answer ) can be summarized as tracking live nodes, not the entire screen. Then you can do cool things like zooming and panning as if it were Google Maps.

0
source

Source: https://habr.com/ru/post/1500061/


All Articles