Created
October 17, 2019 11:20
-
-
Save Dev-Owl/d149890f62d7c63a51910fd8bca6ad29 to your computer and use it in GitHub Desktop.
Game of life - runTheGame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future<void> _runTheGame() async { | |
while (_running) { | |
await Future.delayed(Duration(milliseconds: 350)); | |
var newWorld = List<bool>.from(_world); | |
for (var i = 0; i < worldSize; ++i) { | |
//In case x cell alive do this | |
var livingNeighbors = _countLivingCellsNearby(i); | |
if (livingNeighbors < 2 || livingNeighbors > 3) { | |
//die | |
newWorld[i] = false; | |
} else if (_world[i] && | |
(livingNeighbors == 2 || livingNeighbors == 3)) { | |
//life | |
newWorld[i] = true; | |
} else if (!_world[i] && livingNeighbors == 3) { | |
//life | |
newWorld[i] = true; | |
} | |
_world[i] = newWorld[i]; | |
} | |
_stateController.add(0); | |
} | |
} | |
int _countLivingCellsNearby(int cellIndexToCheck) { | |
final left = cellIndexToCheck % rowLength == 0 | |
? cellIndexToCheck + (rowLength - 1) | |
: cellIndexToCheck - 1; | |
final right = (cellIndexToCheck + 1) % rowLength == 0 | |
? cellIndexToCheck - (rowLength - 1) | |
: cellIndexToCheck + 1; | |
final top = cellIndexToCheck <= (rowLength - 1) | |
? worldSize - (rowLength - cellIndexToCheck).abs() | |
: cellIndexToCheck - rowLength; | |
final bottom = cellIndexToCheck >= (worldSize - rowLength) | |
? ((worldSize - rowLength) - cellIndexToCheck).abs() | |
: cellIndexToCheck + rowLength; | |
final topLeft = top % rowLength == 0 ? top + (rowLength - 1) : top - 1; | |
final topRight = | |
(top + 1) % rowLength == 0 ? top - (rowLength - 1) : top + 1; | |
final bottomLeft = | |
bottom % rowLength == 0 ? bottom + (rowLength - 1) : bottom - 1; | |
final bottomRight = | |
(bottom + 1) % rowLength == 0 ? bottom - (rowLength - 1) : bottom + 1; | |
return _boolToInt(_world[left]) + | |
_boolToInt(_world[right]) + | |
_boolToInt(_world[top]) + | |
_boolToInt(_world[bottom]) + | |
_boolToInt(_world[topLeft]) + | |
_boolToInt(_world[topRight]) + | |
_boolToInt(_world[bottomLeft]) + | |
_boolToInt(_world[bottomRight]); | |
} | |
int _boolToInt(bool input) { | |
return input ? 1 : 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment