2014年12月29日 星期一

推格子遊戲

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

        public System.Windows.Forms.Button Button1, Button2, Button3, Button4, Button5;
        Button[,] buttons = new Button[50, 50];
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Visible = false;
            Button1 = new Button();
            Button1.Location = new Point(150, 60);
            Button1.Size = new Size(170, 50);
            Button1.Text = "EASY";
            Button1.BackColor = Color.White;
            this.Controls.Add(Button1);
            Button1.Click += new EventHandler(Button1_Click);
            Button2 = new Button();
            Button2.Location = new Point(150, 130);
            Button2.Size = new Size(170, 50);
            Button2.Text = "NORMAL";
            Button2.BackColor = Color.White;
            this.Controls.Add(Button2);
            Button2.Click += new EventHandler(Button2_Click);
            Button3 = new Button();
            Button3.Location = new Point(150, 200);
            Button3.Size = new Size(170, 50);
            Button3.Text = "HARD";
            Button3.BackColor = Color.White;
            this.Controls.Add(Button3);
            Button3.Click += new EventHandler(Button3_Click);
           
        }      

        private void Button1_Click(object sender, EventArgs e)
        {
            Button1.Visible = false;
            Button2.Visible = false;
            Button3.Visible = false;
            Button4 = new Button();
            Button4.Location = new Point(200, 20);
            Button4.Size = new Size(80, 30);
            Button4.Text = "檢測輸贏";
            Button4.BackColor = Color.White;
            this.Controls.Add(Button4);
            Button4.Click += new EventHandler(b_Click);
            Button5 = new Button();
            Button5.Location = new Point(200, 50);
            Button5.Size = new Size(80, 30);
            Button5.Text = "重新開始";
            Button5.BackColor = Color.White;
            this.Controls.Add(Button5);
            Button5.Click += new EventHandler(c_Click);
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 4; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(40 + 30 * (i - 1), 103 + 30 * (j - 1));
                    buttons[i, j].Size = new Size(30, 30);
                    buttons[i, j].Text = " ";
                    buttons[i, j].BackColor = Color.White;
                    this.Controls.Add(buttons[i, j]);
                    buttons[i, j].Click += new EventHandler(a1_Click);
                }
            }
            for (int j = 1; j < 4; j++)
            {
                buttons[1, j].Text = "O";
                buttons[13, j].Text = "X";
                buttons[1, j].Enabled = false;
                buttons[13, j].Enabled = false;
            }
            label2.Text = "3";          
        }
        int c1 = 0;
        private void a1_Click(object sender, EventArgs e)
        {
            int x, y;
            x = Form1.ActiveForm.Location.X + 50;
            y = Form1.ActiveForm.Location.Y + 140;
            c1++;
            //MessageBox.Show(Convert.ToString(c));
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 4; j++)
                {
                    if (MousePosition.X >= x + (i - 1) * 30 && MousePosition.X < x + i * 30 && MousePosition.Y >= y + (j - 1) * 30 && MousePosition.Y < y + j * 30)
                    {
                        if (c1 % 2 == 1)
                        {
                            for (int a = 1; a <= i; a++)
                            {
                                buttons[a, j].Text = " ";
                                buttons[a, j].Enabled = false;
                            }
                            buttons[i, j].Text = "O";
                        }
                        if (c1 % 2 == 0)
                        {
                            for (int a = 13; a >= i; a--)
                            {
                                buttons[a, j].Text = " ";
                                buttons[a, j].Enabled = false;
                            }
                            buttons[i, j].Text = "X";
                        }
                    }                  
                }
            }
            //MessageBox.Show(Convert.ToString(MousePosition)+Convert.ToString(Form1.ActiveForm.Location));
            Button4.Enabled = true;
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 4; j++)
                {
                    if (buttons[i, j].Enabled == true)
                        Button4.Enabled = false;
                }
            }
            label1.Text = Convert.ToString(c1);
        }
        private void Button2_Click(object sender, EventArgs e)
        {   Button1.Visible = false;
            Button2.Visible = false;
            Button3.Visible = false;
            Button4 = new Button();
            Button4.Location = new Point(200, 20);
            Button4.Size = new Size(80, 30);
            Button4.Text = "檢測輸贏";
            Button4.BackColor = Color.White;
            this.Controls.Add(Button4);
            Button4.Click += new EventHandler(b_Click);
            Button5 = new Button();
            Button5.Location = new Point(200, 50);
            Button5.Size = new Size(80, 30);
            Button5.Text = "重新開始";
            Button5.BackColor = Color.White;
            this.Controls.Add(Button5);
            Button5.Click += new EventHandler(c_Click);
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 6; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(40 + 30 * (i - 1), 103 + 30 * (j - 1));
                    buttons[i, j].Size = new Size(30, 30);
                    buttons[i, j].Text = " ";
                    buttons[i, j].BackColor = Color.White;
                    this.Controls.Add(buttons[i, j]);
                    buttons[i, j].Click += new EventHandler(a2_Click);
                }
            }
            for (int j = 1; j < 6; j++)
            {
                buttons[1, j].Text = "O";
                buttons[13, j].Text = "X";
                buttons[1, j].Enabled = false;
                buttons[13, j].Enabled = false;
            }
            label2.Text = "5";          
        }
        int c2 = 0;
        private void a2_Click(object sender, EventArgs e)
        {
            int x, y;
            x = Form1.ActiveForm.Location.X + 50;
            y = Form1.ActiveForm.Location.Y + 140;
            c2++;
            //MessageBox.Show(Convert.ToString(c));
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 6; j++)
                {
                    if (MousePosition.X >= x + (i - 1) * 30 && MousePosition.X < x + i * 30 && MousePosition.Y >= y + (j - 1) * 30 && MousePosition.Y < y + j * 30)
                    {
                        if (c2 % 2 == 1)
                        {
                            for (int a = 1; a <= i; a++)
                            {
                                buttons[a, j].Text = " ";
                                buttons[a, j].Enabled = false;
                            }
                            buttons[i, j].Text = "O";
                        }
                        if (c2 % 2 == 0)
                        {
                            for (int a = 13; a >= i; a--)
                            {
                                buttons[a, j].Text = " ";
                                buttons[a, j].Enabled = false;
                            }
                            buttons[i, j].Text = "X";
                        }
                    }
                }
            }
            //MessageBox.Show(Convert.ToString(MousePosition)+Convert.ToString(Form1.ActiveForm.Location));
            Button4.Enabled = true;
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 6; j++)
                {
                    if (buttons[i, j].Enabled == true)
                        Button4.Enabled = false;
                }
            }
            label1.Text = Convert.ToString(c2);
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            Button1.Visible = false;
            Button2.Visible = false;
            Button3.Visible = false;
            Button4 = new Button();
            Button4.Location = new Point(200, 20);
            Button4.Size = new Size(80, 30);
            Button4.Text = "檢測輸贏";
            Button4.BackColor = Color.White;
            this.Controls.Add(Button4);
            Button4.Click += new EventHandler(b_Click);
            Button5 = new Button();
            Button5.Location = new Point(200, 50);
            Button5.Size = new Size(80, 30);
            Button5.Text = "重新開始";
            Button5.BackColor = Color.White;
            this.Controls.Add(Button5);
            Button5.Click += new EventHandler(c_Click);
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 8; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(40 + 30 * (i - 1), 103 + 30 * (j - 1));
                    buttons[i, j].Size = new Size(30, 30);
                    buttons[i, j].Text = " ";
                    buttons[i, j].BackColor = Color.White;
                    this.Controls.Add(buttons[i, j]);
                    buttons[i, j].Click += new EventHandler(a3_Click);
                }
            }
            for (int j = 1; j < 8; j++)
            {
                buttons[1, j].Text = "O";
                buttons[13, j].Text = "X";
                buttons[1, j].Enabled = false;
                buttons[13, j].Enabled = false;
            }
            label2.Text = "7";
        }
        int c3 = 0;
        private void a3_Click(object sender, EventArgs e)
        {
            int x, y;
            x = Form1.ActiveForm.Location.X + 50;
            y = Form1.ActiveForm.Location.Y + 140;
            c3++;
            //MessageBox.Show(Convert.ToString(c));
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 8; j++)
                {
                    if (MousePosition.X >= x + (i - 1) * 30 && MousePosition.X < x + i * 30 && MousePosition.Y >= y + (j - 1) * 30 && MousePosition.Y < y + j * 30)
                    {
                        if (c3 % 2 == 1)
                        {
                            for (int a = 1; a <= i; a++)
                            {
                                buttons[a, j].Text = " ";
                                buttons[a, j].Enabled = false;
                            }
                            buttons[i, j].Text = "O";
                        }
                        if (c3 % 2 == 0)
                        {
                            for (int a = 13; a >= i; a--)
                            {
                                buttons[a, j].Text = " ";
                                buttons[a, j].Enabled = false;
                            }
                            buttons[i, j].Text = "X";
                        }
                    }
                }
            }
            //MessageBox.Show(Convert.ToString(MousePosition)+Convert.ToString(Form1.ActiveForm.Location));
            Button4.Enabled = true;
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < 8; j++)
                {
                    if (buttons[i, j].Enabled == true)
                        Button4.Enabled = false;
                }
            }
            label1.Text = Convert.ToString(c3);
        }

        private void b_Click(object sender, EventArgs e)
        {
            int d;
            d = Convert.ToInt32(label1.Text);
            if (d % 2 == 0)
                MessageBox.Show("X WIN");
            else
                MessageBox.Show("O WIN");
        }

        private void c_Click(object sender, EventArgs e)
        {
            button1.Visible = false;
            Button1 = new Button();
            Button1.Location = new Point(150, 60);
            Button1.Size = new Size(170, 50);
            Button1.Text = "EASY";
            Button1.BackColor = Color.White;
            this.Controls.Add(Button1);
            Button1.Click += new EventHandler(Button1_Click);
            Button2 = new Button();
            Button2.Location = new Point(150, 130);
            Button2.Size = new Size(170, 50);
            Button2.Text = "NORMAL";
            Button2.BackColor = Color.White;
            this.Controls.Add(Button2);
            Button2.Click += new EventHandler(Button2_Click);
            Button3 = new Button();
            Button3.Location = new Point(150, 200);
            Button3.Size = new Size(170, 50);
            Button3.Text = "HARD";
            Button3.BackColor = Color.White;
            this.Controls.Add(Button3);
            Button3.Click += new EventHandler(Button3_Click);
            int a;
            a = Convert.ToInt32(label2.Text);
            for (int i = 1; i < 14; i++)
            {
                for (int j = 1; j < a+1; j++)
                {
                    buttons[i, j].Visible = false;
                }
            }
            Button4.Visible = false;
            Button5.Visible = false;
        }
       
       
    }
}

沒有留言:

張貼留言