лаба 3

This commit is contained in:
DavidMakarov 2023-10-20 21:35:03 +04:00
parent 18d5f5f466
commit 06b1fe82e7
10 changed files with 487 additions and 63 deletions

View File

@ -9,9 +9,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Remove="AirplanesGenericCollection.cs" />
<Compile Remove="Class1.cs" />
<Compile Remove="SetGeneric.cs" />
</ItemGroup>
</Project>

View File

@ -7,47 +7,86 @@ using System.Threading.Tasks;
using AirplaneWithRadar.PaintObjects;
using AirplaneWithRadar.MovementStrategy;
namespace AirplaneWithRadar.Generics;
internal class AirplanesGenericCollection<T,U>
where T : PaintAirplane
where U : IMoveableObject
namespace AirplaneWithRadar.Generics
{
private readonly int pictWidth;
private readonly int pictHeight;
private readonly int placeSizeWidth = 210;
private readonly int placeSizeHeight = 90;
private readonly SetGeneric<T> collection;
public AirplanesGenericCollection(int picWidth, int picHeight)
internal class AirplanesGenericCollection<T, U>
where T : PaintAirplane
where U : IMoveableObject
{
int width = picWidth / placeSizeWidth;
int height = picHeight / placeSizeHeight;
pictWidth = picWidth;
pictHeight = picHeight;
collection = new SetGeneric<T>(width * height);
private readonly int pictWidth;
private readonly int pictHeight;
private readonly int placeSizeWidth = 210;
private readonly int placeSizeHeight = 90;
private readonly SetGeneric<T> collection;
}
public static bool operator +(AirplanesGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
public AirplanesGenericCollection(int picWidth, int picHeight)
{
return false;
int width = picWidth / placeSizeWidth;
int height = picHeight / placeSizeHeight;
pictWidth = picWidth;
pictHeight = picHeight;
collection = new SetGeneric<T>(width * height);
}
return collect?.collection.Insert(obj) ?? false;
}
public static T? operator -(AirplanesGenericCollection<T, U> collect, int
pos)
{
T? obj = collect.collection.Get(pos);
if (obj != null)
public static int? operator +(AirplanesGenericCollection<T, U> collect, T? obj)
{
collect.collection.Remove(pos);
if (obj == null)
{
return -1;
}
return collect?.collection.Insert(obj);
}
public static bool operator -(AirplanesGenericCollection<T, U> collect, int pos)
{
T? obj = collect.collection.Get(pos);
if (obj == null)
{
return false;
}
return collect.collection.Remove(pos);
}
public U? GetU(int pos)
{
return (U?)collection.Get(pos)?.GetMoveableObject;
}
public Bitmap ShowAirplanes()
{
Bitmap bmp = new(pictWidth, pictHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < pictWidth / placeSizeWidth; i++)
{
for (int j = 0; j < pictHeight / placeSizeHeight + 1; ++j)
{
g.DrawLine(pen, i * placeSizeWidth, j * placeSizeHeight, i * placeSizeWidth + placeSizeWidth / 2, j * placeSizeHeight);
}
g.DrawLine(pen, i * placeSizeWidth, 0, i * placeSizeWidth, pictHeight / placeSizeHeight * placeSizeHeight);
}
}
private void DrawObjects(Graphics g)
{
int width = pictWidth / placeSizeWidth;
int height = pictHeight / placeSizeHeight;
for (int i = 0; i < collection.Count; i++)
{
PaintAirplane? airplane = collection.Get(i);
if (airplane != null)
{
airplane.SetPosition(placeSizeWidth - (i % 2 * placeSizeWidth), i / 2 * placeSizeHeight);
airplane.DrawTransport(g);
}
}
}
return obj;
}
}

View File

@ -0,0 +1,123 @@
namespace AirplaneWithRadar
{
partial class FormAirplaneCollection
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
groupBox1 = new GroupBox();
maskedTextBoxNumber = new TextBox();
ButtonRefreshCollection = new Button();
ButtonDeleteAirplane = new Button();
ButtonAddAirplane = new Button();
pictureBoxCollection = new PictureBox();
groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
// groupBox1
//
groupBox1.Controls.Add(maskedTextBoxNumber);
groupBox1.Controls.Add(ButtonRefreshCollection);
groupBox1.Controls.Add(ButtonDeleteAirplane);
groupBox1.Controls.Add(ButtonAddAirplane);
groupBox1.Location = new Point(574, 3);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(223, 440);
groupBox1.TabIndex = 0;
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(39, 148);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(161, 23);
maskedTextBoxNumber.TabIndex = 3;
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(20, 229);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(194, 46);
ButtonRefreshCollection.TabIndex = 2;
ButtonRefreshCollection.Text = "Обновить коллекцию";
ButtonRefreshCollection.UseVisualStyleBackColor = true;
ButtonRefreshCollection.Click += ButtonRefreshCollection_Click;
//
// ButtonDeleteAirplane
//
ButtonDeleteAirplane.Location = new Point(20, 177);
ButtonDeleteAirplane.Name = "ButtonDeleteAirplane";
ButtonDeleteAirplane.Size = new Size(194, 46);
ButtonDeleteAirplane.TabIndex = 1;
ButtonDeleteAirplane.Text = "Удалить самолёт";
ButtonDeleteAirplane.UseVisualStyleBackColor = true;
ButtonDeleteAirplane.Click += ButtonDeleteAirplane_Click;
//
// ButtonAddAirplane
//
ButtonAddAirplane.Location = new Point(20, 22);
ButtonAddAirplane.Name = "ButtonAddAirplane";
ButtonAddAirplane.Size = new Size(194, 46);
ButtonAddAirplane.TabIndex = 0;
ButtonAddAirplane.Text = "Добавить самолёт";
ButtonAddAirplane.UseVisualStyleBackColor = true;
ButtonAddAirplane.Click += ButtonAddAirplane_Click;
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(4, 3);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(567, 440);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// FormAirplaneCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(pictureBoxCollection);
Controls.Add(groupBox1);
Name = "FormAirplaneCollection";
Text = "FormAirplaneCollection";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
#endregion
private GroupBox groupBox1;
private Button ButtonRefreshCollection;
private Button ButtonDeleteAirplane;
private Button ButtonAddAirplane;
private PictureBox pictureBoxCollection;
private TextBox maskedTextBoxNumber;
}
}

View File

@ -0,0 +1,69 @@
using AirplaneWithRadar.Generics;
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;
using AirplaneWithRadar.PaintObjects;
using AirplaneWithRadar.MovementStrategy;
namespace AirplaneWithRadar
{
public partial class FormAirplaneCollection : Form
{
private readonly AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane> airplanes;
public FormAirplaneCollection()
{
InitializeComponent();
airplanes = new AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
private void ButtonAddAirplane_Click(object sender, EventArgs e)
{
PlaneVisual form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (airplanes + form.SelectedAirplane != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = airplanes.ShowAirplanes();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void ButtonDeleteAirplane_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (airplanes - pos != false)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = airplanes.ShowAirplanes();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = airplanes.ShowAirplanes();
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing"">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirplaneWithRadar.Entities;
using AirplaneWithRadar.MovementStrategy;
namespace AirplaneWithRadar.PaintObjects
{
@ -23,6 +24,8 @@ namespace AirplaneWithRadar.PaintObjects
public int GetWidth => planeWidth;
public int GetHeight => planeHeight;
public IMoveableObject GetMoveableObject => new PaintObjectAirplane(this);
public bool CanMove(Movement dir)
{
if (AirplaneEntity == null)

View File

@ -37,6 +37,7 @@
StrategyComboBox = new ComboBox();
StepButton = new Button();
CreateAirplaneButton = new Button();
buttonSelectAirplane = new Button();
((System.ComponentModel.ISupportInitialize)PlanesPictureBox).BeginInit();
SuspendLayout();
//
@ -136,11 +137,22 @@
CreateAirplaneButton.UseVisualStyleBackColor = true;
CreateAirplaneButton.Click += CreateAirplaneButton_Click;
//
// buttonSelectAirplane
//
buttonSelectAirplane.Location = new Point(282, 391);
buttonSelectAirplane.Name = "buttonSelectAirplane";
buttonSelectAirplane.Size = new Size(120, 40);
buttonSelectAirplane.TabIndex = 9;
buttonSelectAirplane.Text = "Выбрать самолёт";
buttonSelectAirplane.UseVisualStyleBackColor = true;
buttonSelectAirplane.Click += buttonSelectAirplane_Click;
//
// PlaneVisual
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(buttonSelectAirplane);
Controls.Add(CreateAirplaneButton);
Controls.Add(StepButton);
Controls.Add(StrategyComboBox);
@ -167,5 +179,6 @@
private ComboBox StrategyComboBox;
private Button StepButton;
private Button CreateAirplaneButton;
private Button buttonSelectAirplane;
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Drawing;
using AirplaneWithRadar.MovementStrategy;
using AirplaneWithRadar.PaintObjects;
@ -8,7 +9,7 @@ namespace AirplaneWithRadar
{
private PaintAirplane? PaintPlanes;
private AbstractStrategy? abstractStrategy;
public PaintAirplane? SelectedAirplane { get; private set; }
private void Draw()
{
if (PaintPlanes == null)
@ -23,7 +24,8 @@ namespace AirplaneWithRadar
public PlaneVisual()
{
InitializeComponent();
abstractStrategy = null;
SelectedAirplane = null;
}
private void buttonMove_Click(object sender, EventArgs e)
@ -54,8 +56,22 @@ namespace AirplaneWithRadar
{
Random random = new Random();
PaintPlanes = new PaintAirplaneWithRadar(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), true,
Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
dopColor = dialog.Color;
}
PaintPlanes = new PaintAirplaneWithRadar(random.Next(100, 300), random.Next(1000, 3000), color, dopColor, true,
Convert.ToBoolean(random.Next(0, 2)), PlanesPictureBox.Width, PlanesPictureBox.Height);
PaintPlanes.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
@ -64,7 +80,14 @@ namespace AirplaneWithRadar
private void CreateAirplaneButton_Click(object sender, EventArgs e)
{
Random random = new Random();
PaintPlanes = new PaintAirplane(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
PaintPlanes = new PaintAirplane(random.Next(100, 300), random.Next(1000, 3000), color,
PlanesPictureBox.Width, PlanesPictureBox.Height);
PaintPlanes.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
@ -103,5 +126,12 @@ namespace AirplaneWithRadar
abstractStrategy = null;
}
}
private void buttonSelectAirplane_Click(object sender, EventArgs e)
{
SelectedAirplane = PaintPlanes;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -11,7 +11,7 @@ namespace AirplaneWithRadar
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new PlaneVisual());
Application.Run(new FormAirplaneCollection());
}
}
}

View File

@ -10,44 +10,73 @@ internal class SetGeneric<T>
where T : class
{
private readonly T?[] places;
public int Count => places.Length;
public int Count => places.Length;
public SetGeneric(int count)
{
places = new T?[count];
}
public bool Insert(T airplane)
public int Insert(T airplane)
{
if (places[0] == null)
int emptyPosition = -1;
for (int i = 0; i < Count; i++)
{
places[0] = airplane;
}
else
{
for (int i = Count - 1; i < 0; i--)
if (places[i] == null)
{
places[i] = places[i - 1];
emptyPosition = i;
break;
}
places[0] = airplane;
}
return true;
if (emptyPosition < 0)
{
return -1;
}
for (int i = emptyPosition; i > 0; i--)
{
places[i] = places[i - 1];
}
places[0] = airplane;
return 0;
}
public bool Insert(T airplane, int position)
public int Insert(T airplane, int position)
{
if (position > Count || position < 0)
{
return false;
return -1;
}
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции
if (places[position] == null)
{
places[position] = airplane;
return position;
}
int emptyPosition = -1;
for (int i = position + 1; i < Count; i++)
{
if (places[i]==null)
{
emptyPosition = i;
break;
}
}
if (emptyPosition < 0)
{
return -1;
}
for (int i = emptyPosition; i > position; i--)
{
places[i] = places[i - 1];
}
places[position] = airplane;
return true;
return position;
}
public bool Remove(int position)
{
if (position > Count || position < 0)
if ((position >= Count && position < 0) || places[position] == null)
{
return false;
}
@ -56,7 +85,7 @@ internal class SetGeneric<T>
}
public T? Get(int position)
{
if (position > Count || position < 0)
if (position >= Count && position < 0)
{
return null;
}