Last active
June 7, 2019 11:04
-
-
Save xylude/124edc8b9768f60679e4fac060e5356b to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Gol : MonoBehaviour { | |
private float spacing = .5f; | |
public GameObject Cell; | |
private Dictionary<string, GameObject> _board = new Dictionary<string, GameObject>(); | |
// Use this for initialization | |
void Start () { | |
PopulateCells(new List<string>() { | |
"0,0", | |
"0,1", | |
"0,3", | |
"0,4", | |
"1,0", | |
"1,1", | |
"1,4", | |
"1,5", | |
"1,6", | |
}); | |
} | |
// Update is called once per frame | |
void Update () { | |
if (Input.GetKeyDown("space")) { | |
IterateBoard(); | |
} | |
} | |
void IterateBoard() { | |
var processed = new List<string>(); | |
var toKill = new List<string>(); | |
var toPopulate = new List<string>(); | |
foreach (var cell in _board) { | |
var activeNeighborCount = ActiveNeighborCount(cell.Key); | |
if ( activeNeighborCount > 3 || activeNeighborCount < 2 ) { | |
// kill cell | |
toKill.Add(cell.Key); | |
} | |
foreach( var neighbor in GetNeighbors( cell.Key ) ) { | |
// only look at empty space that has not already been looked at | |
if ( !processed.Contains(neighbor) && !_board.ContainsKey(neighbor) ) { | |
var activeNeighborCount_neighbors = ActiveNeighborCount(neighbor); | |
if (activeNeighborCount_neighbors == 3) { | |
toPopulate.Add(neighbor); | |
} | |
processed.Add(neighbor); | |
} | |
} | |
} | |
foreach( var loc_kill in toKill ) { | |
Destroy(_board[loc_kill]); | |
_board.Remove(loc_kill); | |
} | |
PopulateCells(toPopulate); | |
} | |
int ActiveNeighborCount(string location) { | |
var neighbors = GetNeighbors(location); | |
var active = 0; | |
foreach( var neighbor in neighbors ) { | |
if (_board.ContainsKey(neighbor)) { | |
active++; | |
} | |
} | |
return active; | |
} | |
List<string> GetNeighbors (string location) { | |
var coords = GetCoordsFromString(location); | |
return new List<string>() { | |
ConvertCoordsToString(new List<int>() { coords[0] - 1, coords[1] - 1 }), | |
ConvertCoordsToString(new List<int>() { coords[0] - 1, coords[1] }), | |
ConvertCoordsToString(new List<int>() { coords[0] - 1, coords[1] + 1 }), | |
ConvertCoordsToString(new List<int>() { coords[0], coords[1] - 1 }), | |
ConvertCoordsToString(new List<int>() { coords[0], coords[1] + 1 }), | |
ConvertCoordsToString(new List<int>() { coords[0] + 1, coords[1] - 1 }), | |
ConvertCoordsToString(new List<int>() { coords[0] + 1, coords[1] }), | |
ConvertCoordsToString(new List<int>() { coords[0] + 1, coords[1] + 1 }), | |
}; | |
} | |
void PopulateCells(List<string> locations) { | |
foreach (var location in locations) { | |
if (!_board.ContainsKey(location)) { | |
var coords = GetCoordsFromString(location); | |
_board[location] = Instantiate( | |
Cell, | |
new Vector3( | |
coords[0] * spacing, | |
.15f, | |
coords[1] * spacing | |
), | |
Quaternion.identity | |
); | |
} | |
} | |
} | |
List<int> GetCoordsFromString(string coords) { | |
var parts = coords.Split(','); | |
return new List<int>() { | |
System.Convert.ToInt32(parts[0]), | |
System.Convert.ToInt32(parts[1]), | |
}; | |
} | |
string ConvertCoordsToString(List<int> coords) { | |
return coords[0].ToString() + "," + coords[1].ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment