Autor Beitrag
PixxPaddy
Hält's aus hier
Beiträge: 1



BeitragVerfasst: Fr 06.01.17 18:16 
Guten Tag bin neu hier und habe eine Frage ich möchte das Conways game of life spiel von einer dynamischen oder Consolenerzeugten in eine feste Ansicht auf einem 6x6 Textboxen basierenden Form wiedergeben
ich weis nur nicht wie ich die textboxen anspreche weil mein beim dynamischen jeder noch eienn x und y wert bekommt.
Code:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
using System;
using System.Threading.Tasks;

namespace Life
{
    public class LifeSimulation
    {
        private bool[,] world;
        private bool[,] nextGeneration;
        private Task processTask;

        public LifeSimulation(int size)
        {
            if (size < 0throw new ArgumentOutOfRangeException("Size must be greater than zero");
            this.Size = size;
            world = new bool[size, size];
            nextGeneration = new bool[size, size];
        }

        public int Size { get; private set; }
        public int Generation { get; private set; }

        public Action<bool[,]> NextGenerationCompleted;

        public bool this[int x, int y]
        {
            get { return this.world[x, y]; }
            set { this.world[x, y] = value; }
        }

        public bool ToggleCell(int x, int y)
        {
            bool currentValue = this.world[x, y];
            return this.world[x, y] = !currentValue;
        }

        public void Update()
        {
            if (this.processTask != null && this.processTask.IsCompleted)
            {
                // when a generation has completed
                // now flip the back buffer so we can start processing on the next generation
                var flip = this.nextGeneration;
                this.nextGeneration = this.world;
                this.world = flip;
                Generation++;

                // begin the next generation's processing asynchronously
                this.processTask = this.ProcessGeneration();

                if (NextGenerationCompleted != null) NextGenerationCompleted(this.world);
            }
        }

        public void BeginGeneration()
        {
            if (this.processTask == null || (this.processTask != null && this.processTask.IsCompleted))
            {
                // only begin the generation if the previous process was completed
                this.processTask = this.ProcessGeneration();
            }
        }

        public void Wait()
        {
            if (this.processTask != null)
            {
                this.processTask.Wait();
            }
        }

        private Task ProcessGeneration()
        {
            return Task.Factory.StartNew(() =>
            {
                Parallel.For(0, Size, x =>
                {
                    Parallel.For(0, Size, y =>
                    {
                        int numberOfNeighbors = IsNeighborAlive(world, Size, x, y, -10)
                            + IsNeighborAlive(world, Size, x, y, -11)
                            + IsNeighborAlive(world, Size, x, y, 01)
                            + IsNeighborAlive(world, Size, x, y, 11)
                            + IsNeighborAlive(world, Size, x, y, 10)
                            + IsNeighborAlive(world, Size, x, y, 1, -1)
                            + IsNeighborAlive(world, Size, x, y, 0, -1)
                            + IsNeighborAlive(world, Size, x, y, -1, -1);

                        bool shouldLive = false;
                        bool isAlive = world[x, y];

                        if (isAlive && (numberOfNeighbors == 2 || numberOfNeighbors == 3))
                        {
                            shouldLive = true;
                        }
                        else if (!isAlive && numberOfNeighbors == 3// zombification
                        {
                            shouldLive = true;
                        }

                        nextGeneration[x, y] = shouldLive;

                    });
                });
            });
        }

        private static int IsNeighborAlive(bool[,] world, int size, int x, int y, int offsetx, int offsety)
        {
            int result = 0;

            int proposedOffsetX = x + offsetx;
            int proposedOffsetY = y + offsety;
            bool outOfBounds = proposedOffsetX < 0 || proposedOffsetX >= size | proposedOffsetY < 0 || proposedOffsetY >= size;
            if (!outOfBounds)
            {
                result = world[x + offsetx, y + offsety] ? 1 : 0;
            }
            return result;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            LifeSimulation sim = new LifeSimulation(10);

            // initialize with a blinker
            sim.ToggleCell(55);
            sim.ToggleCell(56);
            sim.ToggleCell(57);

            sim.BeginGeneration();
            sim.Wait();
            OutputBoard(sim);

            sim.Update();
            sim.Wait();
            OutputBoard(sim);

            sim.Update();
            sim.Wait();
            OutputBoard(sim);

            Console.ReadKey();
        }

        private static void OutputBoard(LifeSimulation sim)
        {
            var line = new String('-', sim.Size);
            Console.WriteLine(line);

            for (int y = 0; y < sim.Size; y++)
            {
                for (int x = 0; x < sim.Size; x++)
                {
                    Console.Write(sim[x, y] ? "1" : "0");
                }

                Console.WriteLine();
            }
        }
    }
}


hat jemand eine Idee?

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Moderiert von user profile iconTh69: Topic aus C# - Die Sprache verschoben am Sa 07.01.2017 um 10:08
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 06.01.17 22:33 
Hallo und :welcome:!

Da die Größe der Simulation ja variabel ist, kannst Du eigentlich nur die TextBoxen dynamisch erzeugen. Zum Beispiel so:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
public partial class Form1 : Form
    {
        private TextBox[,] boxArray;

        public Form1()
        {
            InitializeComponent();
            CreateBoxes(5);
        }

        private void CreateBoxes(int size)
        {            
            boxArray = new TextBox[size, size];

            for(int x = 0; x < size; x++)
                for(int y = 0; y < size; y++)
                {
                    boxArray[x, y] = new TextBox();
                    boxArray[x, y].Parent = this;
                    boxArray[x, y].Left = boxArray[x, y].Width * x;
                    boxArray[x, y].Top = boxArray[x, y].Height * y;
                }
        }
    }

So kannst Du über ein 2D-Array auf die TextBoxen zugreifen.

Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".