Как связать между собой 3 класса - C#

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

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

Подскажите пожалуйста, как можно связать между собой 3 класса.

Решение задачи: «Как связать между собой 3 класса»

textual
Листинг программы
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Input;
  4. using System.Windows.Shapes;
  5.  
  6. namespace Lines
  7. {
  8.     /// <summary>
  9.     /// Логика взаимодействия для MainWindow.xaml
  10.     /// </summary>
  11.     public partial class MainWindow
  12.     {
  13.        
  14.         public Verify vf;
  15.         private Point _pt;
  16.         public myShape ms = new myShape(vf);
  17.  
  18.         public MainWindow()
  19.         {
  20.            
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void Button_Click_1(object sender, RoutedEventArgs e)
  25.         {
  26.             if (Field.Children.Count == 101)
  27.             {
  28.                 Field.Children.RemoveRange(1, 101);
  29.                 ms.gridArray = new myShape[10,10];
  30.             }
  31.  
  32.             for (int i = 0; i < 10; i++)
  33.                 for (int j = 0; j < 10; j++)
  34.                 {
  35.                     ms.Start(i,j);
  36.                     Grid.SetRow(ms._el, i);
  37.                     Grid.SetColumn(ms._el, j);
  38.                     Field.Children.Add(ms._el);
  39.  
  40.                     ms._el.MouseLeftButtonDown += el_MouseLeftButtonDown;
  41.                     ms._el.MouseLeftButtonUp += el_MouseLeftButtonUp;
  42.                 }
  43.         }
  44.  
  45.         public void el_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  46.         {
  47.             ms._el = sender as Ellipse;
  48.  
  49.            _pt = e.GetPosition(this);
  50.  
  51.             var i = (int)(_pt.Y / 40);
  52.             var j = (int)((_pt.X - 129) / 40);
  53.  
  54.             ms.ms1 = new myShape(ms._el, ms.col, i, j);
  55.         }
  56.  
  57.         public void el_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  58.         {
  59.             ms._el = sender as Ellipse;
  60.  
  61.             _pt = e.GetPosition(this);
  62.  
  63.             var i = (int)(_pt.Y / 40);
  64.             var j = (int)((_pt.X - 129) / 40);
  65.  
  66.             ms.ms2 = new myShape(ms._el, ms.col, i, j);
  67.  
  68.             ms.Animation(ms.ms1, ms.ms2);
  69.         }
  70.  
  71.     }
  72. }
  73.  
  74. класс myShape
  75.  
  76. using System;
  77. using System.Collections.Generic;
  78. using System.Windows;
  79. using System.Windows.Controls;
  80. using System.Windows.Media;
  81. using System.Windows.Media.Animation;
  82. using System.Windows.Shapes;
  83.  
  84. namespace Lines
  85. {
  86.      public class myShape : Verify
  87.      {
  88.  
  89.          public Ellipse el;
  90.          public int x;
  91.          public int y;
  92.          public Color ch;
  93.  
  94.          public myShape(Ellipse El, Color Ch, int X, int Y)
  95.          {
  96.              ch = Ch;
  97.              el = El;
  98.              x = X;
  99.              y = Y;
  100.          }
  101.  
  102.          public myShape(Verify Vf)
  103.          {
  104.              vf = Vf;
  105.          }
  106.  
  107.          public myShape ms1, ms2;
  108.          public Verify vf;
  109.        
  110.          readonly Color[] _cl = { Colors.Black, Colors.Chartreuse, Colors.Blue, Colors.Crimson, Colors.Orchid, Colors.Yellow };
  111.          readonly Color[,] _fieldColors = new Color[10, 10];
  112.          public Ellipse _el;
  113.          public Color col;
  114.          readonly Random _rnd = new Random();
  115.          readonly DoubleAnimation _elW = new DoubleAnimation();
  116.          readonly DoubleAnimation _elH = new DoubleAnimation();
  117.          public myShape[,] gridArray = new myShape[10, 10];
  118.          public List<myShape> list = new List<myShape>();
  119.          
  120.         public void Start(int i, int j)
  121.         {
  122.             int color = _rnd.Next(0, 6);
  123.  
  124.             _fieldColors[i, j] = _cl[color];
  125.  
  126.             col = _cl[color];
  127.  
  128.             _el = new Ellipse { Width = 1, Height = 1, Fill = new SolidColorBrush(col) };
  129.            
  130.             _elW.From = _el.Width;
  131.             _elW.To = 35;
  132.             _elW.Duration = new Duration(TimeSpan.Parse("0:0:0,4"));
  133.  
  134.             _elH.From = _el.Height;
  135.             _elH.To = 35;
  136.             _elH.Duration = new Duration(TimeSpan.Parse("0:0:0,4"));
  137.  
  138.             _el.BeginAnimation(FrameworkElement.WidthProperty, _elW);
  139.             _el.BeginAnimation(FrameworkElement.HeightProperty, _elH);
  140.  
  141.             gridArray[i, j] = new myShape(_el, col, i, j);
  142.         }
  143.  
  144.         public void Animation(myShape ms1, myShape ms2)
  145.         {
  146.             var ta = new ThicknessAnimation();
  147.             var ta1 = new ThicknessAnimation();
  148.  
  149.             if (ms1.x == ms2.x)      //горизонтальоне перемещение  
  150.             {
  151.                 if (ms1.y + 1 == ms2.y)
  152.                 {
  153.                     ta.From = ms1.el.Margin;
  154.                     ta.To = new Thickness(ms1.el.Width + 5, 0, -ms1.el.Width - 5, 0);
  155.                     ta1.From = ms2.el.Margin;
  156.                     ta1.To = new Thickness(-ms2.el.Width - 5, 0, ms2.el.Width + 5, 0);
  157.  
  158.                     ta1.Completed += ta_Completed;
  159.  
  160.                     ta.Duration = new Duration(TimeSpan.Parse("0:0:2"));
  161.                     ta1.Duration = new Duration(TimeSpan.Parse("0:0:2"));
  162.  
  163.                     ms1.el.BeginAnimation(FrameworkElement.MarginProperty, ta);
  164.                     ms2.el.BeginAnimation(FrameworkElement.MarginProperty, ta1);
  165.                 }
  166.                 if (ms1.y - 1 == ms2.y)
  167.                 {
  168.                     ta.From = ms2.el.Margin;
  169.                     ta.To = new Thickness(ms2.el.Width + 5, 0, -ms2.el.Width - 5, 0);
  170.                     ta1.From = ms1.el.Margin;
  171.                     ta1.To = new Thickness(-ms1.el.Width - 5, 0, ms1.el.Width + 5, 0);
  172.  
  173.                     ta.Duration = new Duration(TimeSpan.Parse("0:0:2"));
  174.                     ta1.Duration = new Duration(TimeSpan.Parse("0:0:2"));
  175.  
  176.                     ta1.Completed += ta_Completed;
  177.  
  178.                     ms1.el.BeginAnimation(FrameworkElement.MarginProperty, ta1);
  179.                     ms2.el.BeginAnimation(FrameworkElement.MarginProperty, ta);
  180.                 }
  181.  
  182.             }
  183.             if (ms1.y == ms2.y)//вертикальное перемещение
  184.             {
  185.                 if (ms1.x + 1 == ms2.x)
  186.                 {
  187.                     ta.From = ms1.el.Margin;
  188.                     ta.To = new Thickness(0, ms1.el.Height + 5, 0, -ms1.el.Height - 5);
  189.                     ta1.From = ms2.el.Margin;
  190.                     ta1.To = new Thickness(0, -ms1.el.Height - 5, 0, ms1.el.Height + 5);
  191.  
  192.                     ta.Duration = new Duration(TimeSpan.Parse("0:0:2"));
  193.                     ta1.Duration = new Duration(TimeSpan.Parse("0:0:2"));
  194.  
  195.                     ta.Completed += ta_Completed;
  196.  
  197.                     ms1.el.BeginAnimation(FrameworkElement.MarginProperty, ta);
  198.                     ms2.el.BeginAnimation(FrameworkElement.MarginProperty, ta1);
  199.  
  200.                 }
  201.                 if (ms1.x - 1 == ms2.x)
  202.                 {
  203.                     ta.From = ms2.el.Margin;
  204.                     ta.To = new Thickness(0, ms2.el.Height + 5, 0, -ms2.el.Height - 5);
  205.                     ta1.From = ms1.el.Margin;
  206.                     ta1.To = new Thickness(0, -ms1.el.Height - 5, 0, ms1.el.Height + 5);
  207.  
  208.                     ta.Completed += ta_Completed;
  209.  
  210.                     ta.Duration = new Duration(TimeSpan.Parse("0:0:2"));
  211.                     ta1.Duration = new Duration(TimeSpan.Parse("0:0:2"));
  212.  
  213.                     ms1.el.BeginAnimation(FrameworkElement.MarginProperty, ta1);
  214.                     ms2.el.BeginAnimation(FrameworkElement.MarginProperty, ta);
  215.                 }
  216.             }
  217.         }
  218.  
  219.         void ta_Completed(object sender, EventArgs e)
  220.         {
  221.             ms1.el.BeginAnimation(FrameworkElement.MarginProperty, null);
  222.             ms2.el.BeginAnimation(FrameworkElement.MarginProperty, null);
  223.  
  224.             if (ms1.x == ms2.x) // горизонтальное перемещение
  225.             {
  226.                 if (ms1.y + 1 == ms2.y)
  227.                 {
  228.                     ms1.y++;
  229.                     ms2.y--;
  230.  
  231.                     ms1.el.Margin = new Thickness(0);
  232.                     ms2.el.Margin = new Thickness(0);
  233.  
  234.                     Grid.SetColumn(ms1.el, ms1.y);
  235.                     Grid.SetColumn(ms2.el, ms2.y);
  236.                 }
  237.                 else if (ms1.y - 1 == ms2.y)
  238.                 {
  239.                     ms1.y--;
  240.                     ms2.y++;
  241.  
  242.                     ms1.el.Margin = new Thickness(0);
  243.                     ms2.el.Margin = new Thickness(0);
  244.  
  245.                     Grid.SetColumn(ms1.el, ms1.y);
  246.                     Grid.SetColumn(ms2.el, ms2.y);
  247.                 }
  248.             }
  249.             if (ms1.y == ms2.y)  //вертикальное перемещение
  250.             {
  251.                 if (ms1.x + 1 == ms2.x)
  252.                 {
  253.                     ms1.x++;
  254.                     ms2.x--;
  255.  
  256.                     ms1.el.Margin = new Thickness(0);
  257.                     ms2.el.Margin = new Thickness(0);
  258.  
  259.                     Grid.SetRow(ms1.el, ms1.x);
  260.                     Grid.SetRow(ms2.el, ms2.x);
  261.                 }
  262.                 else if (ms1.x - 1 == ms2.x)
  263.                 {
  264.                     ms1.x--;
  265.                     ms2.x++;
  266.  
  267.                     ms1.el.Margin = new Thickness(0);
  268.                     ms2.el.Margin = new Thickness(0);
  269.  
  270.                     Grid.SetRow(ms1.el, ms1.x);
  271.                     Grid.SetRow(ms2.el, ms2.x);
  272.                 }
  273.             }
  274.            vf.Sovpadenie();
  275.         }
  276.  
  277.         public void Disapear(int i, int j)
  278.         {
  279.             _elW.From = _el.Width;
  280.             _elW.To = 0;
  281.             _elW.Duration = new Duration(TimeSpan.Parse("0:0:1"));
  282.  
  283.             _elH.From = _el.Height;
  284.             _elH.To = 0;
  285.             _elH.Duration = new Duration(TimeSpan.Parse("0:0:1"));
  286.  
  287.             _elH.Completed += _elH_Completed;
  288.  
  289.             gridArray[i, j].el.BeginAnimation(Ellipse.WidthProperty, _elW);
  290.             gridArray[i, j].el.BeginAnimation(Ellipse.HeightProperty, _elH);
  291.  
  292.         }
  293.  
  294.         void _elH_Completed(object sender, EventArgs e)
  295.         {
  296.             //int color = _rnd.Next(0, 6);
  297.  
  298.             //_fieldColors[i, j] = _cl[color];
  299.  
  300.             //col = _cl[color];
  301.  
  302.             //_el = new Ellipse { Width = 1, Height = 1, Fill = new SolidColorBrush(col) };
  303.  
  304.             //_elW.From = _el.Width;
  305.             //_elW.To = 35;
  306.             //_elW.Duration = new Duration(TimeSpan.Parse("0:0:0,4"));
  307.  
  308.             //_elH.From = _el.Height;
  309.             //_elH.To = 35;
  310.             //_elH.Duration = new Duration(TimeSpan.Parse("0:0:0,4"));
  311.  
  312.             //_el.BeginAnimation(FrameworkElement.WidthProperty, _elW);
  313.             //_el.BeginAnimation(FrameworkElement.HeightProperty, _elH);
  314.  
  315.             //gridArray[i, j] = new myShape(_el, col, i, j);
  316.         }
  317.  
  318.     }
  319. класс Verify
  320.  
  321. namespace Lines
  322. {
  323.      public class Verify :MainWindow
  324.      {
  325.          
  326.          private int _score;
  327.  
  328.          public void Sovpadenie()
  329.          {
  330.              for (int i = 0; i <= 7; i++)
  331.              {
  332.                  for (int j = 0; j <= 7; j++)
  333.                  {
  334.                      if ((ms.gridArray[i, j].ch.B == ms.gridArray[i, j + 1].ch.B) &&
  335.                          (ms.gridArray[i, j].ch.B == ms.gridArray[i, j + 2].ch.B))
  336.                      {
  337.                          ms.Disapear(i, j);
  338.            
  339.                          _score += 3;
  340.                          //ScoreLabel.Content = _score.ToString();
  341.            
  342.                          //Sovpadenie();
  343.                      }
  344.            
  345.                      if ((ms.gridArray[i, j].ch.B == ms.gridArray[i + 1, j].ch.B) &&
  346.                          (ms.gridArray[i, j].ch.B == ms.gridArray[i + 2, j].ch.B))
  347.                      {
  348.                          
  349.                          
  350.                          _score += 3;
  351.                          //ScoreLabel.Content = _score.ToString();
  352.            
  353.                          //Sovpadenie();
  354.                      }
  355.                  }
  356.              }
  357.              for (int i = 8; i < 9; i++)
  358.              {
  359.                  for (int j = 0; j < 7; j++)
  360.                  {
  361.                      if ((ms.gridArray[i, j].ch.B == ms.gridArray[i, j + 1].ch.B) &&
  362.                          (ms.gridArray[i, j].ch.B == ms.gridArray[i, j + 2].ch.B))
  363.                      {
  364.            
  365.                          _score += 3;
  366.                          //ScoreLabel.Content = _score.ToString();
  367.            
  368.                          //Sovpadenie();
  369.                      }
  370.                  }
  371.              }
  372.            
  373.              for (int j = 8; j < 9; j++)
  374.              {
  375.                  for (int i = 0; i < 7; i++)
  376.                  {
  377.                      if ((ms.gridArray[i, j].ch.B == ms.gridArray[i + 1, j].ch.B) &&
  378.                          (ms.gridArray[i, j].ch.B == ms.gridArray[i + 2, j].ch.B))
  379.                      {
  380.            
  381.                          _score += 3;
  382.                          //ScoreLabel.Content = _score.ToString();
  383.            
  384.                          //Sovpadenie();
  385.                      }
  386.                  }
  387.              }
  388.          }
  389.      }

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


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

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

7   голосов , оценка 4 из 5

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

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

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