2010年11月5日 星期五

井字遊戲

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        System.Windows.Forms.Button[] Buttons;
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
          
            Buttons = new System.Windows.Forms.Button[9];
            //Buttons[i].Size=new Size(50,50);
            for (int i = 0; i < 9; ++i)
            {
             
                Buttons[i] = new Button();
                Buttons[i].Click += new System.EventHandler(Buttons_Click);
                this.Controls.Add(Buttons[i]);
                if (i <= 2)
                    Buttons[i].Location = new System.Drawing.Point(10 + i * 100, 10);
                else if (i > 2 && i <= 5)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 3) * 100, 40);
                else if (i > 5 && i <= 9)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 6) * 100, 70);
         
            }

        }
        public void Buttons_Click(object sender, EventArgs e)
        {
           int bno = 0;
           int i = 0;
          
                Buttons[i].Text = Buttons[bno].Text;
                    bno = i;
                    switch (bno)
                    {
                        case 0:
                            Buttons[0].Image = pictureBox2.Image;
                            break;
                        case 1:
                            Buttons[1].Image = pictureBox2.Image;
                            break;
                        case 2:
                            Buttons[2].Image = pictureBox2.Image;
                            break;
                        case 3:
                            Buttons[3].Image = pictureBox2.Image;
                            break;
                        case 4:
                            Buttons[4].Image = pictureBox2.Image;
                            break;
                        case 5:
                            Buttons[5].Image = pictureBox2.Image;
                            break;
                        case 6:
                            Buttons[6].Image = pictureBox2.Image;
                            break;
                        case 7:
                            Buttons[7].Image = pictureBox2.Image;
                            break;
                        case 8:
                            Buttons[8].Image = pictureBox2.Image;
                            break;
                    }
            }
        private void button1_Click(object sender, EventArgs e)
        {
            Random Rd = new Random();
            int Rdno = Rd.Next(9);
            label1.Text = "" + Rdno;
            Buttons[Rdno].Image =pictureBox1.Image;
        }
    }
}

沒有留言:

張貼留言