I take the class that C # uses, and our first assignment is to implement the Conway Life game. We must do this by reading in a text file something like this:
* * *** ***
Then we must display the next 10 generations on the screen. I have a file read into a string array, then I copy it to another array. Then I look at it by character and change the copied array according to what should be the next generation. My problem is that the code that I have to consider as living neighbors does not work, and I cannot understand why. I displayed the number of living neighbors for each cell on the screen, and about half of them are wrong. I know that an error occurs with cells on the edge of the βboardβ, but I cannot figure out how to fix it.
Now I donβt want all this written for me, it would be a little pointless. I just can't figure out where my logic is off. Any help would be greatly appreciated. Also, I know that my code is pretty poor overall. This was the only way to understand this. Unfortunately.
class Program { static void Main(string[] args) { //gets file name from command arguments //checks to make sure file exists, exits if file does not exist if (!File.Exists(Environment.GetCommandLineArgs()[1])) { System.Environment.Exit(1); } //gets file name from command arguments then reads file into array of strings string[] gen0 = File.ReadAllLines(Environment.GetCommandLineArgs()[1]); string[] gen1 = gen0; char alive = '*'; char dead = ' '; //displays first generation foreach (string s in gen0) { Console.WriteLine(s); } Console.WriteLine("====================================="); //counts live neighbors of a cell int count = 0; for (int i = 0; i < gen0.Length; i++) { count = 0; for (int j = 0; j < gen0[i].Length; j++) { //check top left neighbor if (i > 0 && j > 0 && j < gen0[i-1].Length ) { if (gen0[i - 1][j - 1] == alive) { count++; } } //check above neighbor if (i > 0 && j < gen0[i-1].Length) { if (gen0[i - 1][j] == alive) { count++; } } //check top right neighbor if (i > 0 && j + 1 < gen0[i - 1].Length) { if (gen0[i - 1][j + 1] == alive) { count++; } } //check left neighbor if (j > 0) { if (gen0[i][j - 1] == alive) { count++; } } //check right neighbor if (j + 1 < gen0[i].Length) { if (gen0[i][j + 1] == alive) { count++; } } //check bottom left neighbor if (i + 1 < gen0.Length && j > 0 && j < gen0[i+1].Length) { if (gen0[i + 1][j - 1] == alive) { count++; } } //check below neighbor if (i + 1 < gen0.Length && j < gen0[i+1].Length) { if (gen0[i + 1][j] == alive) { count++; } } //check bottom right neighbor if (i + 1 < gen0.Length && j + 1 < gen0[i].Length && j + 1 < gen0[i+1].Length) { if (gen0[i + 1][j + 1] == alive) { count++; } } //Console.WriteLine(count); //kills cells if (count < 2 || count > 3) { gen1[i] = gen1[i].Remove(j, 1); gen1[i] = gen1[i].Insert(j, dead.ToString()); } //births cells if (count == 3) { gen1[i] = gen1[i].Remove(j, 1); gen1[i] = gen1[i].Insert(j, alive.ToString()); } } } foreach (string s in gen1) { Console.WriteLine(s); } } }
source share