Как связать между собой 3 класса - C#
Формулировка задачи:
Подскажите пожалуйста, как можно связать между собой 3 класса.
Решение задачи: «Как связать между собой 3 класса»
textual
Листинг программы
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Shapes;
namespace Lines
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public Verify vf;
private Point _pt;
public myShape ms = new myShape(vf);
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (Field.Children.Count == 101)
{
Field.Children.RemoveRange(1, 101);
ms.gridArray = new myShape[10,10];
}
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
{
ms.Start(i,j);
Grid.SetRow(ms._el, i);
Grid.SetColumn(ms._el, j);
Field.Children.Add(ms._el);
ms._el.MouseLeftButtonDown += el_MouseLeftButtonDown;
ms._el.MouseLeftButtonUp += el_MouseLeftButtonUp;
}
}
public void el_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ms._el = sender as Ellipse;
_pt = e.GetPosition(this);
var i = (int)(_pt.Y / 40);
var j = (int)((_pt.X - 129) / 40);
ms.ms1 = new myShape(ms._el, ms.col, i, j);
}
public void el_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
ms._el = sender as Ellipse;
_pt = e.GetPosition(this);
var i = (int)(_pt.Y / 40);
var j = (int)((_pt.X - 129) / 40);
ms.ms2 = new myShape(ms._el, ms.col, i, j);
ms.Animation(ms.ms1, ms.ms2);
}
}
}
класс myShape
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Lines
{
public class myShape : Verify
{
public Ellipse el;
public int x;
public int y;
public Color ch;
public myShape(Ellipse El, Color Ch, int X, int Y)
{
ch = Ch;
el = El;
x = X;
y = Y;
}
public myShape(Verify Vf)
{
vf = Vf;
}
public myShape ms1, ms2;
public Verify vf;
readonly Color[] _cl = { Colors.Black, Colors.Chartreuse, Colors.Blue, Colors.Crimson, Colors.Orchid, Colors.Yellow };
readonly Color[,] _fieldColors = new Color[10, 10];
public Ellipse _el;
public Color col;
readonly Random _rnd = new Random();
readonly DoubleAnimation _elW = new DoubleAnimation();
readonly DoubleAnimation _elH = new DoubleAnimation();
public myShape[,] gridArray = new myShape[10, 10];
public List<myShape> list = new List<myShape>();
public void Start(int i, int j)
{
int color = _rnd.Next(0, 6);
_fieldColors[i, j] = _cl[color];
col = _cl[color];
_el = new Ellipse { Width = 1, Height = 1, Fill = new SolidColorBrush(col) };
_elW.From = _el.Width;
_elW.To = 35;
_elW.Duration = new Duration(TimeSpan.Parse("0:0:0,4"));
_elH.From = _el.Height;
_elH.To = 35;
_elH.Duration = new Duration(TimeSpan.Parse("0:0:0,4"));
_el.BeginAnimation(FrameworkElement.WidthProperty, _elW);
_el.BeginAnimation(FrameworkElement.HeightProperty, _elH);
gridArray[i, j] = new myShape(_el, col, i, j);
}
public void Animation(myShape ms1, myShape ms2)
{
var ta = new ThicknessAnimation();
var ta1 = new ThicknessAnimation();
if (ms1.x == ms2.x) //горизонтальоне перемещение
{
if (ms1.y + 1 == ms2.y)
{
ta.From = ms1.el.Margin;
ta.To = new Thickness(ms1.el.Width + 5, 0, -ms1.el.Width - 5, 0);
ta1.From = ms2.el.Margin;
ta1.To = new Thickness(-ms2.el.Width - 5, 0, ms2.el.Width + 5, 0);
ta1.Completed += ta_Completed;
ta.Duration = new Duration(TimeSpan.Parse("0:0:2"));
ta1.Duration = new Duration(TimeSpan.Parse("0:0:2"));
ms1.el.BeginAnimation(FrameworkElement.MarginProperty, ta);
ms2.el.BeginAnimation(FrameworkElement.MarginProperty, ta1);
}
if (ms1.y - 1 == ms2.y)
{
ta.From = ms2.el.Margin;
ta.To = new Thickness(ms2.el.Width + 5, 0, -ms2.el.Width - 5, 0);
ta1.From = ms1.el.Margin;
ta1.To = new Thickness(-ms1.el.Width - 5, 0, ms1.el.Width + 5, 0);
ta.Duration = new Duration(TimeSpan.Parse("0:0:2"));
ta1.Duration = new Duration(TimeSpan.Parse("0:0:2"));
ta1.Completed += ta_Completed;
ms1.el.BeginAnimation(FrameworkElement.MarginProperty, ta1);
ms2.el.BeginAnimation(FrameworkElement.MarginProperty, ta);
}
}
if (ms1.y == ms2.y)//вертикальное перемещение
{
if (ms1.x + 1 == ms2.x)
{
ta.From = ms1.el.Margin;
ta.To = new Thickness(0, ms1.el.Height + 5, 0, -ms1.el.Height - 5);
ta1.From = ms2.el.Margin;
ta1.To = new Thickness(0, -ms1.el.Height - 5, 0, ms1.el.Height + 5);
ta.Duration = new Duration(TimeSpan.Parse("0:0:2"));
ta1.Duration = new Duration(TimeSpan.Parse("0:0:2"));
ta.Completed += ta_Completed;
ms1.el.BeginAnimation(FrameworkElement.MarginProperty, ta);
ms2.el.BeginAnimation(FrameworkElement.MarginProperty, ta1);
}
if (ms1.x - 1 == ms2.x)
{
ta.From = ms2.el.Margin;
ta.To = new Thickness(0, ms2.el.Height + 5, 0, -ms2.el.Height - 5);
ta1.From = ms1.el.Margin;
ta1.To = new Thickness(0, -ms1.el.Height - 5, 0, ms1.el.Height + 5);
ta.Completed += ta_Completed;
ta.Duration = new Duration(TimeSpan.Parse("0:0:2"));
ta1.Duration = new Duration(TimeSpan.Parse("0:0:2"));
ms1.el.BeginAnimation(FrameworkElement.MarginProperty, ta1);
ms2.el.BeginAnimation(FrameworkElement.MarginProperty, ta);
}
}
}
void ta_Completed(object sender, EventArgs e)
{
ms1.el.BeginAnimation(FrameworkElement.MarginProperty, null);
ms2.el.BeginAnimation(FrameworkElement.MarginProperty, null);
if (ms1.x == ms2.x) // горизонтальное перемещение
{
if (ms1.y + 1 == ms2.y)
{
ms1.y++;
ms2.y--;
ms1.el.Margin = new Thickness(0);
ms2.el.Margin = new Thickness(0);
Grid.SetColumn(ms1.el, ms1.y);
Grid.SetColumn(ms2.el, ms2.y);
}
else if (ms1.y - 1 == ms2.y)
{
ms1.y--;
ms2.y++;
ms1.el.Margin = new Thickness(0);
ms2.el.Margin = new Thickness(0);
Grid.SetColumn(ms1.el, ms1.y);
Grid.SetColumn(ms2.el, ms2.y);
}
}
if (ms1.y == ms2.y) //вертикальное перемещение
{
if (ms1.x + 1 == ms2.x)
{
ms1.x++;
ms2.x--;
ms1.el.Margin = new Thickness(0);
ms2.el.Margin = new Thickness(0);
Grid.SetRow(ms1.el, ms1.x);
Grid.SetRow(ms2.el, ms2.x);
}
else if (ms1.x - 1 == ms2.x)
{
ms1.x--;
ms2.x++;
ms1.el.Margin = new Thickness(0);
ms2.el.Margin = new Thickness(0);
Grid.SetRow(ms1.el, ms1.x);
Grid.SetRow(ms2.el, ms2.x);
}
}
vf.Sovpadenie();
}
public void Disapear(int i, int j)
{
_elW.From = _el.Width;
_elW.To = 0;
_elW.Duration = new Duration(TimeSpan.Parse("0:0:1"));
_elH.From = _el.Height;
_elH.To = 0;
_elH.Duration = new Duration(TimeSpan.Parse("0:0:1"));
_elH.Completed += _elH_Completed;
gridArray[i, j].el.BeginAnimation(Ellipse.WidthProperty, _elW);
gridArray[i, j].el.BeginAnimation(Ellipse.HeightProperty, _elH);
}
void _elH_Completed(object sender, EventArgs e)
{
//int color = _rnd.Next(0, 6);
//_fieldColors[i, j] = _cl[color];
//col = _cl[color];
//_el = new Ellipse { Width = 1, Height = 1, Fill = new SolidColorBrush(col) };
//_elW.From = _el.Width;
//_elW.To = 35;
//_elW.Duration = new Duration(TimeSpan.Parse("0:0:0,4"));
//_elH.From = _el.Height;
//_elH.To = 35;
//_elH.Duration = new Duration(TimeSpan.Parse("0:0:0,4"));
//_el.BeginAnimation(FrameworkElement.WidthProperty, _elW);
//_el.BeginAnimation(FrameworkElement.HeightProperty, _elH);
//gridArray[i, j] = new myShape(_el, col, i, j);
}
}
класс Verify
namespace Lines
{
public class Verify :MainWindow
{
private int _score;
public void Sovpadenie()
{
for (int i = 0; i <= 7; i++)
{
for (int j = 0; j <= 7; j++)
{
if ((ms.gridArray[i, j].ch.B == ms.gridArray[i, j + 1].ch.B) &&
(ms.gridArray[i, j].ch.B == ms.gridArray[i, j + 2].ch.B))
{
ms.Disapear(i, j);
_score += 3;
//ScoreLabel.Content = _score.ToString();
//Sovpadenie();
}
if ((ms.gridArray[i, j].ch.B == ms.gridArray[i + 1, j].ch.B) &&
(ms.gridArray[i, j].ch.B == ms.gridArray[i + 2, j].ch.B))
{
_score += 3;
//ScoreLabel.Content = _score.ToString();
//Sovpadenie();
}
}
}
for (int i = 8; i < 9; i++)
{
for (int j = 0; j < 7; j++)
{
if ((ms.gridArray[i, j].ch.B == ms.gridArray[i, j + 1].ch.B) &&
(ms.gridArray[i, j].ch.B == ms.gridArray[i, j + 2].ch.B))
{
_score += 3;
//ScoreLabel.Content = _score.ToString();
//Sovpadenie();
}
}
}
for (int j = 8; j < 9; j++)
{
for (int i = 0; i < 7; i++)
{
if ((ms.gridArray[i, j].ch.B == ms.gridArray[i + 1, j].ch.B) &&
(ms.gridArray[i, j].ch.B == ms.gridArray[i + 2, j].ch.B))
{
_score += 3;
//ScoreLabel.Content = _score.ToString();
//Sovpadenie();
}
}
}
}
}