代码下载:https://download.csdn.net/download/caozhy/11449907
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 Q770812
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int[,] area;
private int step = 1;
private void button1_Click(object sender, EventArgs e)
{
//step++;
for (int k = 0; k < step; k++)
{
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 15; j++)
{
if (area[i, j] == 4) area[i, j] = 1;
}
}
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 15; j++)
{
if (area[i, j] == 0)
{
bool pt = false;
for (int ii = (i > 0 ? i - 1 : 0); ii <= (i < 29 ? i + 1 : 29); ii++)
for (int jj = (j > 0 ? j - 1 : 0); jj <= (j < 14 ? j + 1 : 14); jj++)
{
if ((ii == i && jj != j) || (ii != i && jj == j))
if (area[ii, jj] == 1) pt = true;
}
area[i, j] = pt ? 4 : area[i, j];
}
}
}
}
pictureBox1.Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (area == null) return;
var g = e.Graphics;
g.FillRectangle(Brushes.White, 0f, 0f, pictureBox1.Width, pictureBox1.Height);
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 15; j++)
{
Brush br = new SolidBrush(Color.FromArgb(110, 136, 199));
switch (area[i, j])
{
case 0:
br = new SolidBrush(Color.FromArgb(219, 213, 213));
break;
case 1:
br = new SolidBrush(Color.FromArgb(202, 190, 180));
break;
case 3:
br = new SolidBrush(Color.FromArgb(134, 134, 122));
break;
}
g.FillRectangle(br, 10.0f + i * 20.0f, 10.0f + j * 20.0f, 18.0f, 18.0f);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
area = new int[30, 15];
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 15; j++)
area[i, j] = 0;
}
for (int i = 3; i <= 11; i++)
{
area[3, i] = 3;
area[4, i] = 3;
}
for (int i = 4; i <= 14; i++)
{
area[13, i] = 3;
area[14, i] = 3;
}
for (int i = 0; i <= 6; i++)
{
area[22, i] = 3;
area[23, i] = 3;
}
for (int i = 0; i <= 6; i++)
{
area[22, i] = 3;
area[23, i] = 3;
}
for (int i = 5; i <= 6; i++)
{
area[24, i] = 3;
area[25, i] = 3;
area[26, i] = 3;
}
area[8, 7] = 1;
step = 1;
pictureBox1.Refresh();
}
private void Form1_Load(object sender, EventArgs e)
{
button2.PerformClick();
}
}
}
namespace Q770812
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.White;
this.pictureBox1.Location = new System.Drawing.Point(30, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(616, 319);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
//
// button1
//
this.button1.Location = new System.Drawing.Point(431, 354);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(90, 25);
this.button1.TabIndex = 1;
this.button1.Text = "next";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(556, 354);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(90, 25);
this.button2.TabIndex = 2;
this.button2.Text = "reset";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(673, 391);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}