Игра пятнашки, как проверить где пустое поле для перемещения соседних кнопок - C#
Формулировка задачи:
Листинг программы
- 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 Fifteen
- {
- public partial class Form1 : Form
- {
- int[,] field=new int[4,4];
- int[] mass=new int[16];
- Random myRand = new Random();
- Button[] ButtonArray = new Button[16];
- List<int> array =new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 };
- public Form1()
- {
- InitializeComponent();
- int x = 0;
- int y = 0;
- for (int i = 0; i < ButtonArray.Count(); i++)
- {
- ButtonArray[i] = new Button();
- int randomNumber = myRand.Next(array.Count);
- ButtonArray[i].Text = array[randomNumber].ToString();
- array.RemoveAt(randomNumber);
- switch (i)
- {
- case 4: y = y + 40;
- x = 0;
- break;
- case 8: y = y + 40;
- x = 0;
- break;
- case 12: y = y + 40;
- x = 0;
- break;
- }
- ButtonArray[i].SetBounds(x, y, 50, 40);
- x = x + 50;
- if (ButtonArray[i].Text == "0")
- ButtonArray[i].Visible = false;
- this.Controls.Add(ButtonArray[i]);
- this.ButtonArray[i].Click +=new EventHandler(Button_Click);
- }
- }
- private void Button_Click(object sender, EventArgs e)
- {
- Button bt = (sender) as Button;
- Check();
- for (int i = 0; i < ButtonArray.Count(); i++)
- {
- if (ButtonArray[i].Text == "0")
- {
- Point pt = bt.Location;
- bt.Location = ButtonArray[i].Location;
- ButtonArray[i].Location = pt;
- }
- }
- }
- }
- }
неужели ни кто не знает(
Решение задачи: «Игра пятнашки, как проверить где пустое поле для перемещения соседних кнопок»
textual
Листинг программы
- private void Button_Click(object sender, EventArgs e)
- {
- Button bt = (sender) as Button;
- Button bt0 = new Button();
- int a=0,b=0;
- for (int i = 0; i < ButtonArray.Count(); i++)
- {
- if (ButtonArray[i].Text == "0")
- {
- bt0 = ButtonArray[i];
- }
- }
- if (bt0.Location.X == bt.Location.X + 50 && bt0.Location.Y == bt.Location.Y ||
- bt0.Location.X == bt.Location.X - 50 && bt0.Location.Y == bt.Location.Y ||
- bt0.Location.Y == bt.Location.Y + 40 && bt0.Location.X == bt.Location.X ||
- bt0.Location.Y == bt.Location.Y - 40 && bt0.Location.X == bt.Location.X)
- {
- for (int i = 0; i < ButtonArray.Count(); i++)
- {
- if (ButtonArray[i].Text == "0")
- {
- Point pt = bt.Location;
- bt.Location = ButtonArray[i].Location;
- ButtonArray[i].Location = pt;
- }
- if (mass[i] == 0)
- a = i;
- if (mass[i] == Convert.ToInt16(bt.Text))
- b = i;
- }
- mass[a] = mass[b];
- mass[b] = 0;
- a = 0;
- for (int j = 0; j < mass.Count(); j++)
- {
- if (j+1 == mass[j])
- a++;
- if (a == 15)
- MessageBox.Show("YOU WIN!");
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д