2014/10/31

10/31第六週 補充圈叉遊戲玩家輸贏判斷

10/31第六週  補充圈叉遊戲玩家輸贏判斷

首先,一樣利用工具箱建立9個button、1個textbox

然後在 public partial class Form1 : Form裏頭輸入 int c=0;

接者,在表單程式裡輸入button1.Text = "";
依此類推輸入另外8個

接下來和上次一樣在每個button內輸入相同宣告、條件來判斷玩家下的是O or X
但是這次不先輸入連線判斷,改用judge();來撰寫
註:judge()可隨意命名,最後宣告物件程式碼相同即可

在程式最後建立 private void judge()
輸入判斷輸贏的假設條件,即可完成程式

Ex:
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 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 = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button1.Text = "O";
            }
            else
            {
                button1.Text = "X";
            }
            button1.Enabled = false;
            judge();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button2.Text = "O";
            }
            else
            {
                button2.Text = "X";
            }
            button2.Enabled = false;

            judge();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button3.Text = "O";
            }
            else
            {
                button3.Text = "X";
            }
            button3.Enabled = false;

            judge();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button4.Text = "O";
            }
            else
            {
                button4.Text = "X";
            }
            button4.Enabled = false;

            judge();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button5.Text = "O";
            }
            else
            {
                button5.Text = "X";
            }
            button5.Enabled = false;

            judge();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button6.Text = "O";
            }
            else
            {
                button6.Text = "X";
            }
            button6.Enabled = false;

            judge();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button7.Text = "O";
            }
            else
            {
                button7.Text = "X";
            }
            button7.Enabled = false;

            judge();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button8.Text = "O";
            }
            else
            {
                button8.Text = "X";
            }
            button8.Enabled = false;

            judge();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
            if (r == 0)
            {
                button9.Text = "O";
            }
            else
            {
                button9.Text = "X";
            }
            button9.Enabled = false;

            judge();
        }

        private void judge()
        {
            if ((button1.Text == "O" && button2.Text == "O" && button3.Text == "O") ||
               (button4.Text == "O" && button5.Text == "O" && button6.Text == "O") ||
               (button7.Text == "O" && button8.Text == "O" && button9.Text == "O") ||
               (button1.Text == "O" && button4.Text == "O" && button7.Text == "O") ||
               (button2.Text == "O" && button5.Text == "O" && button8.Text == "O") ||
               (button3.Text == "O" && button6.Text == "O" && button9.Text == "O") ||
               (button1.Text == "O" && button5.Text == "O" && button9.Text == "O") ||
               (button3.Text == "O" && button5.Text == "O" && button7.Text == "O"))
                MessageBox.Show("O WIN!");
            else if ((button1.Text == "X" && button2.Text == "X" && button3.Text == "X") ||
                (button4.Text == "X" && button5.Text == "X" && button6.Text == "X") ||
                (button7.Text == "X" && button8.Text == "X" && button9.Text == "X") ||
                (button1.Text == "X" && button4.Text == "X" && button7.Text == "X") ||
                (button2.Text == "X" && button5.Text == "X" && button8.Text == "X") ||
                (button3.Text == "X" && button6.Text == "X" && button9.Text == "X") ||
                (button1.Text == "X" && button5.Text == "X" && button9.Text == "X") ||
                (button3.Text == "X" && button5.Text == "X" && button7.Text == "X"))
                MessageBox.Show("X WIN!!");
        }
    }
}









2014/10/24

10/24第五週 補充10/17之賽車比賽

10/24第五週  補充10/17之賽車比賽

首先,利用工具箱建立以下物件
2個label (作為點數用)、一個textbox(作為累積總步數)、4個button(1、4為骰子,2、3為車)

然後,先在宣告欄的位置輸入using System.Threading;

接者,在 public partial class Form1 : Form裏頭輸入 int total1 = 0, total2 = 0;

再來,進入button1的程式裡輸入骰子的宣告、假設條件、迴圈以及輸贏的判斷

同理,button4也仿照button1的程式碼,即完成賽車遊戲

Ex:
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 total1 = 0, total2 = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int d1, d2, sum;
            Random ran = new Random();
            d1 = ran.Next(1, 7);
            d2 = ran.Next(1, 7);
            label1.Text = Convert.ToString(d1);
            label2.Text = Convert.ToString(d2);
            sum = d1 + d2;
            for (int i = 1; i <= sum; i++)
            {
                button2.Left = (i + total1) * 5;
                Thread.Sleep(100);
                Application.DoEvents();
            }
            total1 = total1 + sum;
            textBox1.Text = Convert.ToString(total1);

            if (button2.Left >=100)
                MessageBox.Show("甲車贏");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int d1, d2, sum;
            Random ran = new Random();
            d1 = ran.Next(1, 7);
            d2 = ran.Next(1, 7);
            label1.Text = Convert.ToString(d1);
            label2.Text = Convert.ToString(d2);
            sum = d1 + d2;
            for (int i = 1; i <= sum; i++)
            {
                button3.Left = (i + total2) * 5;
                Thread.Sleep(100);
                Application.DoEvents();
            }
            total2 = total2 + sum;
            textBox1.Text = Convert.ToString(total2);

            if (button3.Left >= 100)
                MessageBox.Show("乙車贏");
        }
    }
}





10/24第五週 圈叉遊戲

10/24第五週  圈叉遊戲

首先,利用工具箱的button功能建立9個按鈕以及建立一個label

然後在public partial class Form1 : Form裏頭輸入int c;

再來進入表單程式輸入 button1.Text = "";
依樣畫葫蘆地把另外8個也輸入進去

最後,在每個按鈕的程式裡宣告變數以及假設條件
即可完成圈叉遊戲

Ex:
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;

        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 = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            label1.Text = Convert.ToString(r);

            if (r == 1)
                button1.Text = "O";
            else
                button1.Text = "X";

            button1.Enabled = false;

            if(button1.Text==button2.Text&button2.Text==button3.Text&button1.Text==button3.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button1.Text == button4.Text & button4.Text == button7.Text & button1.Text == button7.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button1.Text == button5.Text & button5.Text == button9.Text & button1.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

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

            if (r == 1)
                button2.Text = "O";
            else
                button2.Text = "X";

            button2.Enabled = false;

            if (button1.Text == button2.Text & button2.Text == button3.Text & button1.Text == button3.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button2.Text == button5.Text & button5.Text == button8.Text & button2.Text == button8.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

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

            if (r == 1)
                button3.Text = "O";
            else
                button3.Text = "X";

            button3.Enabled = false;

            if (button1.Text == button2.Text & button2.Text == button3.Text & button1.Text == button3.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button3.Text == button6.Text & button6.Text == button9.Text & button3.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button3.Text == button5.Text & button5.Text == button7.Text & button3.Text == button7.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
        }

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

            if (r == 1)
                button4.Text = "O";
            else
                button4.Text = "X";

            button4.Enabled = false;

            if (button4.Text == button5.Text & button5.Text == button6.Text & button4.Text == button6.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button1.Text == button4.Text & button4.Text == button7.Text & button1.Text == button7.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

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

            if (r == 1)
                button5.Text = "O";
            else
                button5.Text = "X";

            button5.Enabled = false;

            if (button4.Text == button5.Text & button5.Text == button6.Text & button4.Text == button6.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button2.Text == button5.Text & button5.Text == button8.Text & button2.Text == button8.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button1.Text == button5.Text & button5.Text == button9.Text & button1.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button3.Text == button5.Text & button5.Text == button7.Text & button3.Text == button7.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

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

            if (r == 1)
                button6.Text = "O";
            else
                button6.Text = "X";

            button6.Enabled = false;

            if (button4.Text == button5.Text & button5.Text == button6.Text & button4.Text == button6.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button3.Text == button6.Text & button6.Text == button9.Text & button3.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
        }

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

            if (r == 1)
                button7.Text = "O";
            else
                button7.Text = "X";

            button7.Enabled = false;

            if (button7.Text == button8.Text & button8.Text == button9.Text & button7.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button1.Text == button4.Text & button4.Text == button7.Text & button1.Text == button7.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button3.Text == button5.Text & button5.Text == button7.Text & button3.Text == button7.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

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

            if (r == 1)
                button8.Text = "O";
            else
                button8.Text = "X";

            button8.Enabled = false;

            if (button7.Text == button8.Text & button8.Text == button9.Text & button7.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button2.Text == button5.Text & button5.Text == button8.Text & button2.Text == button8.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

        private void button9_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            label1.Text = Convert.ToString(r);

            if (r == 1)
                button9.Text = "O";
            else
                button9.Text = "X";

            button9.Enabled = false;

            if (button7.Text == button8.Text & button8.Text == button9.Text & button7.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button3.Text == button6.Text & button6.Text == button9.Text & button3.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }
            else if (button1.Text == button5.Text & button5.Text == button9.Text & button1.Text == button9.Text)
            {
                if (r == 1)
                    MessageBox.Show("O is winner!!");
                else
                    MessageBox.Show("X is winner!!");
            }

        }

    }
}










2014/10/17

10/17第四週 利用隨機擲骰做車子比賽

10/17第四週  利用隨機擲骰做車子比賽

首先,利用工具箱建立四個button、一個Textbox、一個label
其中button1表車子1、button2表骰子1、button3表車子2、button4表骰子2
然後進入buttin2程式
一開始先在宣告位置那建入一類別 using System.Threading;

然後回到button2的程式輸入骰子的條件
並在最後輸入比大小的條件

同理進入button4的程式
依樣畫葫蘆仿照button2的程式
*記得改變數名稱

Ex:
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
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int d1, d2, sum = 0;
            Random ran = new Random();
            d1 = ran.Next(1, 7);
            d2 = ran.Next(1, 7);

            label1.Text = Convert.ToString(d1);
            textBox1.Text = Convert.ToString(d2);

            for (int i = 0; i <= d1 + d2; i++)
            {
                sum = sum + d1 + d2;
                Thread.Sleep(1000);
                Application.DoEvents();

                button1.Left = button1.Left + i;

            }

            if (button1.Left >= 80)
                MessageBox.Show("car1贏");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int d3, d4, sum = 0;
            Random ran = new Random();
            d3 = ran.Next(1, 7);
            d4 = ran.Next(1, 7);

            label1.Text = Convert.ToString(d3);
            textBox1.Text = Convert.ToString(d4);

            for (int i = 0; i <= d3+d4; i++)
            {
                sum = sum + d3 + d4;
                Thread.Sleep(1000);
                Application.DoEvents();

                button3.Left = button3.Left + i;
            }

            if (button3.Left >= 80)
                MessageBox.Show("car2贏");
        }
    }
}





2014/10/3

10/3第三週 物體來回移動

10/3第三週  物體來回移動

visual C++

首先,先至工具箱建立兩個button、一個Timer

進入button2程式
先在public partial class Form1 : Form裡 輸入int c=0,s;
在到button2輸入移動的條件

然後回到表單點入Timer的程式
將剛剛的條件複製進來,即可執行

Ex:
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 c, s = 1;

        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button1.Left > 430)
            {
                s = -1;
            }
            c = c + s;
            button1.Left = 50 * c;

            if (button1.Left < 0)
            {
                s = 1;
            }
            c = c + s;
            button1.Left = 50 * c;


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (button1.Left > 430)
            {
                s = -1;
            }
            c = c + s;
            button1.Left = 50 * c;

            if (button1.Left < 0)
            {
                s = 1;
            }
            c = c + s;
            button1.Left = 50 * c;

        }
    }
}







Excel VB

首先,至檢視點選錄製聚集
在表單輸入1後按Enter,並至檢視停止錄製聚集
在點選檢視的檢視聚集做編輯
左上點選插入-->自訂表單
選擇空白按鈕並新增兩個
點選buttin2後進入程式輸入條件,即可執行
Ex:
Dim c, d As Integer

Private Sub CommandButton2_Click()

If CommandButton1.Left > 150 Then
d = -1
End If
If CommandButton1.Left < 50 Then
d = 1
End If
c = c + d
CommandButton1.Left = 50 * c

End Sub