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:
| namespace Game_of_Life_C { public partial class Form1 : Form { bool[,] LiveArea; int environment = 12; bool isDrawing = false; bool drawColor;
public Form1() { InitializeComponent(); IniLiveArea(); } private void IniLiveArea() { LiveArea = new bool[160, 116]; LiveArea[23, 12] = true; LiveArea[24, 13] = true; LiveArea[23, 14] = true; guiField.Size = new Size(LiveArea.GetLength(0) * environment, LiveArea.GetLength(1) * environment); }
private void pictureBox2_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Brush alive = Brushes.Violet; Brush dead = Brushes.black;
for (int i = 0; i < LiveArea.GetLength(0); i++) { for (int I = 0; I < LiveArea.GetLength(1); I++) { Brush b; if (LiveArea[i, I]) b = alive; else b = dead; g.FillRectangle(b, i * environment, I * environment, environment, environment); } } }
private void button3_Click(object sender, EventArgs e) { Einzelschritt(); } private void Einzelschritt() { LiveArea = NextGeneration(LiveArea); guiField.Invalidate(); } private bool[,] NextGeneration(bool[,] feld) { bool[,] nyplan = new bool[feld.GetLength(0), feld.GetLength(1)]; for (int i = 0; i < feld.GetLength(0); i++) { for (int y = 0; y < feld.GetLength(1); y++) { int neighbors = getAliveNeighbors(feld, i, y); if (feld[i, y]) {
nyplan[i, y] = (neighbors >= 2 && neighbors <= 3); } else
{ nyplan[i, y] = neighbors == 3;
} } } return nyplan; } private int getAliveNeighbors(bool[,] plan, int x, int y) { int neighbors = 0; int maxX = plan.GetLength(0); int maxY = plan.GetLength(1);
for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { int cx = x + dx; int cy = y + dy; if (cx == x && cy == y) continue; if (cx < 0 || cx >= maxX) continue; if (cy < 0 || cy >= maxY) continue;
if (plan[cx, cy]) { neighbors++; } } } return neighbors; }
private void guiField_MouseDown(object sender, MouseEventArgs e) { int x = e.X / environment; int y = e.Y / environment; isDrawing = true; drawColor = !LiveArea[x, y]; guiField.Invalidate(); }
private void guiField_MouseMove(object sender, MouseEventArgs e) { if (!isDrawing) return; int x = e.X / environment; int y = e.Y / environment; if (x < 0 || x >= LiveArea.GetLength(0)) return; if (y < 0 || y >= LiveArea.GetLength(1)) return;
LiveArea[x, y] = drawColor; guiField.Invalidate(); }
private void guiField_MouseUp(object sender, MouseEventArgs e) { isDrawing = false; }
private void button4_Click(object sender, EventArgs e) { if (!stepTimer.Enabled) { stepTimer.Enabled = true; stepTimer.Start(); btnStart.Text = "Stopp"; } else { stepTimer.Enabled = false; stepTimer.Stop(); btnStart.Text = "Start"; } }
private void timer1_Tick(object sender, EventArgs e) { Einzelschritt(); }
private void trackBar1_ValueChanged(object sender, EventArgs e) { stepTimer.Interval = speedBar.Value; } } } |