2015年10月30日 星期五

推盤子

勝利畫面思考中ing


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const int buttonXSize = 2;
        const int buttonYSize = 2;
        const int buttonStartX = 10;
        const int buttonStartY = 10;
        const int buttonWidth = 50;
        const int buttonHeight = 50;

        Button[,] newBtn = new Button[buttonXSize, buttonYSize];
        Button startButton;
        Random rnd = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int count = 0;

            for (int i = 0; i < buttonXSize; i++)
            {
                for (int j = 0; j < buttonYSize; j++)
                {
                    newBtn[i, j] = new Button();
                    newBtn[i, j].Location = new Point(buttonStartX + buttonWidth * i, buttonStartY + buttonHeight * j);
                    newBtn[i, j].Text = (count + 1).ToString();
                    newBtn[i, j].Name = newBtn[i, j].Text;
                    newBtn[i, j].Size = new Size(buttonWidth, buttonHeight);
                    newBtn[i, j].TabIndex = count + 1;
                    this.Controls.Add(newBtn[i, j]);
                    newBtn[i, j].Click += new System.EventHandler(button1_Click);
                    count++;
                }
            }

            startButton = new Button();
            startButton.Location = new Point(buttonStartX + buttonWidth * (buttonXSize + 2), buttonStartY + buttonHeight * buttonYSize);
            startButton.Text = (count + 1).ToString();
            startButton.Name = startButton.Text;
            startButton.Size = new Size(buttonWidth * 2, buttonHeight * 2);
            startButton.TabIndex = count + 1;
            this.Controls.Add(startButton);
            startButton.Click += new System.EventHandler(startbutton_Click);
            startButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
            startButton.BackgroundImage = Image.FromFile("C:\\Users\\student\\Downloads\\IMAG0003.jpg");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool act = true;
            int n = Int32.Parse(((Button)sender).Name) - 1;

            int x = n / buttonYSize;
            int y = n % buttonYSize;

            if (x + 1 < buttonXSize && act)
            {
                if (newBtn[x + 1, y].Text == "0")
                {
                    change(x, y, x + 1, y);
                    act = false;
                    return;
                }
            }
            if (y + 1 < buttonYSize && act)
            {
                if (newBtn[x, y + 1].Text == "0")
                {
                    change(x, y, x, y + 1);
                    act = false;
                    return;
                }
            }
            if (x - 1 >= 0 && act)
            {
                if (newBtn[x - 1, y].Text == "0")
                {
                    change(x, y, x - 1, y);
                    act = false;
                    return;
                }
            }
            if (y - 1 >= 0 && act)
            {
                if (newBtn[x, y - 1].Text == "0")
                {
                    change(x, y, x, y - 1);
                    act = false;
                    return;
                }
            }
        }

        private void change(int x1, int y1, int x2, int y2)
        {
            string s = newBtn[x1, y1].Text;
            newBtn[x1, y1].Text = newBtn[x2, y2].Text;
            newBtn[x2, y2].Text = s;
            if (newBtn[x1, y1].Text == "0")
                newBtn[x1, y1].Hide();
            else
                newBtn[x1, y1].Show();
            if (newBtn[x2, y2].Text == "0")
                newBtn[x2, y2].Hide();
            else
                newBtn[x2, y2].Show();

            bool iswin = true;
            for (int i = 0; i < buttonXSize; i++)
            {
                for (int j = 0; j < buttonYSize; j++)
                {
                    if (newBtn[i, j].Text != (i * buttonYSize + j + 1).ToString())
                        iswin = false;
                }
            }
            if (iswin)
                MessageBox.Show("Win !!");
        }

        private void startbutton_Click(object sender, EventArgs e)
        {
            int[] arr = new int[buttonXSize * buttonYSize + 1];
            for (int i = 0; i < buttonXSize * buttonYSize + 1; i++)
                arr[i] = i;

            for (int i = 0; i < buttonXSize * buttonYSize; i++)
            {
                int rand = rnd.Next(0, buttonXSize * buttonYSize - 1);
                if (rand == i)
                    continue;
                int tem = arr[rand];
                arr[rand] = arr[i];
                arr[i] = tem;
            }

            for (int i = 0; i < buttonXSize; i++)
            {
                for (int j = 0; j < buttonYSize; j++)
                {
                    newBtn[i, j].Text = arr[i * buttonYSize + j].ToString();
                    if (newBtn[i, j].Text == "0")
                        newBtn[i, j].Hide();
                    else
                        newBtn[i, j].Show();
                }
            }
        }
    }
}

沒有留言:

張貼留言