Startseite > .NET, Code, J# > Game of Life mit J#

Game of Life mit J#


Ich hab einen dummen Fehler gefunden. Der Fehler ist im Grunde ein Denkfehler von mir. Ich habe den “Flag”, ob die Zelle lebt oder stirbt direkt im selben Array geschrieben weswegen das Ergebnis dann verfälscht wurde. Wenn z.B. Zelle 1 genau 3 Nachbarn hat und deswegen wiederbelebt wird, wird im selben Moment Zelle 2 sterben, da in diesem Fall Regel 4 greift.

image

Um das Problem zu umgehen bzw. zu Lösen, schreibt man einfach die Ergebnisse der “Nachbarprüfung” in einem Buffer. Danach schreibt man den Buffer in das eigentliche Zellenarray zurück.

Dieses J# Programm funktioniert zum Beispiel genauso wie es sein sollte. Ich werde die anderen Game of Life Versionen nach und nach korrigieren.

image

J#
1
23
45
67
89
1011
1213
1415
1617
1819
2021
2223
2425
2627
2829
3031
3233
3435
3637
3839
4041
4243
4445
4647
4849
5051
5253
5455
5657
5859
6061
6263
6465
6667
6869
7071
7273
7475
7677
7879
8081
8283
8485
8687
8889
9091
9293
9495
9697
9899
100101
102103
104105
106107
108109
110111
112113
114115
116117
118119
120121
122123
124125
126127
128129
130131
132133
134135
136137
138139
140141
142143
144145
146147
148149
150151
152153
154155
package GameOfLifeJSharp;
 import java.util.Random;
import System.*;  public class Program{
    final static int WORLD_WIDTH = 30;    final static int WORLD_HEIGHT = 20;
    final static double LIFE_ABUNDANCY = 31.5;    final static int STABLE_GENERATIONS = 10;
    final static char CELL_PRESENTATION = 'O';      public static void main(String[] args)    {
        int[,] cell = new int[WORLD_WIDTH,WORLD_HEIGHT];        // In my older Game of Life code I used to
        // check directly, but this seems to lead to        // faulty results...
        int[,] cellBuffer = new int[WORLD_WIDTH,WORLD_HEIGHT];          int stableGenerations = 0;        int startingCell = (int)(LIFE_ABUNDANCY * 
            (WORLD_WIDTH * WORLD_HEIGHT) / 100);        int oldPopulation = 0; // How many cells do we have last gen?
        int population = startingCell;        int generation = 0;
        // This avoids the "if"        char[] cellPresentation = new char[] {' ', 
            CELL_PRESENTATION};          // assign a value to the cells ... zero!        for(int y=0; y < WORLD_HEIGHT; y++) {
            for(int x=0; x< WORLD_WIDTH; x++) {                cell[x,y] = 0;
                cellBuffer[x,y] =0;            }
        }          // randomize cell        Random random = new Random(
            System.currentTimeMillis()); // create a randomizer        // java... makes my life harder ^^
        int randX;        int randY;
        while (startingCell > 0) {            randX=random.nextInt(WORLD_WIDTH - 2) + 1;
            randY=random.nextInt(WORLD_HEIGHT - 2) + 1;            cell[randX, randY] = randomCell(
                cell[randX, randY], random);            startingCell -= cell[randX, randY];
        }          while (stableGenerations < STABLE_GENERATIONS)        {
            // show the cells            // but clear the screen first
            Console.Clear();            for (int y = 0; y < WORLD_HEIGHT; y++)
            {                for (int x = 0; x < WORLD_WIDTH; x++)
                {                    Console.Write(cellPresentation[cell[x, y]]);
                }                Console.Write("\n");
            }            Console.Write("Generation: " + generation + 
                " Population: " + population);            generation++;
             // check if this generation is stable
            if (population == oldPopulation)                stableGenerations++;
            else                stableGenerations = 0;
             oldPopulation = population;
            population = 0;              // check the cells            for (int y = 1; y < WORLD_HEIGHT - 2; y++)
            {                for (int x = 1; x < WORLD_WIDTH - 2; x++)
                {                    // count my neighbors
                    int neighbors = 0;                    neighbors += cell[x - 1, y - 1];
                    neighbors += cell[x - 1, y];                    neighbors += cell[x - 1, y + 1];
                    neighbors += cell[x, y - 1];                    neighbors += cell[x, y + 1];
                    neighbors += cell[x + 1, y - 1];                    neighbors += cell[x + 1, y];
                    neighbors += cell[x + 1, y + 1];                      // apply rules, but change thier                    // status in the buffer
                    // Birth                    if (cell[x, y] == 0 && neighbors == 3)
                        cellBuffer[x, y] = 1;                    else if (cell[x, y] == 0)
                        // we copy dead cells too;                        cellBuffer[x, y] = 0;
                     // Death due to loneliness
                    if (cell[x, y] == 1 && neighbors < 2)                        cellBuffer[x, y] = 0;
                     // live longer
                    if (cell[x, y] == 1 && neighbors > 1 &&                        neighbors < 4)
                        cellBuffer[x, y] = 1;                      // Death due to overpopulation                    if (cell[x, y] == 1 && neighbors > 3)
                        cellBuffer[x, y] = 0;                      population += cellBuffer[x, y];                }
            }              // now copy the buffer to cells            // clone doesnt seem to work :-/
            copyBufferToCell(cell, cellBuffer);            try
            {                Thread.sleep(100);
            }            catch (Exception e) { }
        }          Console.Read();    }
     // Java is such a sad language... no pass by reference
    // Even the shitty VB6 has pass by reference    public static int randomCell(int cell, Random rand)
    {        if (cell == 0 && rand.nextInt(1000) > 500)
            return 1;        else
            return 0;    }
     public static void copyBufferToCell(int[,] cell, 
        int[,] buffer)    {
        for (int x = 0; x < WORLD_WIDTH; x++) {            for (int y = 0; y < WORLD_HEIGHT; y++) {
                cell[x, y] = buffer[x, y];            }
        }    }
} 
GeSHi 1.0.8.8
Advertisements
Kategorien:.NET, Code, J# Schlagwörter: , ,
  1. Es gibt noch keine Kommentare.
  1. No trackbacks yet.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s

%d Bloggern gefällt das: