2015年1月8日 星期四

許志宇老師網誌-程式設計工藝大師

http://tccnchsu.blogspot.tw/
---------程式設計工藝大師Blog--------------------------------------------

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

2014年12月18日 星期四

4*4猜數字

4X4的格狀井字遊戲

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
    {
        Button[,] buttons = new Button[17, 17]; //因為<17 我們需要4*4=16個數字 故為16+1=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 = 1; i < 17; i++)
            {
                array[i] = i;
            }


            /*
            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();
                    buttons[i, j].Location = new Point(i * 75, j * 58); //改變格子間距大小 讓介面美觀
                    buttons[i, j].Size = new Size(75, 58);  //改變格子尺寸大小
                    buttons[i, j].Text = j.ToString();
                    this.Controls.Add(buttons[i, j]);
                    buttons[i, j].Click += new EventHandler(Button1_Click);  //增加一種按鍵功能
                }
            }

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

            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+" is "+buttons[i,j].Text);
                    }
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
       
                 
         
              int d1, tmp, k;

            Random irand = new Random();
            d1 = irand.Next(1, 10);
            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 * 75, j * 58);
                    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();

   
        }
    }
}

Guess the number

3X3格子猜數字
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
    {
        Button[,] buttons = new Button[10, 10];
        int[] array = new int[10];

        public Form1()
        {
            InitializeComponent();
        }

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



            for (int i = 1; i < 10; i++)
            {
                array[i] = i;
            }


            /*
            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 < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                    buttons[i, j].Text = j.ToString();
                    this.Controls.Add(buttons[i, j]);
                    buttons[i, j].Click += new EventHandler(Button1_Click);
                }
            }

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

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (sender == buttons[i, j])
                    {
                        MessageBox.Show("i="+i+".j="+j+" is "+buttons[i,j].Text);
                    }
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
       
                 
         
              int d1, tmp, k;

            Random irand = new Random();
            d1 = irand.Next(1, 10);
            label1.Text = d1.ToString();


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


            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    //buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                    buttons[i, j].Text = array[i*3+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日 星期四

亂數陣列

形成亂數陣列
是華榮道類型遊戲的基礎

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];
        Random a = new Random();


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            int x;
            int width, height;
            int[] myarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            Random ranObj = new Random();
            LinkedList<int> itemList = new LinkedList<int>();
            // Instantiating all the buttons in the array
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    width = this.Size.Width;
                    height = this.Size.Height;
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                    x = myarray[j * 4 + i] = ranObj.Next(15);
                    itemList.AddLast(j * 4 + i);
                    Console.WriteLine("myarray[" + j * 4 + i + "] = " + myarray[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]);



                }

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}

老師我之前的網誌檔案分布在不同網誌 剛剛才重新集中到一個網址 所以順序有點亂

紅綠燈--------------2014/9/26

   利用餘數來決定BUTTON顏色
結此達到紅綠燈效果


 public partial class Form1 : Form
    {
        int c;
        int r;
        public Form1()
        {

            InitializeComponent();
            c = 0;
        }

        private void button3_Click(object sender, EventArgs e)
        {

            c = c + 1;
            r = c % 2;
            label1.Text = "r=" + r;
            if (r == 0)
            {
                button1.Location = new Point(50, 50);
                button2.BackColor = Color.Black;
            }
            else
            {
                button1.Location = new Point(0, 0);
                button2.BackColor = Color.Yellow;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            c = c + 1;
            r = c % 3;
            label1.Text = "r=" + r;
            if (r == 0)
            {
                button1.BackColor = Color.Yellow;
                button2.BackColor = Color.Black;
                button3.BackColor = Color.Black;
            }
            else
                if (r == 1)
                {
                    button1.BackColor = Color.Black;
                    button2.BackColor = Color.Red;
                    button3.BackColor = Color.Black;
                }
                else
                {
                    button1.BackColor = Color.Black;
                    button2.BackColor = Color.Black;
                    button3.BackColor = Color.Green;
                }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            c = c + 1;
            r = c % 3;
            label1.Text = "r=" + r;
            if (r == 0)
            {
                button1.BackColor = Color.Yellow;
                button2.BackColor = Color.Black;
                button3.BackColor = Color.Black;
            }
            else
                if (r == 1)
                {
                    button1.BackColor = Color.Black;
                    button2.BackColor = Color.Red;
                    button3.BackColor = Color.Black;
                }
                else
                {
                    button1.BackColor = Color.Black;
                    button2.BackColor = Color.Black;
                    button3.BackColor = Color.Green;
                }




        }
    }
}

擲骰子大富翁----------------2014/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;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        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);  //隨機從1~6中選取一數 就可以有擲骰子功能 
            label1.Text = Convert.ToString(d1);
            label2.Text = Convert.ToString(d2);
            for (int i = 1; i <= d1 + d2; i++)
            {

                Thread.Sleep(1000); //Delay 1秒  
                Application.DoEvents();
                button3.Left = 10 * i;   //此為設定物件移動速度
            }
        }

        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++)
            {
                button4.Left = 10 * i;
                Thread.Sleep(1000); //Delay 1秒
                Application.DoEvents();
              
            }
            }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

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