2015年1月8日 星期四

小遊戲

接觸C#差不多半年了,這是我寫的小遊戲(充滿不是bug的bug)
遊戲遊5*5的按鈕組成,玩法類似於井字遊戲或是五子棋,兩個或三個玩家共玩,四個連線就贏了,而且可以穿過牆壁(斜的也算),有點難度(已經證實兩個玩家的時候,第一個下的人很容易贏)
以下是程式碼://兩個人的時候
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 h
{
    
    public partial class Form1 : Form
    {

        string c;
        int r=1;//r=round,
        int[] a = new int[10];
        Button[,] b = new Button[5, 5];
        int[,] w = new int[5, 5];
        int[] n = new int[8];//n=number
        Random r1 = new Random();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            
            for (int j = 0; j < 5; j++)
            {
                for (int i = 0; i < 5; i++)
                {
                    b[i, j] = new System.Windows.Forms.Button();
                    b[i, j].Location = new Point(50+50 * i,50+50 * j);
                    b[i, j].Click += new EventHandler(b_Click);
                    b[i, j].Width = 50;
                    b[i, j].Height = 50;
                    b[i, j].Text = "";
                    b[i, j].BackColor = button1.BackColor;
                    w[i, j] = 0;
                    this.Controls.Add(b[i, j]);
                    
                }
            }
            button1.Left=300;
            button1.Top=240;
            label1.Left=350;
            label1.Top=181;
            label2.Left=310;
            label2.Top=181;
        }
        
        private void b_Click(object sender, EventArgs e)
        {
            
            Button bu =sender as Button ;
            if (r == 1 && bu.Text=="")
            {
                bu.Text = "O";
                label1.Text = "X";
                r = 2;
            }
            else if (r == 2 && bu.Text == "")
            {
                bu.Text = "X";
                //label1.Text = "O";
                //r = 1;
                label1.Text = "#";
                r = 3;
            }
            else if (r == 3 && bu.Text == "")
            {
                bu.Text = "#";
                label1.Text = "O";
                r = 1;
            }
            win();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int j = 0; j < 5; j++)
            {
                for (int i = 0; i < 5; i++)
                {

                    b[i, j].Text = "";
                    b[i, j].BackColor = button1.BackColor;
                }
            }
            label1.Text = "O";
            r = 1;
        }
        private void win()
        {
            for (int i1 = 0; i1 < 5; i1++)
            {
                for (int j1 = 0; j1 < 5; j1++)
                {
                    if (b[i1, j1].Text != "")
                    {
                        for (int i2 = 1; i2 <= 3; i2++)
                        {
                            if (b[(i1 + 0 * i2) % 5, (j1 + 1 * i2) % 5].Text == b[i1, j1].Text) { n[0]++; }
                            if (b[(i1 + 1 * i2) % 5, (j1 + 1 * i2) % 5].Text == b[i1, j1].Text) { n[1]++; }
                            if (b[(i1 + 1 * i2) % 5, (j1 + 0 * i2) % 5].Text == b[i1, j1].Text) { n[2]++; }
                            if (b[(i1 + 1 * i2) % 5, (j1 + 4 * i2) % 5].Text == b[i1, j1].Text) { n[3]++; }
                        }
                        for (int j2 = 0; j2 < 4; j2++)
                        {
                            if (n[j2] < 3) { n[j2] = 0; }
                            else if (n[j2] >= 3)
                            {
                                b[i1, j1].BackColor = button2.BackColor;
                                if (n[0] >= 3)
                                {
                                    for (int i2 = 1; i2 <= 3; i2++)
                                    {
                                        b[(i1 + 0 * i2) % 5, (j1 + 1 * i2) % 5].BackColor = button2.BackColor;
                                    }
                                }
                                if (n[1] >= 3)
                                {
                                    for (int i2 = 1; i2 <= 3; i2++)
                                    {
                                        b[(i1 + 1 * i2) % 5, (j1 + 1 * i2) % 5].BackColor = button2.BackColor;
                                    }
                                }
                                if (n[2] >= 3)
                                {
                                    for (int i2 = 1; i2 <= 3; i2++)
                                    {
                                        b[(i1 + 1 * i2) % 5, (j1 + 0 * i2) % 5].BackColor = button2.BackColor;
                                    }
                                }
                                if (n[3] >= 3)
                                {
                                    for (int i2 = 1; i2 <= 3; i2++)
                                    {
                                        b[(i1 + 1 * i2) % 5, (j1 + 4 * i2) % 5].BackColor = button2.BackColor;
                                    }
                                }
                                n[0] = 0;
                                n[1] = 0;
                                n[2] = 0;
                                n[3] = 0;
                                if (b[i1, j1].Text == "O")
                                {
                                    MessageBox.Show("O win");
                                }
                                if (b[i1, j1].Text == "X")
                                {
                                    MessageBox.Show("X win");
                                }
                                if (b[i1, j1].Text == "#")
                                {
                                    MessageBox.Show("# win");
                                }
                                j1 = 5;
                                i1 = 5;
                            }
                        }
                    }
                }
            }
        }
    }
}

不重複亂數 方法二

public partial class Form1 : Form
    {
        int i, j,i1,i2,w;
        Button[,] b = new Button[5, 5];
        Random r1 = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int j = 0; j < 5; j++)
            {
                for (int i = 0; i < 5; i++)
                {
                    b[i, j] = new System.Windows.Forms.Button();
                 
                    b[i, j].Click += new EventHandler(buttons_Click);
                    b[i, j].Width = 50;
                    b[i, j].Height = 50;
                    b[i, j].Text = "";//一開始不給名子
                 
                }
            }
            for (int j = 0; j < 5; j++)
            {
                for (int i = 0; i < 5; i++)
                {
                    i1 = r1.Next(0, 5);
                    i2 = r1.Next(0, 5);
                    do{
                        i1 = r1.Next(0, 5);
                        i2 = r1.Next(0, 5);

                    }while(b[i1, i2].Text!="");//亂數到再給名子,如果已經有了就在重新亂數
                    b[i1, i2].Location = new Point(200 + 50 * i, 200 + 50 * j);
                    this.Controls.Add(b[i1, i2]);
                    b[i1, i2].Text =""+ (5 * i1 + i2);
                }
            }
            b[0, 0].Enabled = false;

        }//後面是最數字推盤遊戲用的程式碼
        private void buttons_Click(object sender, EventArgs e)
        {

         
            Button but = sender as Button;



            if (((but.Left == b[0, 0].Left + 50 || but.Left == b[0, 0].Left - 50) && but.Top == b[0, 0].Top) || ((but.Top == b[0, 0].Top + 50 || but.Top == b[0, 0].Top - 50) && but.Left == b[0, 0].Left))
            {
                i1 = but.Left;
                i2 = but.Top;
                but.Left = b[0, 0].Left;
                but.Top = b[0, 0].Top;
                b[0, 0].Left = i1;
                b[0, 0].Top = i2;
            }

                w = 0;
                for (i = 0; i < 5; i++)
                {
                    for (j = 0; j < 5; j++)
                    {
                        i2 = (b[i, j].Left - 200) / 50;
                        i1 = (b[i, j].Top - 200) / 50;
                        if (b[i, j].Text == "" + (5 * i2 + i1))
                        {
                            w = w + 1;
                        }
                    }
                }

                if (w == 25) { MessageBox.Show("win"); }
             

        }
    }

2014年12月4日 星期四

物件矩陣與不重複亂數 方法一

public partial class Form1 : Form
    {
        string c;
        int[] a = new int[10];
        Button[,] b = new Button[5, 5];
        int[,] w=new int[5,5];
        Random r1 = new Random();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
         
            for (int j = 0; j < 5; j++)
            {
                for (int i = 0; i < 5; i++)
                {
                    b[i, j] = new System.Windows.Forms.Button();
                    b[i, j].Location = new Point(50 * i, 50 * j);
                    b[i, j].Click += new EventHandler(b_Click);
                    b[i, j].Width = 50;
                    b[i, j].Height = 50;
                    b[i, j].Text = "" + (i + j * 5);
                    w[i, j] = i + j * 5;//w[i,j]可以用來紀錄b[i,j]有關的數值
                    this.Controls.Add(b[i, j]);
                 
                }
            }
            for (int j = 0; j < 25; j++)
            {
                for (int i = 0; i < j; i++)
                {
                    a[0] = r1.Next(0, j);//a[0]從0到j
                    a[1] = a[0] % 5;//a[1],a[2]從0到4
                    a[2] = (a[0] - a[1]) / 5;
                    c = b[a[1], a[2]].Text;
                    b[a[1], a[2]].Text = b[i % 5, i / 5].Text;
                    b[i % 5, i / 5].Text = c;//被隨機到的按鈕與第i個交換名稱

                }
            }
        }
    }
//對於1,2,..,n的數列,被分為未打亂與已打亂,每次從未打亂的數列中隨機挑一個數到已打亂數列裡面,n次之後就會得到隨機不重複數列

2014年11月20日 星期四

賽馬遊戲與亂數


public partial class Form1 : Form
    {
        int i,j=0,k,r;
        public Form1()
        {
            InitializeComponent();
         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random random1 = new Random();
            //這邊Random為一種物件類別,宣告random1為此類別
            if (j == 0)
            {
                j = 1;
                i = random1.Next(1, 7);
                label1.Text = i.ToString();
                k = (Form1.ActiveForm.Size.Width - pictureBox1.Left) / 32;
                for (r = 1; r <= i; r++)
                {
                    pictureBox1.Left = pictureBox1.Left + k;
                    Application.DoEvents();
                    Thread.Sleep(100);
                    Application.DoEvents();

                    // Thread.Sleep(100)可讓程式等待0.1秒
                    //Application.DoEvents();可在等待過程中執行其他行程式

                    if (pictureBox1.Left+ pictureBox1.Width >= Form1.ActiveForm.Size.Width) {
                        r = i + 1;
                        j = 2;
                        MessageBox.Show("play1 win");
                    }
                }
             
            }
            else if (j == 1)
            {
                j = 0;
                i = random1.Next(1, 7);
                label2.Text = i.ToString();
                k = (Form1.ActiveForm.Size.Width - pictureBox1.Left) / 32;
                for (r = 1; r <= i; r++)
                {
                    pictureBox2.Left = pictureBox2.Left + k;
                    Application.DoEvents();
                    Thread.Sleep(100);
                    Application.DoEvents();
                    if (pictureBox2.Left +pictureBox2.Width >= Form1.ActiveForm.Size.Width)
                    {
                        r = i + 1;
                        j = 2;
                        MessageBox.Show("play2 win");
                    }
                }
             
            }
        }
-------------------------------------------------------------------------------------
執行畫面

小算盤與副程式

public partial class Form1 : Form
    {
        int a = 0, b = 0,i=4;
        float a1 = 0, b1 = 0, c1 = 1;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text ="+";
            if (a == 0) {
                a = 1;
                textBox2.Visible = true;
                textBox3.Visible = true;
                b = 0;
                button17.Enabled = true;
                c1 = 1;
            }
            i = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text ="-";
            if (a == 0)
            {
                a = 1;
                textBox2.Visible = true;
                textBox3.Visible = true;
                b = 0;
                button17.Enabled = true;
                c1 = 1;
            }
            i = 1;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text ="*";
            if (a == 0)
            {
                a = 1;
                textBox2.Visible = true;
                textBox3.Visible = true;
                b = 0;
                button17.Enabled = true;
                c1 = 1;
            }
            i = 2;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox2.Text ="/";
            if (a == 0)
            {
                a = 1;
                textBox2.Visible = true;
                textBox3.Visible = true;
                b = 0;
                button17.Enabled = true;
                c1 = 1;
            }
            i = 3;
        }
        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            a = 0;
            b = 0;
            a1 = 0;
            b1 = 0;
            c1 = 1;
            i = 4;
            textBox2.Visible = false;
            textBox3.Visible = false;
            textBox4.Visible = false;
            button17.Enabled = true;
        }
        private void button6_Click(object sender, EventArgs e)
        {
            add(0);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox4.Visible = true;
            if (i == 0)
            {
                c1 = a1 + b1;
                textBox4.Text = "" + c1;
            }
            if (i == 1)
            {
                c1 = a1 - b1;
                textBox4.Text = "" + c1;
            }
            if (i == 2)
            {
                c1 = a1 * b1;
                textBox4.Text = "" + c1;
            }
            if (i == 3)
            {
                if (b1 != 0)
                {
                    c1 = a1 / b1;
                    textBox4.Text = "" + c1;
                }
                else if (b1 == 0) { textBox4.Text = "error!"; }
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            add(1);
        }

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

        private void button10_Click(object sender, EventArgs e)
        {
            add(3);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            add(4);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            add(5);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            add(6);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            add(7);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            add(8);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            add(9);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            b = 1;
            button17.Enabled = false;
            if (a == 0) { textBox1.Text = textBox1.Text + "."; }
            if (a == 1)
            {
                textBox3.Text = textBox3.Text + ".";
            }
        }
        private void add(int i)//這邊宣告了副程式add(int i)
        {
            if (a == 0)
            {
                textBox1.Text = textBox1.Text +i;
                if (b == 0)
                {
                    a1 = a1 * 10 + i;
                }
                if (b == 1)
                {
                    c1 = c1 / 10;
                    a1 = a1 + i * c1;
                }
            }
            if (a == 1)
            {
                textBox3.Text = textBox3.Text+i ;
                if (b == 0)
                {
                    b1 = b1 * 10 + i;
                }
                if (b == 1)
                {
                    c1 = c1 / 10;
                    b1 = b1 + i * c1;
                }
            }
        }
    }
--------------------------------------------------------------------------------------------

井字遊戲、副程式與物件陣列

public partial class Form1 : Form
    {
        int n = 0, m = 0;
        int i=1, j=1;
        Button[,] buttons=new Button[3,3];//3,3為(0~2,0~2)
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
                for (int j = 0; j < 3; j++)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        buttons[i, j] = new System.Windows.Forms.Button();
                        buttons[i, j].Location = new Point(50 * i, 50 * j);
                        buttons[i, j].Click += new EventHandler(buttons_Click);//宣告點擊事件
                        buttons[i, j].Width = 50;
                        buttons[i, j].Height = 50;
                        this.Controls.Add(buttons[i, j]);//將物件顯示
                    }
                }
        }

        private void buttons_Click(object sender, EventArgs e)//當事件被處發時,處發者為sander
        {

            Button but = sender as Button;//宣告but為跟sander一樣的按鈕
            if (n == 0) { n = 1; but.Text = "O"; }
            else if (n == 1) { n = 0; but.Text = "X"; }
            but.Enabled = false;
            for (m = 0; m <= 2; m++)
            {
                    if (buttons[m, 0].Text == buttons[m, 1].Text && buttons[m, 0].Text == buttons[m, 2].Text && buttons[m, 0].Text != "")
                    {
                        if (n == 1) { MessageBox.Show("O勝利"); } else if (n == 0) { MessageBox.Show("X勝利"); }
                    }
                    if (buttons[0, m].Text == buttons[1, m].Text && buttons[0, m].Text == buttons[2, m].Text && buttons[0, m].Text != "")
                    {
                        if (n == 1) { MessageBox.Show("O勝利"); } else if (n == 0) { MessageBox.Show("X勝利"); }
                    }
            }
            if (buttons[0, 0].Text == buttons[1, 1].Text && buttons[0, 0].Text == buttons[2, 2].Text && buttons[0, 0].Text != "")
            {
                if (n == 1) { MessageBox.Show("O勝利"); } else if (n == 0) { MessageBox.Show("X勝利"); }
            }
            if (buttons[0, 2].Text == buttons[1, 1].Text && buttons[0, 2].Text == buttons[2, 0].Text && buttons[0, 2].Text != "")
            {
                if (n == 1) { MessageBox.Show("O勝利"); } else if (n == 0) { MessageBox.Show("X勝利"); }
            }
        }
    }
--------------------------------------------------------------------