Error in Conway Game of Life

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); } } } 
+4
source share
2 answers

Your problem is very simple - you are reloading count in the wrong place. Put it inside the loop and it (probably) will work.

As for the rest of the code, if you want to make it much easier, just give the playing area a singleton border.

You will need to place the file you are reading (empty line above and below, empty characters on the left and right) and change your loops to:

  for (int i = 1; i < gen0.Length - 1; i++) 

and

  for (int j = 1; j < gen0[i].Length - 1; j++) 

but your calculation of the central count can then be reduced to one calculation:

  count = (gen0[i - 1][j - 1] == alive) ? 1 : 0 + (gen0[i - 1][j] == alive) ? 1 : 0 + ... etc ... 

which should make for cleaner code and ensure that any other errors you can make are much easier to detect.

+2
source

So, the first mistake I see is that you are not actually copying the board for the next iteration.

 gen1 = gen0; 

The code above only assigns the link gen1 to the same object as gen0. Therefore, when you change gen1, you also modify gen0 ... causing inconsistencies in the last iterations. Try the following:

 gen1 = (string[])gen0.Clone(); 

The second error is that int count = 0 should be in the second loop instead of the first:

 for (int i = 0; i< gen0.Length; i++) { // not here // int count = 0 for (int j = 0; j < gen0[i].Length; j++) { // here int count = 0 ... } ... } 

This way you reset counts every cell instead of every row.

+2
source

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


All Articles