Archiv

Posts Tagged ‘Game of Life’

Game of Life mit J#

23. Januar 2011 Hinterlasse einen Kommentar

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
Kategorien:.NET, Code, J# Schlagwörter: , ,

Game of Life mit C# und WinForms

22. Januar 2011 Hinterlasse einen Kommentar

Diesmal habe ich in C# mit Klassen so gearbeitet wie es im OOP angedacht ist. Die Zellen als Klasse, eine Gruppe der Zellen als Klasse, die Welt als Klasse, der Renderer als Klasse u.s.w…

Es ist lang geworden, da ich noch ein paar Gimmicks eingebaut habe; Zum Beispiel Mutanten und Zombies!

Das Hauptprogramm ist eigentlich schön kurz

C#
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
    static class Program
    {        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.        /// </summary>
        [STAThread]        static void Main()
        {            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);              // Create world            World SimWorld = new World();
            SimWorld.GridSize = 18;            SimWorld.Height = 30;
            SimWorld.Width = 40;            SimWorld.LifeAbundancy = 30;
            SimWorld.Zombies = true;            SimWorld.Mutants = true;
            SimWorld.Initialize();              FormWorld mainWindow = new FormWorld(SimWorld);              // Initialize form            mainWindow.FormBorderStyle = FormBorderStyle.FixedSingle;
            mainWindow.MaximizeBox = false;            mainWindow.Height = 600;
            mainWindow.Width = 800;            mainWindow.UseHexagon = false;
            mainWindow.CellBornedColor = Color.GreenYellow;            mainWindow.CellDyingColor = Color.Gray;
            mainWindow.CellLiveColor = Color.LimeGreen;            mainWindow.CellZombieColor = Color.DarkSlateBlue;
            mainWindow.CellMutantColor = Color.Red;            mainWindow.BackgroundColor = Color.PaleGreen;
            mainWindow.RefreshTime = 50;              // Create and init Button            Button btnGo = new Button();
            mainWindow.Controls.Add(btnGo);            btnGo.AutoSize = true;
            btnGo.Text = "Simulation starten";            btnGo.Top = (mainWindow.Height >> 1) - (btnGo.Height >> 1);
            btnGo.Left = (mainWindow.Width >> 1) - (btnGo.Width >> 1);            // Assign Click event
            btnGo.Click += delegate(object sender, EventArgs e)            // I know this is bad... 
            {                btnGo.Visible = false;
                mainWindow.StartSimulation();            };
                        Application.Run(mainWindow);
        }    }
GeSHi 1.0.8.8


Aber die Klassen sind arg lang…

Und so sieht es aus…

Normal

image

Normal in Hexagon Anordnung

image

Mit Zombies

image

Mit Zombies und Mutanten

image

Und der Source Code

C#
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
156157
158159
160161
162163
164165
166167
168169
170171
172173
174175
176177
178179
180181
182183
184185
186187
188189
190191
192193
194195
196197
198199
200201
202203
204205
206207
208209
210211
212213
214215
216217
218219
220221
222223
224225
226227
228229
230231
232233
234235
236237
238239
240241
242243
244245
246247
248249
250251
252253
254255
256257
258259
260261
262263
264265
266267
268269
270271
272273
274275
276277
278279
280281
282283
284285
286287
288289
290291
292293
294295
296297
298299
300301
302303
304305
306307
308309
310311
312313
314315
316317
318319
320321
322323
324325
326327
328329
330331
332333
334335
336337
338339
340341
342343
344345
346347
348349
350351
352353
354355
356357
358359
360361
362363
364365
366367
368369
370371
372373
374375
376377
378379
380381
382383
384385
386387
388389
390391
392393
394395
396397
398399
400401
402403
404405
406407
408409
410411
412413
414415
416417
418419
420421
422423
424425
426427
428429
430431
432433
434435
436437
438439
440441
442443
444445
446447
448449
450451
452453
454455
456457
458459
460461
462463
464465
466467
468469
470471
472473
474475
476477
478479
480481
482483
484485
486487
488489
490491
492493
494495
496497
498499
500501
502503
504505
506507
508509
510511
512513
514515
516517
518519
520521
522523
524525
526527
528529
530531
532533
534535
536537
538539
540541
542543
544545
546547
548549
550551
552553
554555
556557
558559
560561
562563
564565
566567
568569
570571
572573
574575
576577
578579
580581
582583
584585
586587
588589
590591
592593
594595
596597
598599
600601
602603
604605
606607
608609
610611
612613
614615
616617
618619
620621
622623
624625
626627
628629
630631
632633
634635
636637
638639
640641
642643
644645
646647
648649
650651
652653
654655
656657
658659
660661
662663
664665
666667
668669
670671
672673
674675
676677
678679
680681
682683
684685
686687
688689
690691
692693
694695
696697
698699
700701
702703
704705
706707
708709
710711
712713
714715
using System;
using System.Drawing;using System.Threading;
using System.Windows.Forms;  namespace GameOfLifeCSharpWinForms{
    static class Program    {
        /// <summary>        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>        [STAThread]
        static void Main()        {
            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);
             // Create world
            World SimWorld = new World();            SimWorld.GridSize = 16;
            SimWorld.Height = 30;            SimWorld.Width = 60;
            SimWorld.LifeAbundancy = 30;            SimWorld.Zombies = true;
            SimWorld.Mutants = true;            SimWorld.Initialize();
             FormWorld mainWindow = new FormWorld(SimWorld);
             // Initialize form
            mainWindow.FormBorderStyle = FormBorderStyle.FixedSingle;            mainWindow.MaximizeBox = false;
            mainWindow.Height = 600;            mainWindow.Width = 800;
            mainWindow.UseHexagon = true;            mainWindow.CellBornedColor = Color.GreenYellow;
            mainWindow.CellDyingColor = Color.Gray;            mainWindow.CellLiveColor = Color.LimeGreen;
            mainWindow.CellZombieColor = Color.DarkSlateBlue;            mainWindow.CellMutantColor = Color.Red;
            mainWindow.BackgroundColor = Color.PaleGreen;            mainWindow.RefreshTime = 400;
             // Create and init Button
            Button btnGo = new Button();            mainWindow.Controls.Add(btnGo);
            btnGo.AutoSize = true;            btnGo.Text = "Simulation starten";
            btnGo.Top = (mainWindow.Height >> 1) - (btnGo.Height >> 1);            btnGo.Left = (mainWindow.Width >> 1) - (btnGo.Width >> 1);
            // Assign Click event            btnGo.Click += delegate(object sender, EventArgs e)
            // I know this is bad...             {
                btnGo.Visible = false;                mainWindow.StartSimulation();
            };                        Application.Run(mainWindow);        }
    }      public class FormWorld : Form     {
        private static World World;        public bool Simulate { get; set; }
        public Color BackgroundColor { set; get; }        public Color CellDyingColor { get; set; }
        public Color CellBornedColor { get; set; }        public Color CellLiveColor { get; set; }
        public Color CellZombieColor { get; set; }        public Color CellMutantColor { get; set; }
        public int RefreshTime { get; set; }          public bool UseHexagon         { 
            get{return World.Cells.HexagonGrid;}            set { World.Cells.HexagonGrid = value; } 
        }          public FormWorld(World world): base()        {
            World = world;        }
         public void StartSimulation()
        {            Simulate = true;
             Graphics g = Graphics.FromHwnd(this.Handle);
            int Generation = 0;              while (Simulate)            {
                this.Invalidate();                Application.DoEvents();
                DrawBuffer(g);                  this.Text = "Generation: " + Generation +                    " Population: " + World.Cells.Population +
                    " Stable Generation: " +                     World.StableGenerationCounter +
                    " Is Stable: " + World.IsStable.ToString();                  Generation++;                if (World.IsStable) Simulate = false;
                 Thread.Sleep(RefreshTime);
            }              g.Dispose();        }
         private void DrawBuffer(Graphics g)
        {            // Create Buffer
            Bitmap Buffer = new Bitmap(this.Width,this.Height);            Graphics BufferGraphics = Graphics.FromImage(Buffer);
             // Draw World Background
            SolidBrush brush = new SolidBrush(BackgroundColor);            BufferGraphics.FillRectangle(brush, 0, 0, 
                this.Width, this.Height);              // Net Pen            Pen pen = new Pen(CellLiveColor);
             // Get center
            float X = (this.Width >> 1) -                 (World.GridSize * World.Width >> 1);
            float Y = (this.Height >> 1) -                 (World.GridSize * World.Height >> 1);
             // Draw the cells
            int CellSize = World.GridSize - 2;              Cell.CellState status;            for (int x = 0; x < World.Width; x++)
            {                for (int y = 0; y < World.Height; y++)
                {                    status = World.Cells[x, y].Status;
                    switch (status)                    {
                        case Cell.CellState.Borned:                             brush.Color = CellBornedColor; 
                            break;                        case Cell.CellState.Dying: 
                            brush.Color = CellDyingColor;                             break;
                        case Cell.CellState.Live:                             brush.Color = CellLiveColor;
                            break;                        case Cell.CellState.Zombies:
                            brush.Color = CellZombieColor;                            break;
                        case Cell.CellState.Mutants:                            brush.Color = CellMutantColor;
                            break;                    }
                    if (status != Cell.CellState.Dead)                    {
                        BufferGraphics.FillEllipse(brush,                             (x * World.GridSize) + X + 
                            World.Cells[x, y].XOffset,                             (y * World.GridSize) + Y, 
                            CellSize, CellSize);                    }
                }            }
            try            {
                // Draw me                g.DrawImage(Buffer, 0, 0);
            }            catch { }
             World.Refresh();
                        pen.Dispose();
            brush.Dispose();            BufferGraphics.Dispose();
            Buffer.Dispose();        }
         protected override void OnPaint(PaintEventArgs e)
        {            if(!Simulate)  base.OnPaint(e);
        }          protected override void OnPaintBackground(PaintEventArgs e)        {
            if(!Simulate) base.OnPaintBackground(e);        }
         protected override void OnClosing(
            System.ComponentModel.CancelEventArgs e)        {
            Simulate = false;            base.OnClosing(e);
        }      }      public class Cell    {
        [Flags]        public enum CellState 
        {             Dead = 2, 
            Borned = 4,             Live = 8, 
            Dying = 16,             Zombies = 32, 
            Mutants = 64,            AllLiving = Borned | Live | Mutants,
            AllDead = Dead | Dying        };
         // Properties
        internal int IsZombie        {
            get            {
                return (Status | CellState.Zombies)                    == CellState.Zombies ?
                    1 : 0;            }
        }          internal int IsMutant        {
            get            {
                return (Status | CellState.Mutants)                    == CellState.Mutants ?
                    1 : 0;            }
        }          private bool IsAlwaysDead = true;          private CellState _status;        public CellState Status
        {             get{ return _status; }
        }          private Cells _parent;        public Cells Parent
        { get { return _parent; } }          private int _x;        public int X { get { return _x; } }
         private int _y;
        public int Y { get { return _y; } }          public float XOffset        { 
            get             {
                if (Parent.HexagonGrid)                    return Y % 2 == 1 ? 
                        Parent.Parent.GridSize >> 1 : 0;                else
                    return 0;            } 
        }          public Cell(Cells parent, int x, int y)        {
            _parent = parent;            _x = x;
            _y = y;              // All Cells are always dead in the beginning            _status = CellState.Dead;
             // All Cells along the sides are always dead...
            // This is just easier to check them for neighbors            // cause no Errors will occure in this case
            if ((X > 0 && X < Parent.Parent.Width-1) &&                (Y > 0 && Y < Parent.Parent.Height-1))
                IsAlwaysDead = false;            else
                IsAlwaysDead = true;        }
         ~Cell()
        {            _parent = null;
        }          internal bool HasAttribute(CellState cellState)        {
            if ((Status & cellState) == cellState)                return true;
            else if ((Status | cellState) == cellState)                return true;
            else                return false;
        }          public void Live()        {
            if (Status == CellState.Dead)            {
                Parent.PopulationInternal++;                _status = CellState.Borned;
            }            else if (Status == CellState.Dying)
            {                Parent.PopulationInternal++;
                _status = CellState.Live;            }
            else if (Status == CellState.Borned)                _status = CellState.Live;
        }          public void Die()        {
            if (Status == CellState.Live ||                Status == CellState.Mutants)
            {                Parent.PopulationInternal--;
                _status = CellState.Dying;            }
            else if (Status == CellState.Borned)            {
                Parent.PopulationInternal--;                _status = CellState.Dead;
            }            else if (Status == CellState.Dying || 
                Status == CellState.Zombies)                _status = CellState.Dead;
        }          public Cell RandomizeLife()        {
            if (Status != CellState.Live)            {
                if (Parent.Randomize.Next(1000) > 500)                {
                    Live();                    return this;
                }                else
                    return this;            }
            else                return this;
         }
         public void RefreshCellStatus()
        {            // skip this if we are "always dead"
            if(IsAlwaysDead) return;              #region Normal Cells            int Neighbors = CountNeighbors(CellState.AllLiving);
                        // Apply Rules
            // No 1. - Birth            if (HasAttribute(CellState.AllDead)
                 && Neighbors == 3)                Live();
             else if (HasAttribute(CellState.AllLiving))
            {                // No. 2 - Lonelyness
                if (Neighbors < 2)                    Die();
                // No. 3 - Live longer 🙂                else if (Neighbors > 1 && Neighbors < 4)
                    Live();                // No. 4 - Overpopulated
                // Mutants dont die on overpopulation                else if (Neighbors > 3 &&
                    Status != CellState.Mutants)                    Die();
            }            else if (Status == CellState.Dying)
                Die();              #endregion              #region Unusual Cells            if (Parent.Parent.Zombies || Parent.Parent.Mutants)
            {                  int ZombieNeighbor =                    CountNeighbors(CellState.Zombies);
                 int MutantNeighbor =
                    CountNeighbors(CellState.Mutants);                  #region Zombie Cells                // This is the Zombie special
                if (Parent.Parent.Zombies)                {
                     // The Zombie get beaten to death
                    // if enough cell are around it                    if (Status == CellState.Zombies &&
                        Neighbors > 3)                        Die();
                    // Dead Cell have a small chance                    // in turning to zombies
                    else if (Status == CellState.Dead)                    {
                        if (Parent.Randomize.Next(1, 1000) > 992)                            _status = CellState.Zombies;
                    }                    // if I am not a zombie and one
                    // of my neighbors is a zombie                    // I have a chance of getting
                    // bitten. The more zombies the higher                    // the chance
                    else if (Status != CellState.Zombies &&                        ZombieNeighbor > 0)
                    {                        if (Parent.Randomize.Next(1, 1000) >
                            (999 >> ZombieNeighbor))                            _status = CellState.Zombies;
                    }                    // if there are too many zombies neighbors
                    // My neighbor will eat me                    else if (Status == CellState.Zombies &&
                        ZombieNeighbor > 4)                        Die();
                     // A Zombie has a chance of dying
                    // if a mutant is one of his neighbors                    // The more mutants the higher the
                    // chance                    if (Status == CellState.Zombies &&
                        MutantNeighbor > 0)                    {
                        if (Parent.Randomize.Next(1, 1000) >                            (700 >> ZombieNeighbor))
                            Die();                    }
                 }
                #endregion                  #region Mutant Cells                // Mutant Cell Special
                if (Parent.Parent.Mutants)                {
                    // A living cell has a chance in                    // turning to a mutant
                    if (Status == CellState.Live &&                        Parent.Randomize.Next(1, 1000) > 800)
                        _status = CellState.Mutants;                    // A mutant cell will die if
                    // there are more than 4                    // zombies around him
                    else if (Status == CellState.Mutants &&                        ZombieNeighbor > 4)
                        Die();                    // A mutant will turn normal if
                    // there are too many Mutants around                    // him
                    else if (Status == CellState.Mutants &&                        MutantNeighbor > 3)
                        _status = CellState.Live;                    // Dead Cells will live if more than 2
                    // Mutants are around                    else if (HasAttribute(CellState.AllDead) &&
                        MutantNeighbor > 2)                    { Live(); Live(); }
                 }
                #endregion            }
            #endregion        }
         private int CountNeighbors(CellState cellStateToCount)
        {            int RetVal = 0;
            if(!IsAlwaysDead)            {
                RetVal += Parent[X + 1, Y].                    HasAttribute(cellStateToCount).ToInt();
                 RetVal += Parent[X, Y - 1].
                    HasAttribute(cellStateToCount).ToInt();                RetVal += Parent[X, Y + 1].
                    HasAttribute(cellStateToCount).ToInt();                  RetVal += Parent[X - 1, Y].                    HasAttribute(cellStateToCount).ToInt();
                 // We are arranging the cells in a hexagon
                // Thats why its less 2 neighbors to check                // But we have to swap neighbors every 2nd
                // line                if (!Parent.HexagonGrid)
                {                    RetVal += Parent[X - 1, Y - 1].
                        HasAttribute(cellStateToCount).ToInt();                    RetVal += Parent[X - 1, Y + 1].
                        HasAttribute(cellStateToCount).ToInt();                    RetVal += Parent[X + 1, Y - 1].
                        HasAttribute(cellStateToCount).ToInt();                    RetVal += Parent[X + 1, Y + 1].
                        HasAttribute(cellStateToCount).ToInt();                }
                else                {
                    if (XOffset > 0)                    {
                        RetVal += Parent[X + 1, Y - 1].                            HasAttribute(cellStateToCount).ToInt();
                        RetVal += Parent[X + 1, Y + 1].                            HasAttribute(cellStateToCount).ToInt();
                    }                    else
                    {                        RetVal += Parent[X - 1, Y - 1].
                            HasAttribute(cellStateToCount).ToInt();                        RetVal += Parent[X - 1, Y + 1].
                            HasAttribute(cellStateToCount).ToInt();                    }
                }            }
            return RetVal;        }
         // Overload the plus operator
        public static int operator +(int number, Cell cell)        {
            return (cell.Status == CellState.Live ||                cell.Status== CellState.Borned)? 
                number + 1 : number;        }
         // Overload the minus operator
        public static int operator -(int number, Cell cell)        {
            return (cell.Status == CellState.Live ||                cell.Status == CellState.Borned) ? 
                number - 1 : number;        }
         // Override ToString
        public override string ToString()        {
            return "Cell " + X + " " + Y + " " +                 Status.ToString();
        }    }
     public class Cells
    {        internal int PopulationInternal { get; set; }
        internal Random Randomize { get; set; }          public int Population { get { return PopulationInternal; } }        public bool HexagonGrid { get; set; }
         private Cell[,] Lifeforms;
        public Cell this[int x, int y]        {
            get { return Lifeforms[x, y]; }            set { Lifeforms[x, y] = value; }
        }          private World _parent;        public World Parent
        { get { return _parent; } }                public Cells(World world)        {
            _parent = world;            Lifeforms = new Cell[Parent.Width, Parent.Height];
            PopulationInternal = 0;              // Populate with cells            for (int x=0; x < Parent.Width; x++)
            {                for (int y=0; y < Parent.Height; y++)
                {                    Lifeforms[x, y] = new Cell(this, x, y);
                }            }
             // Initialize Randomizer
            int Seed;            if (!int.TryParse(
                DateTime.Now.ToString("ffffff"), out Seed))                Seed = DateTime.Now.Second;
            Randomize = new Random(Seed);        }
         // Make sure to kill the cells
        ~Cells()        {
            for (int x = 0; x < Parent.Width; x++)            {
                for (int y = 0; y < Parent.Height; y++)                {
                    Lifeforms[x, y] = null;                }
            }            _parent = null;
        }          // Randomly Populate        public void Populate()
        {            // Calculate abundancy of life
            int Abundancy = ((Parent.Width-2) *                (Parent.Height-2)) * 
                Parent.LifeAbundancy / 100;            // randomly scatter cells
            int RandomY;            while (Abundancy > 0)
            {                for (int x = 1; x < Parent.Width - 2; x++)
                {                    RandomY = Randomize.
                        Next(1, Parent.Height - 2);                    Abundancy -= 
                        this[x, RandomY].                        RandomizeLife();
                }            }
        }    }
     public class World
    {        public Cells Cells { get; set; }
        public int Width { get; set; }        public int Height { get; set; }
        public int GridSize { get; set; }        public int StableGenerations { get; set; }
        public bool Zombies { get; set; }        public bool Mutants { get; set; }
        public int StableGenerationCounter        { get { return StabilityCounter; } }
         private bool _isStable = false;
        public bool IsStable { get { return _isStable; } }          private int _lifeAbundancy = 10;        public int LifeAbundancy
        {            get { return _lifeAbundancy; }
            set            {
                if (value < 0)                    _lifeAbundancy = 0;
                else if (value > 100)                    _lifeAbundancy = 100;
                else                    _lifeAbundancy = value;
            }        }
         public World()
        {            StableGenerations = 10;
        }          public void Initialize()        {
            if (Width < 4) Width = 4;            if (Height < 4) Height = 4;
            if (GridSize < 4) GridSize = 4;            Cells = new Cells(this);
            Cells.Populate();        }
         private int StabilityCounter = 0;
        private int OldPopulation = 0;        public void Refresh()
        {            for(int y=1; y<Height-2;y++)
            {                for(int x =1;x<Width-2 ;x++)
                {                    Cells[x,y].RefreshCellStatus();
                }            }
             if (Cells.Population > OldPopulation - 3 && 
                Cells.Population < OldPopulation + 3)                StabilityCounter++;
            else                StabilityCounter = 0;
             if (StabilityCounter == StableGenerations)
                _isStable = true;              OldPopulation = Cells.Population;        }
    }      internal static class Extensions    {
        public static int ToInt(this bool value)        {
            if(value) return 1;            else return 0;
        }    }
} 
GeSHi 1.0.8.8
Kategorien:.NET, C# Schlagwörter:

Game of Life mit VB6 Forms

20. Januar 2011 Hinterlasse einen Kommentar

Die Konsolenversion von VB6 war ja jetzt wirklich extrem hässlich, deswegen habe ich mal was schöneres gebastelt.
So sieht das Teil aus. Leider ist nichts ungewöhnliches dran. Keine Pointer, keine HACKS, nichts… Also richtig langweilig.

image

image

 

Und der Source Code

Visual Basic
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
156157
Option Explicit
 Private Declare Function WaitForSingleObject Lib "kernel32" ( _
    ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long  Private Const WELT_X As Integer = 16Private Const WELT_Y As Integer = 16
Private Const MAX_ANFANG_LEBEN As Integer = 20Private Const STABILE_MAX As Integer = 10
Private Const LEBEN_ZEICHEN As String = "X"  Private WithEvents BtnGo As CommandButton  Private Sub BtnGo_Click()      Dim x As Integer    Dim y As Integer
    Dim StabileGeneration As Integer: StabileGeneration = 0    Dim cells(0 To WELT_X - 1, 0 To WELT_Y - 1) As Byte
        ' welt nullen
    For y = 0 To WELT_Y - 1        For x = 0 To WELT_X - 1
            cells(x, y) = 0        Next
    Next        'welt mit viechern bevölkern    Randomize DateTime.Second(Time)
    Populate cells            Dim iGenerations As Integer: iGenerations = 0    Dim iOldPopulation As Integer: iOldPopulation = 0
    Dim iNewPopulation As Integer: iNewPopulation = MAX_ANFANG_LEBEN    Dim Neighbors As Integer: Neighbors = 0
        Do While StabileGeneration < STABILE_MAX
                DoEvents
        Me.Cls                                Me.Caption = "Generationen: " & iGenerations & _        " Anzahl: " & iNewPopulation
                Sleep 60
                ' stabilität checken
        If iNewPopulation > iOldPopulation - 3 And _            iNewPopulation < iOldPopulation + 3 Then
            StabileGeneration = StabileGeneration + 1        Else
            StabileGeneration = 0        End If
                iOldPopulation = iNewPopulation
        iNewPopulation = 0                For y = 1 To WELT_Y - 2            For x = 1 To WELT_X - 2
                                'Nachbarn zählen
                Neighbors = Neighbors + cells(x, y - 1)                Neighbors = Neighbors + cells(x, y + 1)
                                Neighbors = Neighbors + cells(x - 1, y)
                Neighbors = Neighbors + cells(x + 1, y)                                Neighbors = Neighbors + cells(x - 1, y - 1)                Neighbors = Neighbors + cells(x + 1, y - 1)
                                Neighbors = Neighbors + cells(x - 1, y + 1)
                Neighbors = Neighbors + cells(x + 1, y + 1)                                ' Regel anwenden                ' nr 1
                If cells(x, y) = 0 And Neighbors = 3 Then                    cells(x, y) = 1
                    Me.FillColor = vbGreen                    Me.Circle (16 * x, 16 * y), 7, vbGreen
                                'nr 2
                ElseIf Neighbors < 2 And cells(x, y) = 1 Then                    cells(x, y) = 0
                    Me.FillColor = &H808080                    Me.Circle (16 * x, 16 * y), 7, &H808080
                                    'nr 3
                ElseIf Neighbors > 1 And Neighbors < 4 And _                    cells(x, y) = 1 Then
                    Me.FillColor = vbBlue                    Me.Circle (16 * x, 16 * y), 7, vbBlue
                                    'nr 4
                ElseIf Neighbors > 3 And cells(x, y) = 1 Then                    cells(x, y) = 0
                    Me.FillColor = &H808080                    Me.Circle (16 * x, 16 * y), 7, &H808080
                                End If
                                iNewPopulation = iNewPopulation + cells(x, y)
                Neighbors = 0            Next
        Next                iGenerations = iGenerations + 1    Loop
        Me.FontTransparent = False
    If iOldPopulation = 0 Then        Me.Print "AUSGESTORBEN!"
    Else        Me.Print "Seit " & STABILE_MAX & " Generationen stabil"
    End IfEnd Sub
 Private Sub Populate(ByRef cells() As Byte)
        Dim lstart As Integer: lstart = MAX_ANFANG_LEBEN
    Dim i As Integer        Do While lstart > 0            For i = 1 To WELT_X - 2            lstart = lstart - RandomCells(cells, i, _
                CInt(Rnd() * (WELT_Y - 2)) + 1)        Next
            Loop
    End Sub
 Private Function RandomCells(ByRef cells() As Byte, _
    x As Integer, y As Integer) As Integer    If CInt(Rnd() * 1000) > 500 And cells(x, y) = 0 Then
        cells(x, y) = 1        RandomCells = 1
    Else        RandomCells = 0
    End IfEnd Function
 Private Function Sleep(ByVal millisec As Long)
  WaitForSingleObject -1, millisecEnd Function
 Private Sub Form_Load()
    Me.AutoRedraw = True    Me.ScaleMode = 3 'pixels
    Me.FillStyle = 0    Set BtnGo = Form1.Controls.Add("VB.Commandbutton", "BtnGo", Form1)
    BtnGo.Visible = True    BtnGo.Top = 10
    BtnGo.Left = Form1.ScaleWidth - BtnGo.Width - 10    BtnGo.Caption = "Los"
End Sub 
GeSHi 1.0.8.8

Ich habe mir schon eine Windows 98 SE VM gezogen für Turbo Pascal. Smiley

Kategorien:VB6, VBA Schlagwörter: , ,

Game of Life mit VB6

19. Januar 2011 Hinterlasse einen Kommentar

Game of Life mit VB6 über die Konsole. Das ist der absoluter s…. sag ich euch.

 

Visual Basic
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
156157
158159
160161
162163
164165
166167
168169
170171
172173
174175
176177
178179
180181
182183
184185
186187
188189
190191
192193
194195
196197
Option Explicit
 Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function GetStdHandle Lib "kernel32" ( _    ByVal nStdHandle As Long) As Long
Private Declare Function FreeConsole Lib "kernel32" () As LongPrivate Declare Function SetConsoleCtrlHandler Lib "kernel32" ( _
    ByVal HandlerRoutine As Long, ByVal Add As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32" ( _
    ByVal hObject As Long) As LongPrivate Declare Function WriteConsole Lib "kernel32" Alias _
    "WriteConsoleA" (ByVal hConsoleOutput As Long, lpBuffer As Any, _    ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, _
    lpReserved As Any) As LongPrivate Declare Function WaitForSingleObject Lib "kernel32" ( _
    ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long  Private hConsole As Long  Private Const WELT_X As Integer = 16Private Const WELT_Y As Integer = 16
Private Const MAX_ANFANG_LEBEN As Integer = 100Private Const STABILE_MAX As Integer = 10
Private Const LEBEN_ZEICHEN As String = "X"  Public Sub Main()        If AllocConsole Then            ' Konsole zeugs        ' =========================
        hConsole = GetStdHandle(-11&) ' Konsolenhandler holen             If hConsole = 0 Then            FreeConsole ' wenn handler null dann beenden
            Exit Sub        Else
            SetConsoleCtrlHandler AddressOf ConsoleCtrlHandler, True        End If
        ' bis hier hin        '==========================
                Dim x As Integer
        Dim y As Integer        Dim StabileGeneration As Integer: StabileGeneration = 0
        Dim CellAppearance(0 To 1) As String        CellAppearance(0) = " ": CellAppearance(1) = LEBEN_ZEICHEN
        Dim cells(0 To WELT_X - 1, 0 To WELT_Y - 1) As Byte                ' welt nullen        For y = 0 To WELT_Y - 1
            For x = 0 To WELT_X - 1                cells(x, y) = 0
            Next        Next
                'welt mit viechern bevölkern
        Randomize DateTime.Second(Time)        Populate cells
                Dim sLine(1 To WELT_X - 2) As String
        Dim sBlock As String        Dim iGenerations As Integer: iGenerations = 0
        Dim iOldPopulation As Integer: iOldPopulation = 0        Dim iNewPopulation As Integer: iNewPopulation = MAX_ANFANG_LEBEN
        Dim Neighbors As Integer: Neighbors = 0                Do While StabileGeneration < STABILE_MAX                        'Anzeigen            For y = 1 To WELT_Y - 2
                For x = 1 To WELT_X - 2                    sLine(x) = CellAppearance(cells(x, y))
                Next                sBlock = sBlock & Join(sLine, "") & "\n"
            Next            Printf sBlock, "\n\n", "Generationen: ", iGenerations, _
                " Anzahl: ", iNewPopulation, "\n\n\n\n\n\n\n\n"            Sleep 60
            sBlock = ""                        ' stabilität checken            If iNewPopulation > iOldPopulation - 2 And _
                iNewPopulation < iOldPopulation + 2 Then                StabileGeneration = StabileGeneration + 1
            Else                StabileGeneration = 0
            End If                        iOldPopulation = iNewPopulation            iNewPopulation = 0
                        For y = 1 To WELT_Y - 2
                For x = 1 To WELT_X - 2                                        'Nachbarn zählen                    Neighbors = Neighbors + cells(x, y - 1)
                    Neighbors = Neighbors + cells(x, y + 1)                                        Neighbors = Neighbors + cells(x - 1, y)                    Neighbors = Neighbors + cells(x + 1, y)
                                        Neighbors = Neighbors + cells(x - 1, y - 1)
                    Neighbors = Neighbors + cells(x + 1, y - 1)                                        Neighbors = Neighbors + cells(x - 1, y + 1)                    Neighbors = Neighbors + cells(x + 1, y + 1)
                                        ' Regel anwenden
                    ' nr 1                    If cells(x, y) = 0 And Neighbors = 3 Then
                        cells(x, y) = 1                                        'nr 2                    ElseIf Neighbors < 2 Then
                        cells(x, y) = 0                                            'nr 3                    ElseIf Neighbors > 1 And Neighbors < 4 Then
                        'lebe weiter ^^                                             'nr 4                    ElseIf Neighbors > 3 Then
                        cells(x, y) = 0                                        End If                                        iNewPopulation = iNewPopulation + cells(x, y)                    Neighbors = 0
                Next                sBlock = sBlock & Join(sLine) & "\n"
            Next                        iGenerations = iGenerations + 1        Loop
                Printf "\nSeit ", STABILE_MAX, " Generationen stabil\n"
        Sleep 2000                ' KOnsole killen        If Not (hConsole = 0) Then
            FreeConsole            CloseHandle hConsole
        End If                End If
    End Sub
 Private Sub Populate(ByRef cells() As Byte)
        Dim lstart As Integer: lstart = MAX_ANFANG_LEBEN
    Dim i As Integer        Do While lstart > 0            For i = 1 To WELT_X - 2            lstart = lstart - RandomCells(cells, i, _
                CInt(Rnd() * (WELT_Y - 2)) + 1)        Next
            Loop
    End Sub
 Private Function RandomCells(ByRef cells() As Byte, x As Integer, _
    y As Integer) As Integer    If CInt(Rnd() * 1000) > 500 And cells(x, y) = 0 Then
        cells(x, y) = 1        RandomCells = 1
    Else        RandomCells = 0
    End IfEnd Function
 Private Function Sleep(ByVal millisec As Long)
  WaitForSingleObject -1, millisecEnd Function
 Private Sub Printf(ParamArray strx() As Variant)
        Dim str As Variant
    Dim line As String        For Each str In strx        line = line & Replace$(CStr(str), "\n", vbNewLine)
    Next        WriteConsole hConsole, ByVal line, Len(line), vbNull, ByVal 0&  End Sub  Private Function ConsoleCtrlHandler(ByVal lng As Long) As Long    ConsoleCtrlHandler = 1
End Function 
GeSHi 1.0.8.8
Kategorien:VB6, VBA Schlagwörter:

Game of Life mit C++

19. Januar 2011 1 Kommentar

Ja ich weis… Das hätte man auch mit C machen können…

C++
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
140
  #include <iostream>#include <stdlib.h>
#include <time.h>  using namespace std;  const int FELD_GROESSE = 22;const int MAX_ANFANG_LEBEN = 120;
const int GENERATION_STABIL = 10;const unsigned char LEBEWESEN_ZEICHEN[] = {' ', 219};
 void felder_bevoelkern(bool **, int );
int add_lifeform_in_cell(bool **, int , int );  void main(){
    bool **felder = new bool*[FELD_GROESSE];    for(int i=0; i < FELD_GROESSE; i++) 
        felder[i] = new bool[FELD_GROESSE];      // alle Felder nullen        for(int x = 0; x < FELD_GROESSE ; x++)    {
        for(int y= 0; y < FELD_GROESSE ; y++)            felder[x][y] = false;
    }      // "Innere" Felder mit random wert belegen    // radomizer initialisieren - die aktuelle zeit als seed
    srand((unsigned) time (NULL) ); //    felder_bevoelkern(felder, MAX_ANFANG_LEBEN);
     int izellen_anzahl = 0;
    int izellen_anzahl_alt = 0;    int igeneration = 0;
    int istabil = 0;    int inachbarn = 0;
     string line;
    line.resize (FELD_GROESSE);      while(istabil < GENERATION_STABIL)    {
         // Anzeigen
        system("cls"); // anzeige säubern        for(int y=0; y < FELD_GROESSE; y++) 
        {            // felder nach line "kopieren"
            for(int x=0; x < FELD_GROESSE; x++)                 line[x] = LEBEWESEN_ZEICHEN[ felder[x][y] ];
             cout << line.c_str() << "\n";
        }          // stabilität messen        if(izellen_anzahl > izellen_anzahl_alt-2 && 
            izellen_anzahl < izellen_anzahl_alt+2)            istabil++;
        else            istabil=0;
         izellen_anzahl_alt = izellen_anzahl;
        izellen_anzahl=0;          cout << "Generation: " << igeneration <<             " Zellen: " << izellen_anzahl_alt;
         igeneration++;
         for(int y=1; y < FELD_GROESSE-1; y++) 
        {            for(int x=1; x < FELD_GROESSE-1; x++) 
            {                   // nachbarn zählen
                inachbarn = 0;                inachbarn += felder[x-1][y];
                inachbarn += felder[x+1][y];                  inachbarn += felder[x][y-1];                inachbarn += felder[x][y+1];
                 inachbarn += felder[x-1][y-1];
                inachbarn += felder[x-1][y+1];                  inachbarn += felder[x+1][y-1];                inachbarn += felder[x+1][y+1];
                 // regel anwenden
                // nr. 1 - wiederbeleben                if(!felder[x][y] && inachbarn == 3) 
                    felder[x][y] = true;                // nr. 2 - an vereinsammung sterben
                else if(inachbarn < 2) felder[x][y] = false;                // nr. 3 - weiterleben
                else if(inachbarn > 1 && inachbarn < 4)                { /* passiert nichts */ }
                // nr. 4 - an überbevölkerung sterben                else if(inachbarn > 3) felder[x][y] = false;
                 // ein lb mehr
                izellen_anzahl += felder[x][y];            }
        }    }
     cout << "\nSeit " << GENERATION_STABIL << 
        " Generationen stabil";      int dummy=0;    cin >> dummy;
}  // Dies garantiert eine gute verteilung der Lebe... wesenvoid felder_bevoelkern(bool **felder, int maxleben) 
{    while(maxleben) 
    {        for(int y=1; y < FELD_GROESSE-1; y++) 
        {            maxleben -= add_lifeform_in_cell(felder, 
                (rand() % (FELD_GROESSE-1))+1, y);            if(maxleben == 0) break;
        }    }
}  int add_lifeform_in_cell(bool **felder, int x, int y){
    if(felder[x][y] == 0)     {
        felder[x][y] = ((rand() % 1000) + 1) > 500? true : false;        return 1;
    }      return 0;}
 
GeSHi 1.0.8.8
Kategorien:C++ Schlagwörter:

Game of Life mit C#

19. Januar 2011 Hinterlasse einen Kommentar

Ich hatte heute mal wirklich Lust darauf gehabt das “Game of Life” in verschiedenen Sprachen zu programmieren.
Angedacht hatte ich Assembler, C bzw. C++, C#, VB.NET, VB6, Turbo Pascal, Java… Vielleicht fallen mir noch andere Sprachen ein, dass ich auf die Schnelle erlernen kann…

Hier der Code für C# … Nicht optimiert…

C#
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
using System;
using System.Threading;  namespace GameOfLifeCSharp{
    class Program    {
         private const int FELD_GRÖSSE = 16;
        private const char ZEICHEN_WESEN = '#';        private const int STABILE_GENERATIONEN = 10;
         static void Main(string[] args)
        {            bool[,] felder = new bool [FELD_GRÖSSE, FELD_GRÖSSE];
             // Felder mit leben füllen
            Random randomNumber = new Random(DateTime.Now.Second);                        for (int x = 1; x < FELD_GRÖSSE - 1; x++)            {
                for (int y = 1; y < FELD_GRÖSSE - 1; y++)                    felder[x, y] = 
                        randomNumber.Next(0, 1000) > 500 ?                         true : false;
            }              int nachbarzähler = 0;            int alteanzahl = 0;
            int neueanzahl = 0;            int generationStabilität = 0;
            int generationen=0;              while (generationStabilität < STABILE_GENERATIONEN)            {
                for (int x = 1; x < FELD_GRÖSSE - 1; x++)                {
                    for (int y = 1; y < FELD_GRÖSSE - 1; y++)                    {
                        // Regeln                        // Nachbarn zählen
                        nachbarzähler += felder[x - 1, y] ? 1 : 0;                        nachbarzähler += felder[x + 1, y] ? 1 : 0;
                         nachbarzähler += felder[x, y + 1] ? 1 : 0;
                        nachbarzähler += felder[x, y - 1] ? 1 : 0;                          nachbarzähler += felder[x - 1, y + 1] ? 1 : 0;                        nachbarzähler += felder[x - 1, y - 1] ? 1 : 0;
                         nachbarzähler += felder[x + 1, y + 1] ? 1 : 0;
                        nachbarzähler += felder[x + 1, y - 1] ? 1 : 0;                          // Regel 1                        if (!felder[x, y] && nachbarzähler == 3) 
                            felder[x, y] = true;                        // Regel 2
                        else if (nachbarzähler < 2)                             felder[x, y] = false;
                        // Regel 3                        else if (nachbarzähler > 1 && 
                            nachbarzähler < 4) { }                        // Regel 4
                        else if (nachbarzähler > 3)                             felder[x, y] = false;
                         // anzahl zählen
                        if (felder[x, y]) neueanzahl++;                          nachbarzähler = 0;                    }
                }                  ShowUs(ref felder);                Console.WriteLine("Generation: {0}, Anzahl: {1}", 
                    generationen, neueanzahl);                  if (neueanzahl >= alteanzahl -2 &&                     neueanzahl <= alteanzahl +2)
                    generationStabilität++;                else
                    generationStabilität = 0;                  alteanzahl = neueanzahl;                neueanzahl = 0;
                 generationen++;
                 Thread.Sleep(100);
            }              Console.WriteLine(                "Seit {0} Generationen stabil\nEnter drücken um zu beenden", 
                generationStabilität);            Console.Read();
         }
         private static void ShowUs(ref bool[,] felder)
        {            Console.Clear();
             for (int x = 0; x < FELD_GRÖSSE; x++)
            {                for (int y = 0; y < FELD_GRÖSSE; y++)
                {                    if (felder[x, y])
                        Console.Write(ZEICHEN_WESEN);                    else
                        Console.Write(" ");                }
                Console.Write("\n");            }
        }    }
} 
GeSHi 1.0.8.8
Kategorien:.NET, C# Schlagwörter: