メモ帳でできるプログラミング C#

(サンプル)まるばつゲーム1

まるばつゲームの盤を作ります。クリックすると、○・×が現れる単純なものです。

実行結果

プログラム

public class TestApp
{
	public static void Main()
	{
		System.Windows.Forms.Application.Run(new MainForm());
	}
}

public class MainForm : System.Windows.Forms.Form
{
	private System.Windows.Forms.Label c1;
	private System.Windows.Forms.Label c2;
	private System.Windows.Forms.Label c3;
	private System.Windows.Forms.Label c4;
	private System.Windows.Forms.Label c5;
	private System.Windows.Forms.Label c6;
	private System.Windows.Forms.Label c7;
	private System.Windows.Forms.Label c8;
	private System.Windows.Forms.Label c9;
	private System.Windows.Forms.PictureBox c10;
	private System.String c11;

	public MainForm()
	{
		this.Text = "まるばつ";
		this.ClientSize = new System.Drawing.Size(300, 300);

		c1 = new System.Windows.Forms.Label();
		c1.Top = 5;
		c1.Left = 5;
		c1.Width = 90;
		c1.Height = 90;
		c1.Font = new System.Drawing.Font(System.Windows.Forms.Control.DefaultFont.Name, 50);
		c1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
		c1.Click += c1_Click;
		this.Controls.Add(c1);

		c2 = new System.Windows.Forms.Label();
		c2.Top = 5;
		c2.Left = 105;
		c2.Width = 90;
		c2.Height = 90;
		c2.Font = new System.Drawing.Font(System.Windows.Forms.Control.DefaultFont.Name, 50);
		c2.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
		c2.Click += c2_Click;
		this.Controls.Add(c2);

		c3 = new System.Windows.Forms.Label();
		c3.Top = 5;
		c3.Left = 205;
		c3.Width = 90;
		c3.Height = 90;
		c3.Font = new System.Drawing.Font(System.Windows.Forms.Control.DefaultFont.Name, 50);
		c3.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
		c3.Click += c3_Click;
		this.Controls.Add(c3);

		c4 = new System.Windows.Forms.Label();
		c4.Top = 105;
		c4.Left = 5;
		c4.Width = 90;
		c4.Height = 90;
		c4.Font = new System.Drawing.Font(System.Windows.Forms.Control.DefaultFont.Name, 50);
		c4.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
		c4.Click += c4_Click;
		this.Controls.Add(c4);

		c5 = new System.Windows.Forms.Label();
		c5.Top = 105;
		c5.Left = 105;
		c5.Width = 90;
		c5.Height = 90;
		c5.Font = new System.Drawing.Font(System.Windows.Forms.Control.DefaultFont.Name, 50);
		c5.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
		c5.Click += c5_Click;
		this.Controls.Add(c5);

		c6 = new System.Windows.Forms.Label();
		c6.Top = 105;
		c6.Left = 205;
		c6.Width = 90;
		c6.Height = 90;
		c6.Font = new System.Drawing.Font(System.Windows.Forms.Control.DefaultFont.Name, 50);
		c6.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
		c6.Click += c6_Click;
		this.Controls.Add(c6);

		c7 = new System.Windows.Forms.Label();
		c7.Top = 205;
		c7.Left = 5;
		c7.Width = 90;
		c7.Height = 90;
		c7.Font = new System.Drawing.Font(System.Windows.Forms.Control.DefaultFont.Name, 50);
		c7.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
		c7.Click += c7_Click;
		this.Controls.Add(c7);

		c8 = new System.Windows.Forms.Label();
		c8.Top = 205;
		c8.Left = 105;
		c8.Width = 90;
		c8.Height = 90;
		c8.Font = new System.Drawing.Font(System.Windows.Forms.Control.DefaultFont.Name, 50);
		c8.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
		c8.Click += c8_Click;
		this.Controls.Add(c8);

		c9 = new System.Windows.Forms.Label();
		c9.Top = 205;
		c9.Left = 205;
		c9.Width = 90;
		c9.Height = 90;
		c9.Font = new System.Drawing.Font(System.Windows.Forms.Control.DefaultFont.Name, 50);
		c9.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
		c9.Click += c9_Click;
		this.Controls.Add(c9);

		c10 = new System.Windows.Forms.PictureBox();
		c10.Width = 300;
		c10.Height = 300;
		System.Drawing.Bitmap canvas = new System.Drawing.Bitmap(c10.Width, c10.Height);
		System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(canvas);
		g.DrawLine(System.Drawing.Pens.Black, 0, 100, 300, 100);
		g.DrawLine(System.Drawing.Pens.Black, 0, 200, 300, 200);
		g.DrawLine(System.Drawing.Pens.Black, 100, 0, 100, 300);
		g.DrawLine(System.Drawing.Pens.Black, 200, 0, 200, 300);
		g.Dispose();
		c10.Image = canvas;
		this.Controls.Add(c10);

		c11 = "○";
	}

	private void c1_Click(object btn, System.EventArgs e)
	{
		if (c1.Text == "") {
			c1.Text = c11;
		}
		toggle_c11();
	}

	private void c2_Click(object btn, System.EventArgs e)
	{
		if (c2.Text == "") {
			c2.Text = c11;
		}
		toggle_c11();
	}

	private void c3_Click(object btn, System.EventArgs e)
	{
		if (c3.Text == "") {
			c3.Text = c11;
		}
		toggle_c11();
	}

	private void c4_Click(object btn, System.EventArgs e)
	{
		if (c4.Text == "") {
			c4.Text = c11;
		}
		toggle_c11();
	}

	private void c5_Click(object btn, System.EventArgs e)
	{
		if (c5.Text == "") {
			c5.Text = c11;
		}
		toggle_c11();
	}

	private void c6_Click(object btn, System.EventArgs e)
	{
		if (c6.Text == "") {
			c6.Text = c11;
		}
		toggle_c11();
	}

	private void c7_Click(object btn, System.EventArgs e)
	{
		if (c7.Text == "") {
			c7.Text = c11;
		}
		toggle_c11();
	}

	private void c8_Click(object btn, System.EventArgs e)
	{
		if (c8.Text == "") {
			c8.Text = c11;
		}
		toggle_c11();
	}

	private void c9_Click(object btn, System.EventArgs e)
	{
		if (c9.Text == "") {
			c9.Text = c11;
		}
		toggle_c11();
	}

	private void toggle_c11()
	{
		if (c11 == "○") {
			c11 = "×";
		} else {
			c11 = "○";
		}
	}
}

プログラムが長くて疲れましたか?

プログラムに繰り返しが多くて、とても無駄を感じたとしたら、それは正解です!

次はこのプログラムを短くします。