2014年12月29日 星期一

推格子遊戲

========================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public System.Windows.Forms.Button Button1, Button2, Button3, Button4, Button5;
        Button[,] buttons = new Button[50, 50];
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Visible = false;
            Button1 = new Button();
            Button1.Location = new Point(150, 60);
            Button1.Size = new Size(170, 50);
            Button1.Text = "EASY";
            Button1.BackColor = Color.White;
            this.Controls.Add(Button1);
            Button1.Click += new EventHandler(Button1_Click);
            Button2 = new Button();
            Button2.Location = new Point(150, 130);
            Button2.Size = new Size(170, 50);
            Button2.Text = "NORMAL";
            Button2.BackColor = Color.White;
            this.Controls.Add(Button2);
            Button2.Click += new EventHandler(Button2_Click);
            Button3 = new Button();
            Button3.Location = new Point(150, 200);
            Button3.Size = new Size(170, 50);
            Button3.Text = "HARD";
            Button3.BackColor = Color.White;
            this.Controls.Add(Button3);
            Button3.Click += new EventHandler(Button3_Click);
           
        }      

        private void Button1_Click(object sender, EventArgs e)
        {
            Button1.Visible = false;
            Button2.Visible = false;
            Button3.Visible = false;
            Button4 = new Button();
            Button4.Location = new Point(200, 20);
            Button4.Size = new Size(80, 30);
            Button4.Text = "檢測輸贏";
            Button4.BackColor = Color.White;
            this.Controls.Add(Button4);
            Button4.Click += new EventHandler(b_Click);
            Button5 = new Button();
            Button5.Location = new Point(200, 50);
            Button5.Size = new Size(80, 30);
            Button5.Text = "重新開始";
            Button5.BackColor = Color.White;
            this.Controls.Add(Button5);
            Button5.Click += new EventHandler(c_Click);
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 4; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(40 + 30 * (i - 1), 103 + 30 * (j - 1));
                    buttons[i, j].Size = new Size(30, 30);
                    buttons[i, j].Text = " ";
                    buttons[i, j].BackColor = Color.White;
                    this.Controls.Add(buttons[i, j]);
                    buttons[i, j].Click += new EventHandler(a1_Click);
                }
            }
            for (int j = 1; j < 4; j++)
            {
                buttons[1, j].Text = "O";
                buttons[13, j].Text = "X";
                buttons[1, j].Enabled = false;
                buttons[13, j].Enabled = false;
            }
            label2.Text = "3";          
        }
        int c1 = 0;
        private void a1_Click(object sender, EventArgs e)
        {
            int x, y;
            x = Form1.ActiveForm.Location.X + 50;
            y = Form1.ActiveForm.Location.Y + 140;
            c1++;
            //MessageBox.Show(Convert.ToString(c));
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 4; j++)
                {
                    if (MousePosition.X >= x + (i - 1) * 30 && MousePosition.X < x + i * 30 && MousePosition.Y >= y + (j - 1) * 30 && MousePosition.Y < y + j * 30)
                    {
                        if (c1 % 2 == 1)
                        {
                            for (int a = 1; a <= i; a++)
                            {
                                buttons[a, j].Text = " ";
                                buttons[a, j].Enabled = false;
                            }
                            buttons[i, j].Text = "O";
                        }
                        if (c1 % 2 == 0)
                        {
                            for (int a = 13; a >= i; a--)
                            {
                                buttons[a, j].Text = " ";
                                buttons[a, j].Enabled = false;
                            }
                            buttons[i, j].Text = "X";
                        }
                    }                  
                }
            }
            //MessageBox.Show(Convert.ToString(MousePosition)+Convert.ToString(Form1.ActiveForm.Location));
            Button4.Enabled = true;
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 4; j++)
                {
                    if (buttons[i, j].Enabled == true)
                        Button4.Enabled = false;
                }
            }
            label1.Text = Convert.ToString(c1);
        }
        private void Button2_Click(object sender, EventArgs e)
        {   Button1.Visible = false;
            Button2.Visible = false;
            Button3.Visible = false;
            Button4 = new Button();
            Button4.Location = new Point(200, 20);
            Button4.Size = new Size(80, 30);
            Button4.Text = "檢測輸贏";
            Button4.BackColor = Color.White;
            this.Controls.Add(Button4);
            Button4.Click += new EventHandler(b_Click);
            Button5 = new Button();
            Button5.Location = new Point(200, 50);
            Button5.Size = new Size(80, 30);
            Button5.Text = "重新開始";
            Button5.BackColor = Color.White;
            this.Controls.Add(Button5);
            Button5.Click += new EventHandler(c_Click);
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 6; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(40 + 30 * (i - 1), 103 + 30 * (j - 1));
                    buttons[i, j].Size = new Size(30, 30);
                    buttons[i, j].Text = " ";
                    buttons[i, j].BackColor = Color.White;
                    this.Controls.Add(buttons[i, j]);
                    buttons[i, j].Click += new EventHandler(a2_Click);
                }
            }
            for (int j = 1; j < 6; j++)
            {
                buttons[1, j].Text = "O";
                buttons[13, j].Text = "X";
                buttons[1, j].Enabled = false;
                buttons[13, j].Enabled = false;
            }
            label2.Text = "5";          
        }
        int c2 = 0;
        private void a2_Click(object sender, EventArgs e)
        {
            int x, y;
            x = Form1.ActiveForm.Location.X + 50;
            y = Form1.ActiveForm.Location.Y + 140;
            c2++;
            //MessageBox.Show(Convert.ToString(c));
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 6; j++)
                {
                    if (MousePosition.X >= x + (i - 1) * 30 && MousePosition.X < x + i * 30 && MousePosition.Y >= y + (j - 1) * 30 && MousePosition.Y < y + j * 30)
                    {
                        if (c2 % 2 == 1)
                        {
                            for (int a = 1; a <= i; a++)
                            {
                                buttons[a, j].Text = " ";
                                buttons[a, j].Enabled = false;
                            }
                            buttons[i, j].Text = "O";
                        }
                        if (c2 % 2 == 0)
                        {
                            for (int a = 13; a >= i; a--)
                            {
                                buttons[a, j].Text = " ";
                                buttons[a, j].Enabled = false;
                            }
                            buttons[i, j].Text = "X";
                        }
                    }
                }
            }
            //MessageBox.Show(Convert.ToString(MousePosition)+Convert.ToString(Form1.ActiveForm.Location));
            Button4.Enabled = true;
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 6; j++)
                {
                    if (buttons[i, j].Enabled == true)
                        Button4.Enabled = false;
                }
            }
            label1.Text = Convert.ToString(c2);
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            Button1.Visible = false;
            Button2.Visible = false;
            Button3.Visible = false;
            Button4 = new Button();
            Button4.Location = new Point(200, 20);
            Button4.Size = new Size(80, 30);
            Button4.Text = "檢測輸贏";
            Button4.BackColor = Color.White;
            this.Controls.Add(Button4);
            Button4.Click += new EventHandler(b_Click);
            Button5 = new Button();
            Button5.Location = new Point(200, 50);
            Button5.Size = new Size(80, 30);
            Button5.Text = "重新開始";
            Button5.BackColor = Color.White;
            this.Controls.Add(Button5);
            Button5.Click += new EventHandler(c_Click);
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 8; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(40 + 30 * (i - 1), 103 + 30 * (j - 1));
                    buttons[i, j].Size = new Size(30, 30);
                    buttons[i, j].Text = " ";
                    buttons[i, j].BackColor = Color.White;
                    this.Controls.Add(buttons[i, j]);
                    buttons[i, j].Click += new EventHandler(a3_Click);
                }
            }
            for (int j = 1; j < 8; j++)
            {
                buttons[1, j].Text = "O";
                buttons[13, j].Text = "X";
                buttons[1, j].Enabled = false;
                buttons[13, j].Enabled = false;
            }
            label2.Text = "7";
        }
        int c3 = 0;
        private void a3_Click(object sender, EventArgs e)
        {
            int x, y;
            x = Form1.ActiveForm.Location.X + 50;
            y = Form1.ActiveForm.Location.Y + 140;
            c3++;
            //MessageBox.Show(Convert.ToString(c));
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 8; j++)
                {
                    if (MousePosition.X >= x + (i - 1) * 30 && MousePosition.X < x + i * 30 && MousePosition.Y >= y + (j - 1) * 30 && MousePosition.Y < y + j * 30)
                    {
                        if (c3 % 2 == 1)
                        {
                            for (int a = 1; a <= i; a++)
                            {
                                buttons[a, j].Text = " ";
                                buttons[a, j].Enabled = false;
                            }
                            buttons[i, j].Text = "O";
                        }
                        if (c3 % 2 == 0)
                        {
                            for (int a = 13; a >= i; a--)
                            {
                                buttons[a, j].Text = " ";
                                buttons[a, j].Enabled = false;
                            }
                            buttons[i, j].Text = "X";
                        }
                    }
                }
            }
            //MessageBox.Show(Convert.ToString(MousePosition)+Convert.ToString(Form1.ActiveForm.Location));
            Button4.Enabled = true;
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 8; j++)
                {
                    if (buttons[i, j].Enabled == true)
                        Button4.Enabled = false;
                }
            }
            label1.Text = Convert.ToString(c3);
        }

        private void b_Click(object sender, EventArgs e)
        {
            int d;
            d = Convert.ToInt32(label1.Text);
            if (d % 2 == 0)
                MessageBox.Show("X WIN");
            else
                MessageBox.Show("O WIN");
        }

        private void c_Click(object sender, EventArgs e)
        {
            button1.Visible = false;
            Button1 = new Button();
            Button1.Location = new Point(150, 60);
            Button1.Size = new Size(170, 50);
            Button1.Text = "EASY";
            Button1.BackColor = Color.White;
            this.Controls.Add(Button1);
            Button1.Click += new EventHandler(Button1_Click);
            Button2 = new Button();
            Button2.Location = new Point(150, 130);
            Button2.Size = new Size(170, 50);
            Button2.Text = "NORMAL";
            Button2.BackColor = Color.White;
            this.Controls.Add(Button2);
            Button2.Click += new EventHandler(Button2_Click);
            Button3 = new Button();
            Button3.Location = new Point(150, 200);
            Button3.Size = new Size(170, 50);
            Button3.Text = "HARD";
            Button3.BackColor = Color.White;
            this.Controls.Add(Button3);
            Button3.Click += new EventHandler(Button3_Click);
            int a;
            a = Convert.ToInt32(label2.Text);
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < a+1; j++)
                {
                    buttons[i, j].Visible = false;
                }
            }
            Button4.Visible = false;
            Button5.Visible = false;
        }
       
       
    }
}

2014年12月23日 星期二

猜數字遊戲

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public System.Windows.Forms.Button Button1;
        public System.Windows.Forms.Button Button2;
        Random rand = new Random();
        Button[] buttons = new Button[10];
        int a1, a2, a3, a4;
        double b1, b2, b3, b4;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int x, y;
            int[] m = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            for (int i = 9; i > 1; i--)
            {
                x = rand.Next(0, i);
                y = m[x];
                m[x] = m[i];
                m[i] = y;
            }
            a1 = m[1];
            a2 = m[2];
            a3 = m[3];
            a4 = m[4];
            //MessageBox.Show(Convert.ToString(a1) + Convert.ToString(a2) + Convert.ToString(a3) + Convert.ToString(a4));
            label1.Visible = true;
            label2.Visible = true;
            label3.Visible = true;
            label4.Visible = true;
            label1.Text = "   ";
            label2.Text = "   ";
            label3.Text = "   ";
            label4.Text = "   ";
            for(int i=0;i<10;i++)
            {
                buttons[i] = new Button();
                buttons[i].Location = new Point(10 + i * 30, 120 );
                buttons[i].Size = new Size(30, 30);
                buttons[i].Text = Convert.ToString(i);
                this.Controls.Add(buttons[i]);              
                buttons[i].Click += new EventHandler(a_Click);
            }
            Button1 = new System.Windows.Forms.Button();
            Button1.Location = new Point(165, 150);
            Button1.Size = new Size(50, 30);
            Button1.Click += new EventHandler(Button1_Click);
            Button1.Text = "取消";
            this.Controls.Add(Button1);
            Button2 = new System.Windows.Forms.Button();
            Button2.Location = new Point(105, 150);
            Button2.Size = new Size(50, 30);
            Button2.Click += new EventHandler(Button2_Click);
            Button2.Text = "確定";
            this.Controls.Add(Button2);
            label5.Text = " ";
            button1.Text = "開始新局";
        }
        int j = 0;
        private void a_Click(object sender, EventArgs e)
        {          
            j++;
            Button b = (Button)sender;
            double x = Convert.ToDouble(b.Text);
            for(int i=0;i<10;i++)
            {
                if(i==x)
                {
                    buttons[i].Visible = false;

                }
            }
            if(j%4==1)
            {
                label1.Text = b.Text;
            }
            if (j%4 == 2)
            {
                label2.Text = b.Text;
            }
            if (j%4 == 3)
            {
                label3.Text = b.Text;
            }
            if (j%4 == 0)
            {
                label4.Text = b.Text;
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            label1.Visible = true;
            label1.Text = "   ";
            label2.Visible = true;
            label2.Text = "   ";
            label3.Visible = true;
            label3.Text = "   ";
            label4.Visible = true;
            label4.Text = "   ";
        }
        int p = 0;
        private void Button2_Click(object sender, EventArgs e)
        {
            p++;
            int a=0,b=0;
            b1 = Convert.ToDouble(label1.Text);
            b2 = Convert.ToDouble(label2.Text);
            b3 = Convert.ToDouble(label3.Text);
            b4 = Convert.ToDouble(label4.Text);
            if (a1 == b1 && a2 == b2 && a3 == b3 && a4 == b4)
                MessageBox.Show("贏了");
            if (a1 == b1)
                a++;
            if (a1 == b2 || a1 == b3 || a1 == b4)
                b++;
            if (a2 == b2)
                a++;
            if (a2 == b1 || a2 == b3 || a2 == b4)
                b++;
            if (a3 == b3)
                a++;
            if (a3 == b1 || a3 == b2 || a3 == b4)
                b++;
            if (a4 == b4)
                a++;
            if (a4 == b1 || a4 == b2 || a4 == b3)
                b++;
            label5.Text = label5.Text + Environment.NewLine+a + "a" + b + "b";
            label1.Text = "   ";
            label2.Text = "   ";
            label3.Text = "   ";
            label4.Text = "   ";
            for (int i = 0; i < 10; i++)
            {
                buttons[i].Visible = true;
            }
        }              
    }
}

2014年12月18日 星期四

2014/12/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 _123
{
    public partial class Form1 : Form
    {
        public System.Windows.Forms.Button Button1;
        Random rand = new Random();
        Button[,] buttons = new Button[10, 10];
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Button1 = new System.Windows.Forms.Button();
            Button1.Location = new Point(400, 400);
            Button1.Size = new Size(100, 30);
            Button1.Click += new EventHandler(Button1_Click);
            Button1.Text = "Rand";
            this.Controls.Add(Button1);

            for (int j = 1; j < 5; j++)
            {
                for (int i = 1; i < 5; i++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(80 + i * 50, 80 + j * 50);
                    buttons[i, j].Size = new Size(50, 50);
                    buttons[i, j].Text = Convert.ToString((i - 1) + 4 * (j - 1));
                    this.Controls.Add(buttons[i, j]);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int x, y, a;
            int[] myarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            for (int i = 15; i > 1; i--)
            {

                x = rand.Next(0, i);
                y = myarray[x];
                myarray[x] = myarray[i];
                myarray[i] = y;

            }
            for (int j = 1; j < 5; j++)
            {
                for (int i = 1; i < 5; i++)
                {
                    a = myarray[(j - 1) * 4 + (i - 1)];
                    buttons[i, j].Text = Convert.ToString(a);
                    buttons[i, j].Click += new EventHandler(a_Click);
                    this.Controls.Add(buttons[i, j]);
                }
            }

        }
        //int m, n;
        protected void a_Click(object sender, EventArgs e)
        {
           
            for (int i = 1; i < 5; i++)
            {
                for (int j = 1; j < 5; j++)
                {
                    if (Convert.ToDouble(buttons[i, j].Text) == 0)
                    {
                        buttons[i - 1, j].Click += new EventHandler(b_Click);
                        buttons[i + 1, j].Click += new EventHandler(b_Click);
                        buttons[i, j - 1].Click += new EventHandler(b_Click);
                        buttons[i, j + 1].Click += new EventHandler(b_Click);
                        //i = m;
                        //j = n;
                        //this.Controls.Add(buttons[i, j]);
                        //if (sender == buttons[i, j])
                        //MessageBox.Show("(" + Convert.ToString(i) + "," + Convert.ToString(j) + ")");

                    }
                }
            }
        }
        protected void b_Click(object sender, EventArgs e)
        {
            //buttons[m, n].Text = buttons[m - 1, n].Text;
            //buttons[m-1, n].Text = buttons[m , n].Text;
            MessageBox.Show("  ");
        }

    }
}
       
   

2014年12月4日 星期四

2014/12/05

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;
            //Button[,] Buttons;
            Button[,] buttons = new Button[4, 4];
           
     
        public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
               
         
            }
        private void button1_Click(object sender, EventArgs e)
        {
            int x, d1, a1;
         
            int width, height;
            int[] my = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
         
             Random irand = new Random();

             for (int k = 15; k >= 0; k--)
             {
                 d1 = irand.Next(0, k);
                 a1 = my[d1];
                 my[d1] = my[k];
                 my[k] = a1;
             }
                for (int j = 0; j < 4; j++)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        width = this.Size.Width;
                        height = this.Size.Height;
                        buttons[i, j] = new Button();
                        buttons[i, j].Location = new Point(i * 50, j * 50);
                        //x = (i + 1) + 4 * j;
                        x = my[j * 4 + i];
                        width = 50;
                        height = 50;
                        buttons[i, j].Size = new Size(width, height);
                        buttons[i, j].Text = x.ToString();
                        this.Controls.Add(buttons[i, j]);
                         MessageBox.Show("" + x.ToString());
               
                    }
                }
                   

            }
       
        }
    }