2010年12月30日 星期四

遊戲 XNA期末

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GameObject CD1; // CD宣告
        GameObject CD2;
        GameObject CD3;
        GameObject CD4;
        GameObject CD5;
        GameObject CD6;
        GameObject CD7;
        GameObject CD8;
        GameObject CD9;
        GameObject myplane;//飛機宣告
        int score;//分數宣告
        SpriteFont font;//字體
        Vector2 scoreDrawPoint = new Vector2(0.1f, 0.1f);//SCORE放的地方
        
        
       
       // SpriteFont font;
        Texture2D backgroundTexture;//背景
        Rectangle viewportRect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
           
        }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            base.Initialize();
        }
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
               
            // TODO: use this.Content to load your game content here
            Texture2D texture = Content.Load<Texture2D>("CD2");//載入CD圖形
            Texture2D texture2 = Content.Load<Texture2D>("plane");//載入飛機圖形
            font = Content.Load<SpriteFont>("GameFont");
            backgroundTexture =Content.Load<Texture2D>("images");//背景圖
            myplane = new GameObject(texture2, new Vector2(700f,500f),
                                          new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            //宣告飛機產生(圖片,位置,範圍)
            CD1 = new GameObject(texture,new Vector2(0f, 0f),
                                          new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD1.velocity = new Vector2(-9, 2); // 設位移速度
          
            CD2 = new GameObject(texture,new Vector2(70f, 70f),
                                      new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD2.velocity = new Vector2(1, -4);
            CD3 = new GameObject(texture,new Vector2(140f, 140f),
                                     new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD3.velocity = new Vector2(-2, 4);
            CD4 = new GameObject(texture,new Vector2(210f, 210f),
                                     new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD4.velocity = new Vector2(-3, -8);
            CD5 = new GameObject(texture,new Vector2(300f, 300f),
                                     new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD5.velocity = new Vector2(4,- 3);
            CD6 = new GameObject(texture, new Vector2(370f, 370f),
                                     new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD6.velocity = new Vector2(-5, 3);
            CD7 = new GameObject(texture, new Vector2(460f, 460f),
                                     new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD7.velocity = new Vector2(6, -5);
            CD8 = new GameObject(texture, new Vector2(520f, 520f),
                                    new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD8.velocity = new Vector2(-7, -9);
            CD9 = new GameObject(texture, new Vector2(480f, 80f),
                                     new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            CD9.velocity = new Vector2(8, -7);
            viewportRect = new Rectangle(0, 0,
                graphics.GraphicsDevice.Viewport.Width,
                graphics.GraphicsDevice.Viewport.Height);

        }
        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
           
          
            // TODO: Add your update logic here
            CD1.Move(); //叫出用於移動的指令
            CD2.Move();
            CD3.Move();
            CD4.Move();
            CD5.Move();
            CD6.Move();
            CD7.Move();
            CD8.Move();
            CD9.Move();
            myplane.Move();

            if (CD1.Collides(CD2))//當CD1碰到CD2時...移動速度方向對調
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD2.velocity;
                CD2.velocity = tempVelocity;
            }
            if (CD1.Collides(CD3))//同上
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD3.velocity;
                CD3.velocity = tempVelocity;
            }
            if (CD1.Collides(CD4))
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD4.velocity;
                CD4.velocity = tempVelocity;
            }
            if (CD1.Collides(CD5))
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD5.velocity;
                CD5.velocity = tempVelocity;
            }
            if (CD1.Collides(CD6))
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD6.velocity;
                CD6.velocity = tempVelocity;
            }
            if (CD1.Collides(CD7))
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD7.velocity;
                CD7.velocity = tempVelocity;
            }
            if (CD1.Collides(CD8))
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD1.Collides(CD9))
            {
                Vector2 tempVelocity = CD1.velocity;
                CD1.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
           
            if (CD2.Collides(CD3))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD3.velocity;
                CD3.velocity = tempVelocity;
            }
            if (CD2.Collides(CD4))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD4.velocity;
                CD4.velocity = tempVelocity;
            }
            if (CD2.Collides(CD5))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD5.velocity;
                CD5.velocity = tempVelocity;
            }
            if (CD2.Collides(CD6))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD6.velocity;
                CD6.velocity = tempVelocity;
            }
            if (CD2.Collides(CD7))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD7.velocity;
                CD7.velocity = tempVelocity;
            }
            if (CD2.Collides(CD8))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD2.Collides(CD9))
            {
                Vector2 tempVelocity = CD2.velocity;
                CD2.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
           
           
            if (CD3.Collides(CD4))
            {
                Vector2 tempVelocity = CD3.velocity;
                CD3.velocity = CD4.velocity;
                CD4.velocity = tempVelocity;
            }
            if (CD3.Collides(CD5))
            {
                Vector2 tempVelocity = CD3.velocity;
                CD3.velocity = CD5.velocity;
                CD5.velocity = tempVelocity;
            }
            if (CD3.Collides(CD6))
            {
                Vector2 tempVelocity = CD3.velocity;
                CD3.velocity = CD6.velocity;
                CD6.velocity = tempVelocity;
            }
            if (CD3.Collides(CD7))
            {
                Vector2 tempVelocity = CD3.velocity;
                CD3.velocity = CD7.velocity;
                CD7.velocity = tempVelocity;
            }
            if (CD3.Collides(CD8))
            {
                Vector2 tempVelocity = CD3.velocity;
                CD3.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD3.Collides(CD9))
            {
                Vector2 tempVelocity = CD3.velocity;
                CD3.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
           
          
           
            if (CD4.Collides(CD5))
            {
                Vector2 tempVelocity = CD4.velocity;
                CD4.velocity = CD5.velocity;
                CD5.velocity = tempVelocity;
            }
            if (CD4.Collides(CD6))
            {
                Vector2 tempVelocity = CD4.velocity;
                CD4.velocity = CD6.velocity;
                CD6.velocity = tempVelocity;
            }
            if (CD4.Collides(CD7))
            {
                Vector2 tempVelocity = CD4.velocity;
                CD4.velocity = CD7.velocity;
                CD7.velocity = tempVelocity;
            }
            if (CD4.Collides(CD8))
            {
                Vector2 tempVelocity = CD4.velocity;
                CD4.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD4.Collides(CD9))
            {
                Vector2 tempVelocity = CD4.velocity;
                CD4.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }

            if (CD5.Collides(CD6))
            {
                Vector2 tempVelocity = CD5.velocity;
                CD5.velocity = CD6.velocity;
                CD6.velocity = tempVelocity;
            }
            if (CD5.Collides(CD7))
            {
                Vector2 tempVelocity = CD5.velocity;
                CD5.velocity = CD7.velocity;
                CD7.velocity = tempVelocity;
            }
            if (CD5.Collides(CD8))
            {
                Vector2 tempVelocity = CD5.velocity;
                CD5.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD5.Collides(CD9))
            {
                Vector2 tempVelocity = CD5.velocity;
                CD5.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
            if (CD6.Collides(CD7))
            {
                Vector2 tempVelocity = CD6.velocity;
                CD6.velocity = CD7.velocity;
                CD7.velocity = tempVelocity;
            }
            if (CD6.Collides(CD8))
            {
                Vector2 tempVelocity = CD6.velocity;
                CD6.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD6.Collides(CD9))
            {
                Vector2 tempVelocity = CD6.velocity;
                CD6.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
            if (CD7.Collides(CD8))
            {
                Vector2 tempVelocity = CD7.velocity;
                CD7.velocity = CD8.velocity;
                CD8.velocity = tempVelocity;
            }
            if (CD7.Collides(CD9))
            {
                Vector2 tempVelocity = CD7.velocity;
                CD7.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
            if (CD8.Collides(CD9))
            {
                Vector2 tempVelocity = CD8.velocity;
                CD8.velocity = CD9.velocity;
                CD9.velocity = tempVelocity;
            }
            if (myplane.Collides(CD1))//飛機碰到CD1時..CD1速度方向變反方向..同時分數+1
            {
                CD1.velocity *= -1;
                score += 1;
               
            }
            if (myplane.Collides(CD2))
            {
                CD2.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD3))
            {
                CD3.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD4))
            {
                CD4.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD5))
            {
                CD5.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD6))
            {
                CD6.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD7))
            {
                CD7.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD8))
            {
                CD8.velocity *= -1;
                score += 1;
            }
            if (myplane.Collides(CD9))
            {
                CD9.velocity *= -1;
                score += 1;
            }
            KeyboardState keyboardState = Keyboard.GetState();
            if (keyboardState.IsKeyDown(Keys.Left))//鍵盤案Left時...飛機位置往x軸-10.0f前進
            {
                myplane.position.X -= 5.0f;
            }
            if (keyboardState.IsKeyDown(Keys.Right))
            {
                myplane.position.X += 5.0f;
            }
            if (keyboardState.IsKeyDown(Keys.Up))
            {
                myplane.position.Y -= 5.0f;
            }
            if (keyboardState.IsKeyDown(Keys.Down))
            {
                myplane.position.Y += 5.0f;
            }
           
          
            base.Update(gameTime);
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            spriteBatch.Draw(backgroundTexture, viewportRect,
                Color.White);
            spriteBatch.Draw(CD1.texture, CD1.position, Color.White);//圖.位置.顏色
            spriteBatch.Draw(CD2.texture, CD2.position, Color.White);
            spriteBatch.Draw(CD3.texture, CD3.position, Color.White);
            spriteBatch.Draw(CD4.texture, CD4.position, Color.White);
            spriteBatch.Draw(CD5.texture, CD5.position, Color.White);
            spriteBatch.Draw(CD6.texture, CD6.position, Color.White);
            spriteBatch.Draw(CD7.texture, CD7.position, Color.White);
            spriteBatch.Draw(CD8.texture, CD8.position, Color.White);
            spriteBatch.Draw(CD9.texture, CD9.position, Color.White);
            spriteBatch.Draw(myplane.texture, myplane.position, Color.White);
            spriteBatch.DrawString(font,
                "You're already dead " + score.ToString()+"  times",
                new Vector2(scoreDrawPoint.X * viewportRect.Width,
                scoreDrawPoint.Y * viewportRect.Height),
                Color.Red);//字體.文字.字元轉換.位置.顏色
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

----------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.Text;

namespace WindowsGame1
{
    class GameObject
    {
       
        public Texture2D texture;  // 圖
        public Vector2 position;   // 圖 的位置
        private Vector2 screenSize; // 視窗寬高
        public bool alive;
        public float scale = 1.0f;
      
        public Vector2 velocity = Vector2.Zero;   // 圖 的位移速度
        public GameObject(Texture2D texture, Vector2 position, Vector2 screenSize)
        {
            this.texture = texture;
            this.position = position;
            this.screenSize = screenSize;
            alive = false;
           
        }
     
         // 移動
        public void Move()
        {
            // 圖右邊 碰到 視窗右邊了
            if (position.X + texture.Width + velocity.X > screenSize.X)
                velocity.X = -velocity.X;
            // 圖下面 碰到 視窗底邊了
            if (position.Y + texture.Height + velocity.Y > screenSize.Y)
                velocity.Y = -velocity.Y;
            // 圖左邊 碰到 視窗左邊了
            if (position.X + velocity.X < 0)
                velocity.X = -velocity.X;
            // 圖上面 碰到 視窗上邊了
            if (position.Y + velocity.Y < 0)
                velocity.Y = -velocity.Y;
            position += velocity;
        }
        public bool Collides(GameObject other)
        {
            // 檢查有沒有碰撞
            return (this.position.X + texture.Width > other.position.X &&
                    this.position.X < other.position.X + other.texture.Width &&
                    this.position.Y + texture.Height > other.position.Y &&
                    this.position.Y < other.position.Y + other.texture.Height);
        }
    }
  
      
           
        }
   

2010年11月18日 星期四

2010/11/19

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
    {
        //宣告
        int i,j;
       
        public Form1()
        {
            InitializeComponent();
            i = 0;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            button1.Enabled = false;//按鈕不能按
            button2.Enabled = false;
            button3.Enabled = false;
            i++;
            i = i % 5;//求餘數
            label1.Text=Convert.ToString(i);
            j++;
            j = j % 5;
            switch (j)
            {
                case 0:
                    button1.Enabled = true;//按鈕能按
                    break;
                case 1:
                    button2.Enabled = true;
                    break;
                case 2:
                    button3.Enabled = true;
                    break;
                case 3:
                    button3.Enabled = true;
                    break;
                case 4:
                    button3.Enabled = true;
                    break;
            }
        }
     }
}

2010年11月12日 星期五

2010 11/12 井字遊戲

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[] Button1;
        int[,] a = new int[4, 4];
        int bno,nobtpressed=0;
        int rdno;
        int[] recordbutn = new int[9] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Button1 = new System.Windows.Forms.Button[9];
            for (int i = 0; i < 9; ++i)
            {
                Button1[i] = new Button();
                Button1[i].Size = new Size(50, 50);
                Button1[i].Click += new System.EventHandler(Button1_Click);
                this.Controls.Add(Button1[i]);
                if (i <= 2)
                    Button1[i].Location = new System.Drawing.Point(10 + i * 50, 0 );
                else if (i > 2 && i <= 5)
                    Button1[i].Location = new System.Drawing.Point(10 + (i - 3) * 50, 50 );
                else if (i > 5 && i <= 9)
                    Button1[i].Location = new System.Drawing.Point(10 + (i - 6) * 50, 100 );
                Button1[i].Text = "" + i;

              
              
               
               
            }

        }
        public void Button1_Click(object sender, EventArgs e)
        {
            // System.Windows.Forms.MessageBox.Show("You have clicked button " +
            //   ((System.Windows.Forms.Button)sender).Tag.ToString());
            //MessageBox.Show(Convert.ToString(((Button)sender).Text));
            String tnum = sender.ToString();
            int tlen = tnum.Length;
            String no = tnum.Substring(tlen - 1);
           
            int temp;
         
               
                for (int i = 0; i < 9; i++)
                {
                    if (Button1[i].Text == no)
                        // MessageBox.Show("" + no);
                        bno = i;
                }
                // MessageBox.Show(""+bno);
                switch (bno)
                {
                    case 0:
                        //MessageBox.Show("0");
                        if (Button1[0].Image != pictureBox2.Image && Button1[0].Image != pictureBox1.Image)
                        {
                            Button1[0].Image = pictureBox1.Image;
                            nobtpressed = nobtpressed + 1;  
                            label2.Text = "" + nobtpressed;
                            temp = recordbutn[0];
                            recordbutn[0] = recordbutn[9 - nobtpressed];
                            recordbutn[9 - nobtpressed] = temp;
                            button2_Click(sender, e);
                        }
                        break;
                    case 1:
                        // MessageBox.Show("1");
                       
                       
                        if (Button1[1].Image != pictureBox2.Image && Button1[1].Image != pictureBox1.Image)
                        {
                            Button1[1].Image = pictureBox1.Image;
                            nobtpressed = nobtpressed + 1;
                            label2.Text = "" + nobtpressed;
                            temp = recordbutn[1];
                            recordbutn[1] = recordbutn[9 - nobtpressed];
                            recordbutn[9 - nobtpressed] = temp;
                            button2_Click(sender, e);
                        }
                       
                        break;
                    case 2:
                        // MessageBox.Show("2");
                       
                        if (Button1[2].Image != pictureBox2.Image && Button1[2].Image != pictureBox1.Image)
                        {
                            Button1[2].Image = pictureBox1.Image;
                            nobtpressed = nobtpressed + 1;
                            label2.Text = "" + nobtpressed;
                            temp = recordbutn[2];
                            recordbutn[2] = recordbutn[9 - nobtpressed];
                            recordbutn[9 - nobtpressed] = temp;
                            button2_Click(sender, e);
                        }
                       
                        break;
                    case 3:
                        //MessageBox.Show("3");
                       
                        if (Button1[3].Image != pictureBox2.Image && Button1[3].Image != pictureBox1.Image)
                        {
                            Button1[3].Image = pictureBox1.Image;
                            nobtpressed = nobtpressed + 1;
                            label2.Text = "" + nobtpressed;
                            temp = recordbutn[3];
                            recordbutn[3] = recordbutn[9 - nobtpressed];
                            recordbutn[9 - nobtpressed] = temp;
                            button2_Click(sender, e);
                        }
                       
                        break;
                    case 4:
                        //MessageBox.Show("4");
                       
                        if (Button1[4].Image != pictureBox2.Image && Button1[4].Image != pictureBox1.Image)
                        {
                            Button1[4].Image = pictureBox1.Image;
                            nobtpressed = nobtpressed + 1;
                            label2.Text = "" + nobtpressed;
                            temp = recordbutn[4];
                            recordbutn[4] = recordbutn[9 - nobtpressed];
                            recordbutn[9 - nobtpressed] = temp;
                            button2_Click(sender, e);
                        }
                       
                        break;
                    case 5:
                        // MessageBox.Show("5");
                      
                        if (Button1[5].Image != pictureBox2.Image && Button1[5].Image != pictureBox1.Image)
                        {
                            Button1[5].Image = pictureBox1.Image;
                            nobtpressed = nobtpressed + 1;
                            label2.Text = "" + nobtpressed;
                            temp = recordbutn[5];
                            recordbutn[5] = recordbutn[9 - nobtpressed];
                            recordbutn[9 - nobtpressed] = temp;
                            button2_Click(sender, e);
                        }
                       
                        break;
                    case 6:
                        //  MessageBox.Show("6");
                       
                        if (Button1[6].Image != pictureBox2.Image && Button1[6].Image != pictureBox1.Image)
                        {
                            Button1[6].Image = pictureBox1.Image;
                            nobtpressed = nobtpressed + 1;
                            label2.Text = "" + nobtpressed;
                            temp = recordbutn[6];
                            recordbutn[6] = recordbutn[9 - nobtpressed];
                            recordbutn[9 - nobtpressed] = temp;
                            button2_Click(sender, e);
                        }
                       
                        break;
                    case 7:
                        // MessageBox.Show("7");
                       
                        if (Button1[7].Image != pictureBox2.Image && Button1[7].Image != pictureBox1.Image)
                        {
                            Button1[7].Image = pictureBox1.Image;
                            nobtpressed = nobtpressed + 1;
                            label2.Text = "" + nobtpressed;
                            temp = recordbutn[7];
                            recordbutn[7] = recordbutn[9 - nobtpressed];
                            recordbutn[9 - nobtpressed] = temp;
                            button2_Click(sender, e);
                        }
                       
                        break;
                    case 8:
                        //  MessageBox.Show("8");
                       
                        if (Button1[8].Image != pictureBox2.Image && Button1[8].Image != pictureBox1.Image)
                        {
                            Button1[8].Image = pictureBox1.Image;
                            nobtpressed = nobtpressed + 1;
                            label2.Text = "" + nobtpressed;
                            temp = recordbutn[8];
                            recordbutn[8] = recordbutn[9 - nobtpressed];
                            recordbutn[9 - nobtpressed] = temp;
                            button2_Click(sender, e);
                        }
                        break;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (nobtpressed < 8)
            {
                Random rd = new Random();
                int rdno = rd.Next(0, 9 - nobtpressed);
                label1.Text = "" + recordbutn[rdno];
                if (Button1[recordbutn[rdno]].Image != pictureBox2.Image && Button1[recordbutn[rdno]].Image != pictureBox1.Image)
                {
                    Button1[recordbutn[rdno]].Image = pictureBox2.Image;
                    nobtpressed = nobtpressed + 1;
                    label2.Text = "" + nobtpressed;
                    int temp = recordbutn[rdno];
                    recordbutn[rdno] = recordbutn[9 - nobtpressed];
                    recordbutn[9 - nobtpressed] = temp;
                }
                else
                {
                    button2_Click(sender, e);
                }
            }
            if (nobtpressed <= 9)
            {
                if (Button1[0].Image == pictureBox1.Image && Button1[1].Image == pictureBox1.Image && Button1[2].Image == pictureBox1.Image
                    || Button1[3].Image == pictureBox1.Image && Button1[4].Image == pictureBox1.Image && Button1[5].Image == pictureBox1.Image
                    || Button1[6].Image == pictureBox1.Image && Button1[7].Image == pictureBox1.Image && Button1[8].Image == pictureBox1.Image
                    || Button1[0].Image == pictureBox1.Image && Button1[3].Image == pictureBox1.Image && Button1[6].Image == pictureBox1.Image
                    || Button1[1].Image == pictureBox1.Image && Button1[4].Image == pictureBox1.Image && Button1[7].Image == pictureBox1.Image
                    || Button1[2].Image == pictureBox1.Image && Button1[5].Image == pictureBox1.Image && Button1[8].Image == pictureBox1.Image
                    || Button1[0].Image == pictureBox1.Image && Button1[4].Image == pictureBox1.Image && Button1[8].Image == pictureBox1.Image
                    || Button1[2].Image == pictureBox1.Image && Button1[4].Image == pictureBox1.Image && Button1[6].Image == pictureBox1.Image)
                {
                    MessageBox.Show("YOU WIN!!");
                }

                else if (Button1[0].Image == pictureBox2.Image && Button1[1].Image == pictureBox2.Image && Button1[2].Image == pictureBox2.Image
                    || Button1[3].Image == pictureBox2.Image && Button1[4].Image == pictureBox2.Image && Button1[5].Image == pictureBox2.Image
                    || Button1[6].Image == pictureBox2.Image && Button1[7].Image == pictureBox2.Image && Button1[8].Image == pictureBox2.Image
                    || Button1[0].Image == pictureBox2.Image && Button1[3].Image == pictureBox2.Image && Button1[6].Image == pictureBox2.Image
                    || Button1[1].Image == pictureBox2.Image && Button1[4].Image == pictureBox2.Image && Button1[7].Image == pictureBox2.Image
                    || Button1[2].Image == pictureBox2.Image && Button1[5].Image == pictureBox2.Image && Button1[8].Image == pictureBox2.Image
                    || Button1[0].Image == pictureBox2.Image && Button1[4].Image == pictureBox2.Image && Button1[8].Image == pictureBox2.Image
                    || Button1[2].Image == pictureBox2.Image && Button1[4].Image == pictureBox2.Image && Button1[6].Image == pictureBox2.Image)
                {
                    MessageBox.Show("電腦獲勝!!");
                }
                else if(nobtpressed==9)
                {
                    MessageBox.Show("平手");
                }
            }
        }
       
        private void button3_Click(object sender, EventArgs e)
        {
             if (MessageBox.Show("是否要重新開始遊戲?", "重新開始", MessageBoxButtons.YesNo,
                 MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
                 MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
             {

                    System.Windows.Forms.Application.Restart();
             }
        }
    }
}