Add Game logic
This commit is contained in:
parent
0a25473a9a
commit
3cef3e10b7
3 changed files with 143 additions and 14 deletions
28
Game_of_Life/MainForm1.Designer.cs
generated
28
Game_of_Life/MainForm1.Designer.cs
generated
|
|
@ -37,6 +37,8 @@ partial class MainForm1
|
||||||
this.buttonRepopulate = new System.Windows.Forms.Button();
|
this.buttonRepopulate = new System.Windows.Forms.Button();
|
||||||
this.zoomInButton = new System.Windows.Forms.Button();
|
this.zoomInButton = new System.Windows.Forms.Button();
|
||||||
this.zoomOutButton = new System.Windows.Forms.Button();
|
this.zoomOutButton = new System.Windows.Forms.Button();
|
||||||
|
this.buttonTick = new System.Windows.Forms.Button();
|
||||||
|
this.buttonClear = new System.Windows.Forms.Button();
|
||||||
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
|
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
|
||||||
((System.ComponentModel.ISupportInitialize)(this.scalingSlider)).BeginInit();
|
((System.ComponentModel.ISupportInitialize)(this.scalingSlider)).BeginInit();
|
||||||
this.SuspendLayout();
|
this.SuspendLayout();
|
||||||
|
|
@ -108,12 +110,36 @@ partial class MainForm1
|
||||||
this.zoomOutButton.UseVisualStyleBackColor = true;
|
this.zoomOutButton.UseVisualStyleBackColor = true;
|
||||||
this.zoomOutButton.Click += new System.EventHandler(this.zoomOutButton_Click);
|
this.zoomOutButton.Click += new System.EventHandler(this.zoomOutButton_Click);
|
||||||
//
|
//
|
||||||
|
// buttonTick
|
||||||
|
//
|
||||||
|
this.buttonTick.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
|
||||||
|
this.buttonTick.Location = new System.Drawing.Point(701, 41);
|
||||||
|
this.buttonTick.Name = "buttonTick";
|
||||||
|
this.buttonTick.Size = new System.Drawing.Size(87, 23);
|
||||||
|
this.buttonTick.TabIndex = 6;
|
||||||
|
this.buttonTick.Text = "TICK";
|
||||||
|
this.buttonTick.UseVisualStyleBackColor = true;
|
||||||
|
this.buttonTick.Click += new System.EventHandler(this.buttonTick_Click);
|
||||||
|
//
|
||||||
|
// buttonClear
|
||||||
|
//
|
||||||
|
this.buttonClear.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
|
||||||
|
this.buttonClear.Location = new System.Drawing.Point(608, 12);
|
||||||
|
this.buttonClear.Name = "buttonClear";
|
||||||
|
this.buttonClear.Size = new System.Drawing.Size(87, 23);
|
||||||
|
this.buttonClear.TabIndex = 7;
|
||||||
|
this.buttonClear.Text = "CLEAR";
|
||||||
|
this.buttonClear.UseVisualStyleBackColor = true;
|
||||||
|
this.buttonClear.Click += new System.EventHandler(this.buttonClear_Click);
|
||||||
|
//
|
||||||
// MainForm1
|
// MainForm1
|
||||||
//
|
//
|
||||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||||
this.BackColor = System.Drawing.Color.Black;
|
this.BackColor = System.Drawing.Color.Black;
|
||||||
this.ClientSize = new System.Drawing.Size(800, 450);
|
this.ClientSize = new System.Drawing.Size(800, 450);
|
||||||
|
this.Controls.Add(this.buttonClear);
|
||||||
|
this.Controls.Add(this.buttonTick);
|
||||||
this.Controls.Add(this.zoomOutButton);
|
this.Controls.Add(this.zoomOutButton);
|
||||||
this.Controls.Add(this.zoomInButton);
|
this.Controls.Add(this.zoomInButton);
|
||||||
this.Controls.Add(this.buttonRepopulate);
|
this.Controls.Add(this.buttonRepopulate);
|
||||||
|
|
@ -137,4 +163,6 @@ partial class MainForm1
|
||||||
private Button buttonRepopulate;
|
private Button buttonRepopulate;
|
||||||
private Button zoomInButton;
|
private Button zoomInButton;
|
||||||
private Button zoomOutButton;
|
private Button zoomOutButton;
|
||||||
|
private Button buttonTick;
|
||||||
|
private Button buttonClear;
|
||||||
}
|
}
|
||||||
|
|
@ -180,4 +180,16 @@ public partial class MainForm1 : Form
|
||||||
scalingSlider.Value = scalingAbsolute;
|
scalingSlider.Value = scalingAbsolute;
|
||||||
pixelmap.setScalingAbsolute(scalingAbsolute);
|
pixelmap.setScalingAbsolute(scalingAbsolute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void buttonTick_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
pixelmap.Tick();
|
||||||
|
pictureBox.Refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buttonClear_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
pixelmap.clear();
|
||||||
|
pictureBox.Refresh();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,19 +11,14 @@ public class Pixelmap
|
||||||
private int viewHeight;
|
private int viewHeight;
|
||||||
private int scalingAbsolute;
|
private int scalingAbsolute;
|
||||||
private Color[] colors = {Color.Black, Color.White, Color.OrangeRed};
|
private Color[] colors = {Color.Black, Color.White, Color.OrangeRed};
|
||||||
|
// gameWindow = [leftBound, rightBound, upperBound, lowerBound]
|
||||||
|
private int[] gameWindow = {0, 0, 0, 0};
|
||||||
|
|
||||||
public Pixelmap(int height, int width, int scalingFactor)
|
public Pixelmap(int height, int width, int scalingFactor)
|
||||||
{
|
{
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.width = width;
|
this.width = width;
|
||||||
map = new int[width, height];
|
map = new int[width, height];
|
||||||
for (int w = 0; w < width; w++)
|
|
||||||
{
|
|
||||||
for (int h = 0; h < height; h++)
|
|
||||||
{
|
|
||||||
map[w, h] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.scalingFactor = scalingFactor;
|
this.scalingFactor = scalingFactor;
|
||||||
this.midPoint = new int[] { width / 2, height / 2 };
|
this.midPoint = new int[] { width / 2, height / 2 };
|
||||||
|
|
@ -43,19 +38,46 @@ public class Pixelmap
|
||||||
public void clear()
|
public void clear()
|
||||||
{
|
{
|
||||||
map = new int[width, height];
|
map = new int[width, height];
|
||||||
for (int w = 0; w < width; w++)
|
gameWindow = new int[] {0, 0, 0, 0};
|
||||||
{
|
|
||||||
for (int h = 0; h < height; h++)
|
|
||||||
{
|
|
||||||
map[w, h] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPixel(int x, int y, int val)
|
public void setPixel(int x, int y, int val)
|
||||||
{
|
{
|
||||||
val = val >= 3 ? 0 : val;
|
val = val >= 3 ? 0 : val;
|
||||||
map[x, y] = val;
|
map[x, y] = val;
|
||||||
|
|
||||||
|
if (x == 0)
|
||||||
|
{
|
||||||
|
x = 1;
|
||||||
|
} else if (x == width)
|
||||||
|
{
|
||||||
|
x--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y == 0)
|
||||||
|
{
|
||||||
|
y = 1;
|
||||||
|
} else if (y == height)
|
||||||
|
{
|
||||||
|
y--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x < midPoint[0] - gameWindow[0])
|
||||||
|
{
|
||||||
|
gameWindow[0] = midPoint[0]-x;
|
||||||
|
}
|
||||||
|
if (x > midPoint[0] + gameWindow[1])
|
||||||
|
{
|
||||||
|
gameWindow[1] = midPoint[0] - x;
|
||||||
|
}
|
||||||
|
if (y < midPoint[1] - gameWindow[2])
|
||||||
|
{
|
||||||
|
gameWindow[2] = midPoint[1] - y;
|
||||||
|
}
|
||||||
|
if (y > midPoint[1] + gameWindow[3])
|
||||||
|
{
|
||||||
|
gameWindow[3] = midPoint[1] - y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cyclePixel(int x, int y)
|
public void cyclePixel(int x, int y)
|
||||||
|
|
@ -205,4 +227,71 @@ public class Pixelmap
|
||||||
}
|
}
|
||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Tick()
|
||||||
|
{
|
||||||
|
int[,] mapBuffer = new int[width, height];
|
||||||
|
|
||||||
|
// for (int w = midPoint[0]-gameWindow[0]; w < midPoint[0] + gameWindow[1]; w++)
|
||||||
|
for (int w = 1; w < width - 1; w++)
|
||||||
|
{
|
||||||
|
// for (int h = midPoint[1] - gameWindow[2]; h < midPoint[1] + gameWindow[3]; h++)
|
||||||
|
for (int h = 1; h < height - 1; h++)
|
||||||
|
{
|
||||||
|
int neighbors = 0;
|
||||||
|
|
||||||
|
// iterate through all neighbors
|
||||||
|
for (int x = 0; x < 3; x++)
|
||||||
|
{
|
||||||
|
for (int y = 0; y < 3; y++)
|
||||||
|
{
|
||||||
|
if (map[(w - 1) + x, (h - 1) + y] != 0)
|
||||||
|
{
|
||||||
|
neighbors += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (map[w, h] == 0)
|
||||||
|
{
|
||||||
|
if (neighbors == 3)
|
||||||
|
{
|
||||||
|
mapBuffer[w, h] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// since the current entity is also being counted...
|
||||||
|
neighbors--;
|
||||||
|
|
||||||
|
switch (neighbors)
|
||||||
|
{
|
||||||
|
// dies
|
||||||
|
case < 2:
|
||||||
|
mapBuffer[w, h] = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
// lives
|
||||||
|
case 2:
|
||||||
|
mapBuffer[w, h] = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
// lives
|
||||||
|
case 3:
|
||||||
|
mapBuffer[w, h] = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
// dies
|
||||||
|
case > 3:
|
||||||
|
mapBuffer[w, h] = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
map = mapBuffer;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue