Игра пятнашки, как проверить где пустое поле для перемещения соседних кнопок - C#

Узнай цену своей работы

Формулировка задачи:

Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace Fifteen
  10. {
  11. public partial class Form1 : Form
  12. {
  13. int[,] field=new int[4,4];
  14. int[] mass=new int[16];
  15. Random myRand = new Random();
  16. Button[] ButtonArray = new Button[16];
  17. List<int> array =new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 };
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. int x = 0;
  22. int y = 0;
  23. for (int i = 0; i < ButtonArray.Count(); i++)
  24. {
  25. ButtonArray[i] = new Button();
  26. int randomNumber = myRand.Next(array.Count);
  27. ButtonArray[i].Text = array[randomNumber].ToString();
  28. array.RemoveAt(randomNumber);
  29. switch (i)
  30. {
  31. case 4: y = y + 40;
  32. x = 0;
  33. break;
  34. case 8: y = y + 40;
  35. x = 0;
  36. break;
  37. case 12: y = y + 40;
  38. x = 0;
  39. break;
  40. }
  41.  
  42. ButtonArray[i].SetBounds(x, y, 50, 40);
  43. x = x + 50;
  44. if (ButtonArray[i].Text == "0")
  45. ButtonArray[i].Visible = false;
  46. this.Controls.Add(ButtonArray[i]);
  47. this.ButtonArray[i].Click +=new EventHandler(Button_Click);
  48. }
  49. }
  50.  
  51. private void Button_Click(object sender, EventArgs e)
  52. {
  53. Button bt = (sender) as Button;
  54. Check();
  55. for (int i = 0; i < ButtonArray.Count(); i++)
  56. {
  57. if (ButtonArray[i].Text == "0")
  58. {
  59. Point pt = bt.Location;
  60. bt.Location = ButtonArray[i].Location;
  61. ButtonArray[i].Location = pt;
  62. }
  63. }
  64.  
  65. }
  66.  
  67. }
  68. }
как проверить где пустое поле для перемещения соседних кнопок
неужели ни кто не знает(

Решение задачи: «Игра пятнашки, как проверить где пустое поле для перемещения соседних кнопок»

textual
Листинг программы
  1. private void Button_Click(object sender, EventArgs e)
  2.         {
  3.             Button bt = (sender) as Button;
  4.             Button bt0 = new Button();
  5.             int a=0,b=0;
  6.             for (int i = 0; i < ButtonArray.Count(); i++)
  7.             {
  8.                 if (ButtonArray[i].Text == "0")
  9.                 {
  10.                     bt0 = ButtonArray[i];
  11.                 }
  12.             }
  13.  
  14.             if (bt0.Location.X == bt.Location.X + 50 && bt0.Location.Y == bt.Location.Y ||
  15.                 bt0.Location.X == bt.Location.X - 50 && bt0.Location.Y == bt.Location.Y ||
  16.                 bt0.Location.Y == bt.Location.Y + 40 && bt0.Location.X == bt.Location.X ||
  17.                 bt0.Location.Y == bt.Location.Y - 40 && bt0.Location.X == bt.Location.X)
  18.             {
  19.                 for (int i = 0; i < ButtonArray.Count(); i++)
  20.                 {
  21.  
  22.                     if (ButtonArray[i].Text == "0")
  23.                     {
  24.                         Point pt = bt.Location;
  25.                         bt.Location = ButtonArray[i].Location;
  26.                         ButtonArray[i].Location = pt;
  27.                     }
  28.                     if (mass[i] == 0)
  29.                         a = i;
  30.                     if (mass[i] == Convert.ToInt16(bt.Text))
  31.                         b = i;
  32.                 }
  33.                 mass[a] = mass[b];
  34.                 mass[b] = 0;
  35.                 a = 0;
  36.                 for (int j = 0; j < mass.Count(); j++)
  37.                 {
  38.                     if (j+1 == mass[j])
  39.                         a++;
  40.                     if (a == 15)
  41.                         MessageBox.Show("YOU WIN!");
  42.                 }  
  43.             }
  44.         }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

10   голосов , оценка 3.9 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы