I am planning to program a simple cellular automaton. It would be a Windows forms application and the rule set of the cell life would base onto the Conways Game of Life. I am a beginner with C# and not an expert of programming such complex-seeming applications - and I'd like to understand the cellular automaton program from the programmer's view and of course how to implement it. The things to program would be
1. Draw graphics on the form
2. Draw cells to born in random coordinates of the form
3. Define these rules to the program:
The program would look at the cell at position x,y and its neighbours will be checked. If the cell has 2 or 3 neighbors, it can continue living and won't die. If the cell has 3 neighbors and it was not already alive (it had a value of zero), it will now be born (have a value of 1). And if a cell only has 1 neighbor, it will die, but if it has 2 neighbors, it will continue living. If the cell has exactly 3 neighbors, it will be born if was not yet living. Finally, if the cell has more than 3 neighbors, it will die of overcrowding.
4. Insert a timer event that would start and continue generating the cellsI have this code now created:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Life1
{
public partial class Form1 : Form
{
private string string1 = "o";
int x, y;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (10); // Timer will tick evert second
timer.Enabled = true; // Enable the timer
timer.Start();
}
private void DrawCell()
{
[SIZE=2]// Definition of brushes, colors and fonts[/SIZE]
SolidBrush brush1;
brush1 = new SolidBrush(Color.Black);
Font font1;
font1 = new Font(this.Font, FontStyle.Regular);
[SIZE=2] // Random X-Y coordination creation[/SIZE]
Random r = new Random();
int maxX = this.Width; //This is the maximum X value of the pictureBox1
int maxY = this.Height; //This is the maximum Y value of the pictureBox1
x = r.Next(maxX);
y = r.Next(maxY);
[SIZE=2]// Let's draw the graphics[/SIZE]
Graphics g = pictureBox1.CreateGraphics();
g.DrawString(string1, font1, brush1, x, y);
g.Dispose();
}
void timer_Tick(object sender, EventArgs e)
{
DrawCell();
}
}
}
Any help considering this program is greatly appreciated. Thanks in advance! :)
Edited by Foucault, 05 November 2011 - 06:08 PM.


Sign In
Create Account

Back to top









