Лабораторная работа 3

This commit is contained in:
Efi 2023-12-07 16:08:00 +04:00
parent 59883cb493
commit 04b4e75288
9 changed files with 528 additions and 7 deletions

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using AirBomber.Entities; using AirBomber.Entities;
using AirBomber.MovementStrategy;
namespace AirBomber.DrawingObjects namespace AirBomber.DrawingObjects
{ {
@ -21,6 +22,7 @@ namespace AirBomber.DrawingObjects
public int GetPosY => _startPosY; public int GetPosY => _startPosY;
public int GetWidth => _WarAirplaneWidth; public int GetWidth => _WarAirplaneWidth;
public int GetHeight => _WarAirplaneHeight; public int GetHeight => _WarAirplaneHeight;
public IMoveableObject GetMoveableObject => new DrawingObjectWarAirplane(this);
public DrawingWarAirplane(int speed, double weight, Color bodyColor, public DrawingWarAirplane(int speed, double weight, Color bodyColor,
int width, int height) int width, int height)

View File

@ -37,6 +37,7 @@
buttonCreateAirBomber = new Button(); buttonCreateAirBomber = new Button();
buttonStep = new Button(); buttonStep = new Button();
comboBoxStrategy = new ComboBox(); comboBoxStrategy = new ComboBox();
ButtonSelectWarAirplane = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -138,11 +139,22 @@
comboBoxStrategy.Size = new Size(156, 23); comboBoxStrategy.Size = new Size(156, 23);
comboBoxStrategy.TabIndex = 8; comboBoxStrategy.TabIndex = 8;
// //
// ButtonSelectWarAirplane
//
ButtonSelectWarAirplane.Location = new Point(379, 394);
ButtonSelectWarAirplane.Name = "ButtonSelectWarAirplane";
ButtonSelectWarAirplane.Size = new Size(156, 41);
ButtonSelectWarAirplane.TabIndex = 9;
ButtonSelectWarAirplane.Text = "Выбрать самолёт";
ButtonSelectWarAirplane.UseVisualStyleBackColor = true;
ButtonSelectWarAirplane.Click += ButtonSelectWarAirplane_Click;
//
// FormAirBomber // FormAirBomber
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461); ClientSize = new Size(884, 461);
Controls.Add(ButtonSelectWarAirplane);
Controls.Add(comboBoxStrategy); Controls.Add(comboBoxStrategy);
Controls.Add(buttonStep); Controls.Add(buttonStep);
Controls.Add(buttonCreateAirBomber); Controls.Add(buttonCreateAirBomber);
@ -171,5 +183,6 @@
private Button buttonCreateAirBomber; private Button buttonCreateAirBomber;
private Button buttonStep; private Button buttonStep;
private ComboBox comboBoxStrategy; private ComboBox comboBoxStrategy;
private Button ButtonSelectWarAirplane;
} }
} }

View File

@ -1,5 +1,7 @@
using AirBomber.DrawingObjects; using AirBomber.DrawingObjects;
using AirBomber.MovementStrategy; using AirBomber.MovementStrategy;
using System.Drawing;
using System;
using System.Windows.Forms; using System.Windows.Forms;
namespace AirBomber namespace AirBomber
@ -8,9 +10,12 @@ namespace AirBomber
{ {
private DrawingWarAirplane? _drawingWarAirplane; private DrawingWarAirplane? _drawingWarAirplane;
private AbstractStrategy? _abstractStrategy; private AbstractStrategy? _abstractStrategy;
public DrawingWarAirplane? SelectedWarAirplane { get; private set; }
public FormAirBomber() public FormAirBomber()
{ {
InitializeComponent(); InitializeComponent();
_abstractStrategy = null;
SelectedWarAirplane = null;
} }
private void Draw() private void Draw()
@ -28,17 +33,35 @@ namespace AirBomber
private void ButtonCreateAirBomber_Click(object sender, EventArgs e) private void ButtonCreateAirBomber_Click(object sender, EventArgs e)
{ {
Random rnd = new(); Random rnd = new();
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
Color additionalColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
if (dialog.ShowDialog() == DialogResult.OK)
{
additionalColor = dialog.Color;
}
_drawingWarAirplane = new DrawingAirBomber(rnd.Next(100, 300), rnd.Next(1000, 2000), _drawingWarAirplane = new DrawingAirBomber(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)), Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)), color, additionalColor, Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
_drawingWarAirplane.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); _drawingWarAirplane.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
Draw(); Draw();
} }
private void ButtonCreateWarAirplane_Click(object sender, EventArgs e) private void ButtonCreateWarAirplane_Click(object sender, EventArgs e)
{ {
Random rnd = new(); Random rnd = new();
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
_drawingWarAirplane = new DrawingWarAirplane(rnd.Next(100, 300), rnd.Next(1000, 2000), _drawingWarAirplane = new DrawingWarAirplane(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); color, pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
_drawingWarAirplane.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); _drawingWarAirplane.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
Draw(); Draw();
} }
@ -88,15 +111,14 @@ namespace AirBomber
{ {
return; return;
} }
_abstractStrategy.SetData(new _abstractStrategy.SetData(_drawingWarAirplane.GetMoveableObject, pictureBoxAirBomber.Width,
DrawingObjectWarAirplane(_drawingWarAirplane), pictureBoxAirBomber.Width,
pictureBoxAirBomber.Height); pictureBoxAirBomber.Height);
comboBoxStrategy.Enabled = false;
} }
if (_abstractStrategy == null) if (_abstractStrategy == null)
{ {
return; return;
} }
comboBoxStrategy.Enabled = false;
_abstractStrategy.MakeStep(); _abstractStrategy.MakeStep();
Draw(); Draw();
if (_abstractStrategy.GetStatus() == Status.Finish) if (_abstractStrategy.GetStatus() == Status.Finish)
@ -105,6 +127,11 @@ namespace AirBomber
_abstractStrategy = null; _abstractStrategy = null;
} }
} }
private void ButtonSelectWarAirplane_Click(object sender, EventArgs e)
{
SelectedWarAirplane = _drawingWarAirplane;
DialogResult = DialogResult.OK;
}
} }
} }

View File

@ -0,0 +1,135 @@
namespace AirBomber
{
partial class FormWarAirplaneCollection
{
/// <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()
{
panel1 = new Panel();
Tools = new Label();
buttonRefreshCollection = new Button();
buttonRemove = new Button();
buttonAdd = new Button();
maskedTextBoxNumber = new MaskedTextBox();
pictureBoxCollection = new PictureBox();
panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
// panel1
//
panel1.Controls.Add(Tools);
panel1.Controls.Add(buttonRefreshCollection);
panel1.Controls.Add(buttonRemove);
panel1.Controls.Add(buttonAdd);
panel1.Controls.Add(maskedTextBoxNumber);
panel1.Dock = DockStyle.Right;
panel1.Location = new Point(617, 0);
panel1.Name = "panel1";
panel1.Size = new Size(183, 450);
panel1.TabIndex = 0;
//
// Tools
//
Tools.AutoSize = true;
Tools.Location = new Point(53, 9);
Tools.Name = "Tools";
Tools.Size = new Size(83, 15);
Tools.TabIndex = 3;
Tools.Text = "Инструменты";
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(6, 179);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(174, 43);
buttonRefreshCollection.TabIndex = 2;
buttonRefreshCollection.Text = "Обновить коллекцию";
buttonRefreshCollection.UseVisualStyleBackColor = true;
buttonRefreshCollection.Click += ButtonRefreshCollection_Click;
//
// buttonRemove
//
buttonRemove.Location = new Point(6, 120);
buttonRemove.Name = "buttonRemove";
buttonRemove.Size = new Size(174, 31);
buttonRemove.TabIndex = 2;
buttonRemove.Text = "Удалить военный самолёт";
buttonRemove.UseVisualStyleBackColor = true;
buttonRemove.Click += ButtonRemoveWarAirplane_Click;
//
// buttonAdd
//
buttonAdd.Location = new Point(6, 39);
buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(174, 31);
buttonAdd.TabIndex = 2;
buttonAdd.Text = "Добавить военный самолёт";
buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += ButtonAddWarAirplane_Click;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(6, 91);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(174, 23);
maskedTextBoxNumber.TabIndex = 0;
//
// pictureBoxCollection
//
pictureBoxCollection.Dock = DockStyle.Left;
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(617, 450);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// FormWarAirplaneCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(pictureBoxCollection);
Controls.Add(panel1);
Name = "FormWarAirplaneCollection";
Text = "Набор военных самолётов";
panel1.ResumeLayout(false);
panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
#endregion
private Panel panel1;
private PictureBox pictureBoxCollection;
private Label Tools;
private Button buttonRefreshCollection;
private Button buttonRemove;
private Button buttonAdd;
private MaskedTextBox maskedTextBoxNumber;
}
}

View File

@ -0,0 +1,64 @@
using AirBomber.Generics;
using AirBomber.DrawingObjects;
using AirBomber.MovementStrategy;
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;
namespace AirBomber
{
public partial class FormWarAirplaneCollection : Form
{
private readonly WarAirplaneGenericCollection<DrawingWarAirplane, DrawingObjectWarAirplane> _warAirplane;
public FormWarAirplaneCollection()
{
InitializeComponent();
_warAirplane = new WarAirplaneGenericCollection<DrawingWarAirplane, DrawingObjectWarAirplane>(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
private void ButtonAddWarAirplane_Click(object sender, EventArgs e)
{
FormAirBomber form = new();
if(form.ShowDialog() == DialogResult.OK)
{
if (_warAirplane + form.SelectedWarAirplane != null)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _warAirplane.ShowWarAirplane();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void ButtonRemoveWarAirplane_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_warAirplane - pos != null)
{
MessageBox.Show("Объект удалён");
pictureBoxCollection.Image = _warAirplane.ShowWarAirplane();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _warAirplane.ShowWarAirplane();
}
}
}

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

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

View File

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber.Generics
{
internal class SetGeneric<T>
where T : class
{
private readonly T?[] _places;
public int Count => _places.Length;
public int startPointer = 0;
public SetGeneric(int count)
{
_places = new T?[count];
}
public int Insert(T WarAirplane)
{
if (_places[Count - 1] != null)
return -1;
return Insert(WarAirplane, 0);
}
public int Insert(T WarAirplane, int position)
{
if (!(position >= 0 && position < Count))
return -1;
if (_places[position] != null)
{
int indexEnd = position + 1;
while (_places[indexEnd] != null)
{
indexEnd++;
}
for (int i = indexEnd + 1; i > position; i--)
{
_places[i] = _places[i - 1];
}
}
_places[position] = WarAirplane;
return position;
}
public bool Remove(int position)
{
if (position < Count && position >= 0)
{
_places[position] = null;
return true;
}
return false;
}
public T? Get(int position)
{
if (position < Count && position >= 0)
{
return _places[position];
}
return null;
}
}
}

View File

@ -0,0 +1,94 @@
using AirBomber.MovementStrategy;
using AirBomber.DrawingObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace AirBomber.Generics
{
internal class WarAirplaneGenericCollection<T, U>
where T : DrawingWarAirplane
where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 110;
private readonly int _placeSizeHeight = 110;
private readonly SetGeneric<T> _collection;
public WarAirplaneGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(WarAirplaneGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return -1;
}
return collect._collection.Insert(obj);
}
public static bool operator -(WarAirplaneGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
{
collect._collection.Remove(pos);
}
return true;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
public Bitmap ShowWarAirplane()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
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 < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight +
1; ++j)
{//линия рамзетки места
g.DrawLine(pen, i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i *
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
private void DrawObjects(Graphics g)
{
DrawingWarAirplane warAirplane;
for (int i = 0; i < _collection.Count; i++)
{
warAirplane = _collection.Get(i);
if (warAirplane != null)
{
int numPlacesInRow = _pictureWidth / _placeSizeWidth;
warAirplane.SetPosition((numPlacesInRow - 1 - (i % numPlacesInRow)) * _placeSizeWidth,
i / numPlacesInRow * _placeSizeHeight);
warAirplane.DrawTransport(g);
}
}
}
}
}