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

        }
    }