まるばつゲームの盤を作ります。クリックすると、○・×が現れる単純なものです。勝敗判定、勝負がついたらもう一度の機能を追加しました。
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.PictureBox c10;
private System.String c11;
public MainForm()
{
this.Text = "まるばつゲーム";
this.ClientSize = new System.Drawing.Size(300, 300);
c1 = new System.Windows.Forms.Label[9];
for (int i = 0; i < c1.Length; ++i)
{
c1[i] = new System.Windows.Forms.Label();
c1[i].Top = 5 + (i / 3) * 100;
c1[i].Left = 5 + (i % 3) * 100;
c1[i].Width = 90;
c1[i].Height = 90;
c1[i].Font = new System.Drawing.Font(System.Windows.Forms.Control.DefaultFont.Name, 50);
c1[i].TextAlign = System.Drawing.ContentAlignment.BottomCenter;
c1[i].Click += c1_Click;
this.Controls.Add(c1[i]);
}
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 o, System.EventArgs e)
{
System.Windows.Forms.Label x = (System.Windows.Forms.Label) o;
if (x.Text == "") {
x.Text = c11;
}
if (hantei()) {
System.Windows.Forms.MessageBox.Show(c11 + "の勝ち!");
mouichido();
} else {
toggle_c11();
}
}
private void toggle_c11()
{
if (c11 == "○")
{
c11 = "×";
}
else
{
c11 = "○";
}
}
private bool hantei()
{
if (c1[0].Text != "" && c1[0].Text == c1[1].Text && c1[1].Text == c1[2].Text)
{
return true;
}
if (c1[3].Text != "" && c1[3].Text == c1[4].Text && c1[4].Text == c1[5].Text)
{
return true;
}
if (c1[6].Text != "" && c1[6].Text == c1[7].Text && c1[7].Text == c1[8].Text)
{
return true;
}
if (c1[0].Text != "" && c1[0].Text == c1[3].Text && c1[3].Text == c1[6].Text)
{
return true;
}
if (c1[1].Text != "" && c1[1].Text == c1[4].Text && c1[4].Text == c1[7].Text)
{
return true;
}
if (c1[2].Text != "" && c1[2].Text == c1[5].Text && c1[5].Text == c1[8].Text)
{
return true;
}
if (c1[0].Text != "" && c1[0].Text == c1[4].Text && c1[4].Text == c1[8].Text)
{
return true;
}
if (c1[2].Text != "" && c1[2].Text == c1[4].Text && c1[4].Text == c1[6].Text)
{
return true;
}
else
{
return false;
}
}
private void mouichido()
{
for (int i = 0; i < c1.Length; ++i)
{
c1[i].Text = "";
}
c11 = "○";
}
}