2014年12月18日 星期四

12/19 生成4*4亂數按鈕

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
    {
        Button[,] buttons = new Button[17, 17]; //定義按鈕
        int[] array = new int[17]; //定義按鈕代表數字
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Instantiating all the buttons in the array



            for (int i = 0; i < 16; i++)
            {
                array[i] = i;  //每個按鈕的值分別為0到15
            }


            /*
            for (int j = 1; j < 10; j++)
            {
                buttons[1, j] = new Button();
                buttons[1, j].Location = new Point(50 * j, 50);
                buttons[1, j].Text = array[j].ToString();
                this.Controls.Add(buttons[1, j]);
            }
            */

            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    buttons[i, j] = new Button(); //這行會生成4*4個按鈕
                 
                    buttons[i, j].Location = new Point(j * 50, i * 50); // (i,j)按鈕位置以 (j*50,i*50) 分布
                    buttons[i, j].Text = j.ToString(); //按鈕Text只能顯示文字 所以iToString
                    buttons[i, j].Size = new Size(50, 50); //每個按鈕大小為50*50
                    this.Controls.Add(buttons[i, j]);
                    buttons[i, j].Click += new EventHandler(Button1_Click); //把生成的按鈕設成Button1
                }
            }

         

        }
        private void Button1_Click(object sender, EventArgs e)  //這段是控制程式生成的按鈕Button1
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (sender == buttons[i, j])
                    { MessageBox.Show("i=" + i + ",j=" + j + "," + buttons[i, j].Text); }
                       //點某個按鈕就會寫出它 i,j (位置) 跟按鈕的值
                }
            }
        }


        private void button1_Click(object sender, EventArgs e) //這段是控制自己放的按鈕 button1
        {
            int d1, tmp, k;

            Random irand = new Random();
            d1 = irand.Next(1, 17); //把d1隨機亂數為1到16的值
            label1.Text = d1.ToString();


            for (int j = 1; j < 17; j++)
            {
                k = 16 - j + 1;
                tmp = array[d1];
                array[d1] = array[k];
                array[k] = tmp;
            }


            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    //buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                    buttons[i, j].Text = array[i*4+j+1].ToString();
                    this.Controls.Add(buttons[i, j]);
                }
            }






            //buttons[1, 1].Text = array[1].ToString();

            /*
            for (int j = 1; j < 10; j++)
            {
                //    buttons[1, j] = new Button();
                buttons[1, j].Location = new Point(50 * j, 50);
                buttons[1, j].Text = array[j].ToString();
                this.Controls.Add(buttons[1, j]);
            }
             */


            label2.Text = array[d1].ToString();
         
        }
    }
}







2014年12月4日 星期四

12/5 VB不重複隨機變數+C#變數未完

Private Sub CommandButton1_Click()

For i = 1 To 9
Cells(1, i).Font.ColorIndex = 0
Cells(1, i) = i
Next
'生成9個格子

For i = 1 To 9
j = 10 - i

rnd1 = (Fix(Rnd * j) + 1)
'設變數rnd1
front1 = Cells(1, rnd1)
Cells(1, rnd1) = Cells(1, j)
'用rnd取代j
Cells(1, j) = front1
'Cells變成變數

'Cells(3, 1) = rnd1
'Cells(1, rnd1).Font.ColorIndex = 3
Next



End Sub




以下為C# 還沒做到隨機變數

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
    {
        Button[,] buttons = new Button[4, 4];
        int[] array1 = { 0,1, 2, 3, 4, 5, 6,7,8,9,10,11,12,13,14,15 };
        int x;
        int[] seat; //宣告按鈕 矩陣 X 跟 變數seat
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                    buttons[i, j].Size = new Size(50, 50);
                 
                    this.Controls.Add(buttons[i, j]);
                    x=array1[i*4+j];
                    //對調 i j可變換行列
                    buttons[i, j].Text = Convert.ToString(x);

                 
                    Random rnd = new Random();
            label1.Text = "" + rnd.Next(17).ToString();
                   }

              }
         
           }

        private void button1_Click(object sender, EventArgs e)
        {
         
           // buttons[i, j]=seat[];
         

        }

        }
    }


2014年11月28日 星期五

老師的部落格

http://tccnchsu.blogspot.tw/程式設計工藝大師(Master of Computer Programming)

https://www.youtube.com/watch?v=HzUMN2ILGwc世界激勵大師梁凱恩

9/26 紅綠燈

課堂上來不及上傳BLOG
複習如下

做一個按鈕button1為控制器
控制讓button2、button3、button4變色

1.宣告 int c ;
2.在button1_click事件中寫
   c=c+1;  // button1點擊次數每次+1
   
  if (c%3==1) {button2.BackColor=Color.Red
                        button3.BackColor=Color.Black
                        button4.BackColor=Color.Black };

 else if (c%3==2) {button2.BackColor=Color.Black
                              button3.BackColor=Color.Tellow
                              button4.BackColor=Color.Black };

 else               {button2.BackColor=Color.Black
                        button3.BackColor=Color.Black
                        button4.BackColor=Color.Green };

3.新增TIMER後 參數改為TRUE
Click事件可用TIMER控制 程式碼相同

 

2014年11月27日 星期四

11/28 16個按鈕 亂數未完

做出生成按鈕 還未做隨機變數

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
    {
        Button[,] buttons = new Button[4, 4]; //宣告按鈕
        public Form1()
        {
         

            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    buttons[i, j] = new Button(); //生成4*4按鈕
                    buttons[i, j].Location = new Point(i * 50, j * 50); //按紐位置為(i*50,j*50)
                    buttons[i, j].Size = new Size(50, 50); //按鈕大小為50*50
                 
                 

                    this.Controls.Add(buttons[i, j]);
                }
            }
        }
    }
}


2014年11月13日 星期四

11/14 VB作九九乘法表

Private Sub CommandButton1_Click()
Dim i As Integer

For i = 1 To 9
    For j = 1 To 9
'列出9*9個格子
        Cells(i, j).Value = i * j
'格子值為i*j
    Next j
Next i
End Sub


2014年10月31日 星期五

10/31 井字+判斷輸贏

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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {int c=0; //宣告c
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2; //宣告r為c/2的餘數 為了判斷要下X或O
         
            if (r == 1)
            { button1.Text = "O"; } //餘數為1就下O
            else
            { button1.Text = "X"; }//餘數為0就下X
            button1.Enabled = false;
            if ((button1.Text == button2.Text && button2.Text == button3.Text) ||
            (button1.Text == button5.Text && button5.Text == button9.Text) ||
            (button1.Text == button4.Text && button4.Text == button7.Text))
            { MessageBox.Show("win"); } //如果判斷有三個相同就表示連線 顯示win
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            button7.Text = "";
            button8.Text = "";
            button9.Text = ""; //先把button變為空白
        }

        private void button2_Click(object sender, EventArgs e)
        {

            int r;
            c = c + 1;
            r = c % 2;
         
            if (r == 1)
            { button2.Text = "O"; }
            else
            { button2.Text = "X"; } //在button2下 O或X
            button2.Enabled = false;
            if ((button2.Text == button1.Text && button1.Text == button3.Text) ||
            (button2.Text == button5.Text && button5.Text == button8.Text))
//如果跟button1&3或button5&8 相同
            { MessageBox.Show("win"); }  //就顯示win
        }
     

        private void button5_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
         
            if (r == 1)
            { button5.Text = "O"; }
            else
            { button5.Text = "X"; }
            button5.Enabled = false;
            if ((button5.Text == button4.Text && button4.Text == button6.Text) ||
            (button5.Text == button2.Text && button2.Text == button8.Text) ||
            (button5.Text == button1.Text && button1.Text == button9.Text) ||
            (button5.Text == button3.Text && button3.Text == button7.Text))
            { MessageBox.Show("win"); } //判斷方法同button2寫法
        }

        private void button8_Click(object sender, EventArgs e)
        {
         
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
         
            if (r == 1)
            { button7.Text = "O"; }
            else
            { button7.Text = "X"; }
            button7.Enabled = false;
            if ((button7.Text == button1.Text && button1.Text == button4.Text) ||
            (button7.Text == button8.Text && button8.Text == button9.Text) ||
            (button7.Text == button5.Text && button5.Text == button3.Text))
            { MessageBox.Show("win"); }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
         
            if (r == 1)
            { button6.Text = "O"; }
            else
            { button6.Text = "X"; }
            button6.Enabled = false;
            if ((button3.Text == button2.Text && button2.Text == button1.Text) ||
            (button3.Text == button6.Text && button6.Text == button9.Text) ||
            (button3.Text == button5.Text && button5.Text == button7.Text))
            { MessageBox.Show("win"); }
        }

        private void button9_Click(object sender, EventArgs e)
        {
         
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
         
            if (r == 1)
            { button4.Text = "O"; }
            else
            { button4.Text = "X"; }
            button4.Enabled = false;
            if ((button4.Text == button5.Text && button5.Text == button6.Text) ||
            (button4.Text == button1.Text && button1.Text == button7.Text))

            { MessageBox.Show("win"); }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
         
            if (r == 1)
            { button3.Text = "O"; }
            else
            { button3.Text = "X"; }
            button3.Enabled = false;
            if ((button3.Text == button2.Text && button2.Text == button1.Text) ||
            (button3.Text == button6.Text && button6.Text == button9.Text) ||
            (button3.Text == button5.Text && button5.Text == button7.Text))
            { MessageBox.Show("win"); }
        }

    }
}







方法2:用JUDGE (UNDONE)
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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {int c=0;
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            judge();
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button1.Text = "O"; }
            else
            { button1.Text = "X"; } button1.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            button7.Text = "";
            button8.Text = "";
            button9.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {  int r;
            c = c + 1;
            r = c % 2;
          
            if (r==1)
             { button2.Text = "O"; }
            else
            { button2.Text = "X"; } button2.Enabled = false;
            judge();
            
        }
        private void judge()
        {
            if (button1.Text == "X"&&button2.Text=="X"&&button3.Text=="X"){MessageBox.Show("Win!");}
            

             }

        private void button5_Click(object sender, EventArgs e)
        {
            judge();
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button5.Text = "O"; }
            else
            { button5.Text = "X"; } button5.Enabled = false;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            judge();
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button8.Text = "O"; }
            else
            { button8.Text = "X"; } button8.Enabled = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            judge();
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button7.Text = "O"; }
            else
            { button7.Text = "X"; } button7.Enabled = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            judge();
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button6.Text = "O"; }
            else
            { button6.Text = "X"; } button6.Enabled = false;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            judge();
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button9.Text = "O"; }
            else
            { button9.Text = "X"; } button9.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            judge();
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button4.Text = "O"; }
            else
            { button4.Text = "X"; } button4.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            judge();
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button3.Text = "O"; }
            else
            { button3.Text = "X"; } button3.Enabled = false;
        }

    }
}

2014年10月24日 星期五

10/24 井字遊戲

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        int c=0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            button7.Text = "";
            button8.Text = "";
            button9.Text = ""; //button值設為空白
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2; //宣告r跟c
         
            if (r==1)
             { button1.Text = "O"; }
            else
            { button1.Text = "X"; } button1.Enabled = false; //r用來判斷要填O或X
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button2.Text = "O"; }
            else
            { button2.Text = "X"; } //r為1就填O r為2填X
               button2.Enabled = false; //把按鈕鎖定不能再改
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button3.Text = "O"; }
            else
            { button3.Text = "X"; } button3.Enabled = false; //方法同上
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button4.Text = "O"; }
            else
            { button4.Text = "X"; } button4.Enabled = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button5.Text = "O"; }
            else
            { button5.Text = "X"; } button5.Enabled = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button6.Text = "O"; }
            else
            { button6.Text = "X"; } button6.Enabled = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button7.Text = "O"; }
            else
            { button7.Text = "X"; } button7.Enabled = false;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button8.Text = "O"; }
            else
            { button8.Text = "X"; }  button8.Enabled = false;
        }

     

        private void button9_Click_1(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;

            if (r == 1)
            { button9.Text = "O"; }
            else
            { button9.Text = "X"; }
         
            button9.Enabled = false;
        }

    }
}



2014年10月23日 星期四

10/24 按鈕賽跑 done

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;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int sum1 = 0;
        int sum2 = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) // button1_Click當成骰骰子
        {
            int d1, d2,dsum;
            Random ran = new Random();
            d1 = ran.Next(1, 7);
            d2 = ran.Next(1, 7); //骰子隨機變數為1~6
            label1.Text = Convert.ToString(d1);
            label2.Text = Convert.ToString(d2); //顯示骰子骰到多少
         
            dsum = d1 + d2; //兩顆骰子合計要走幾步
         
            for (int i = 1; i <= dsum; i++)
            {
                button2.Left =  sum1+i;  //讓button2移動sum1+i
                Thread.Sleep(100); //Delay 0.1秒 移動速度
                Application.DoEvents(); //timer的控制項
             
            }
            sum1 = sum1 + dsum;
            textBox1.Text = Convert.ToString(sum1);
            if (button2.Left >= 100) //設100為終點
            { MessageBox.Show("button2 win"); } //到終點則win
        }

        private void button4_Click(object sender, EventArgs e)
        {  //判斷方法同上
            int d1, d2, dsum;
            Random ran = new Random();
            d1 = ran.Next(1, 7);
            d2 = ran.Next(1, 7);
            label1.Text = Convert.ToString(d1);
            label2.Text = Convert.ToString(d2);

            dsum = d1 + d2;

            for (int i = 1; i <= dsum; i++)
            {
                button3.Left = sum2 + i;
                Thread.Sleep(100); //Delay 0.1秒
                Application.DoEvents();

            }
            sum2 = sum2 + dsum;
            textBox1.Text = Convert.ToString(sum2);
            if (button3.Left >= 100)
            { MessageBox.Show("button3 win"); }
        }
        }
     
}



2014年10月17日 星期五

10/17 賽跑

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;
using System.Threading;  //delay thread

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

        private void button2_Click(object sender, EventArgs e)
        {
            int d1, d2;
            Random ran = new Random();
            d1 = ran.Next(1, 7);
            d2 = ran.Next(1, 7);
            label1.Text = Convert.ToString(d1);
            label2.Text = Convert.ToString(d2);
            for (int i = 1; i <= d1 + d2; i++)
            {

                button3.Left = 10 * i;
                Thread.Sleep(1000); //Delay 1秒
                Application.DoEvents();
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int d1, d2;
            Random ran = new Random();
            d1 = ran.Next(1, 7);
            d2 = ran.Next(1, 7);
            label1.Text = Convert.ToString(d1);
            label2.Text = Convert.ToString(d2);
            for ( int i = ; i <= d1 + d2; i++)
            {

                button4.Left = 10 * i;
                Thread.Sleep(1000); //Delay 1秒
                Application.DoEvents();
                int a = d1 + d2;
            }
         
         
        }
    }
}