Note that there are some explanatory texts on larger screens.

plurals
  1. POConway's Game of Life logic error
    primarykey
    data
    text
    <p>I'm taking a class that uses C# and our first assignment is to implement Conway's game of Life. We must do this by reading in a text file formatted something like this:</p> <pre><code> * * *** *** </code></pre> <p>We then have to display the next 10 generations on the screen. I have the file read into a string array, then I copy it to another array. I then go through it character by character and change the copied array to match what the next generation should be. My problem is that the code I have to count the live neighbors isn't working and I can't figure out why. I displayed the number of live neighbors for each cell on the screen and about half of them are wrong. I know the error is occurring with cells on the edge of the "board," but I can't figure out how to fix it. </p> <p>Now, I don't want the whole thing written for me, that'd be a bit pointless. I just can't figure out where my logic is off. Any help would be appreciated. Also, I'm aware that my code is pretty poor, overall. This was just the only way I could figure it out. Sorry.</p> <pre><code> 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 &lt; gen0.Length; i++) { count = 0; for (int j = 0; j &lt; gen0[i].Length; j++) { //check top left neighbor if (i &gt; 0 &amp;&amp; j &gt; 0 &amp;&amp; j &lt; gen0[i-1].Length ) { if (gen0[i - 1][j - 1] == alive) { count++; } } //check above neighbor if (i &gt; 0 &amp;&amp; j &lt; gen0[i-1].Length) { if (gen0[i - 1][j] == alive) { count++; } } //check top right neighbor if (i &gt; 0 &amp;&amp; j + 1 &lt; gen0[i - 1].Length) { if (gen0[i - 1][j + 1] == alive) { count++; } } //check left neighbor if (j &gt; 0) { if (gen0[i][j - 1] == alive) { count++; } } //check right neighbor if (j + 1 &lt; gen0[i].Length) { if (gen0[i][j + 1] == alive) { count++; } } //check bottom left neighbor if (i + 1 &lt; gen0.Length &amp;&amp; j &gt; 0 &amp;&amp; j &lt; gen0[i+1].Length) { if (gen0[i + 1][j - 1] == alive) { count++; } } //check below neighbor if (i + 1 &lt; gen0.Length &amp;&amp; j &lt; gen0[i+1].Length) { if (gen0[i + 1][j] == alive) { count++; } } //check bottom right neighbor if (i + 1 &lt; gen0.Length &amp;&amp; j + 1 &lt; gen0[i].Length &amp;&amp; j + 1 &lt; gen0[i+1].Length) { if (gen0[i + 1][j + 1] == alive) { count++; } } //Console.WriteLine(count); //kills cells if (count &lt; 2 || count &gt; 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); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload