Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 _teste_2
- {
- public partial class Form1 : Form
- {
- int[] array = new int[9];
- int wincount = 0;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- upButtons();
- randoms();
- downButtons();
- button1.Enabled = false;
- }
- private void randoms()
- {
- Random rnd = new Random();
- for (int i = 1; i <= 9; i++)
- {
- int x = rnd.Next(0, 9);
- while (array[x] > 0)
- x = rnd.Next(0, 9);
- array[x] = i;
- }
- }
- private void upButtons()
- {
- for (int i = 1; i <= 9; i++)
- {
- Button upBtn = new Button();
- upBtn.Name = "upBtn" + i;
- upBtn.Text = "" + i;
- upBtn.Size = new System.Drawing.Size(30, 30);
- upBtn.Location = new System.Drawing.Point(120 + 40 * i, 70);
- upBtn.MouseDown += new MouseEventHandler(this.upBtn_MouseDown);
- upBtn.DragOver += new DragEventHandler(this.Btn_DragOver);
- upBtn.DragDrop += new DragEventHandler(this.Btn_DragDrop);
- upBtn.AllowDrop = true;
- Controls.Add(upBtn);
- }
- }
- private void downButtons()
- {
- int count = 1;
- for (int i = 1; i <= 3; i++)
- {
- for (int h = 1; h <= 3; h++)
- {
- Button downBtn = new Button();
- downBtn.Name = "downBtn" + array[count - 1];
- downBtn.Size = new System.Drawing.Size(50, 50);
- downBtn.Location = new System.Drawing.Point(220 + 60 * i, 100 +
- 60 * h);
- downBtn.BackColor = Color.Orange;
- downBtn.DragOver += new DragEventHandler(this.Btn_DragOver);
- downBtn.DragDrop += new DragEventHandler(this.Btn_DragDrop);
- downBtn.AllowDrop = true;
- Controls.Add(downBtn);
- count++;
- }
- }
- }
- private void upBtn_MouseDown(object sender, MouseEventArgs e)
- {
- DoDragDrop(sender, DragDropEffects.Move);
- }
- private void Btn_DragOver(object sender, DragEventArgs e)
- {
- if (e.Data.GetDataPresent(typeof(Button)))
- e.Effect = DragDropEffects.Move;
- else
- e.Effect = DragDropEffects.None;
- }
- private void Btn_DragDrop(object sender, DragEventArgs e)
- {
- var upBtn = e.Data.GetData(typeof(Button)) as Button;
- var downBtn = sender as Button;
- if (upBtn.Text[0] == downBtn.Name[7])
- {
- downBtn.Text = upBtn.Text;
- wincount++;
- }
- if (wincount == 9)
- {
- label1.Visible = true;
- button2.Focus();
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Environment.Exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment