PIbd-21 Belianin N.N. LabWork03 #3

Closed
Belnik wants to merge 4 commits from LabWork03 into LabWork02
15 changed files with 597 additions and 30 deletions

View File

@ -1,9 +1,9 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33801.468
VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tank", "Tank\Tank.csproj", "{19BA0FA1-372B-490A-AA7F-757B0B8F322B}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tank", "Tank\Tank.csproj", "{5C602C58-ECEC-4FAB-BA10-FB671582D227}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -11,15 +11,15 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Release|Any CPU.Build.0 = Release|Any CPU
{5C602C58-ECEC-4FAB-BA10-FB671582D227}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C602C58-ECEC-4FAB-BA10-FB671582D227}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C602C58-ECEC-4FAB-BA10-FB671582D227}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C602C58-ECEC-4FAB-BA10-FB671582D227}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5332F2FC-D1C2-4B42-B32E-53ECC094BBB7}
SolutionGuid = {73BF3262-E499-401F-BDBD-BD2BC7D3C324}
EndGlobalSection
EndGlobal

View File

@ -5,12 +5,13 @@ using System.Security.Cryptography.Pkcs;
using System.Text;
using System.Threading.Tasks;
using Tank.Entities;
using Tank.MovementStrategy;
namespace Tank
{
public class DrawArmoVehicle
{
public EntityBase? Tank { get; protected set; }
public EntityArmoVehicle? Tank { get; protected set; }
protected int _pictureWidth;
protected int _pictureHeight;
protected int _startPosX;
@ -29,7 +30,7 @@ namespace Tank
_pictureWidth = width;
if (_pictureHeight < _Height || _pictureWidth < _Width)
return;
Tank = new EntityBase(speed, weight, bodyColor);
Tank = new EntityArmoVehicle(speed, weight, bodyColor);
}
protected DrawArmoVehicle(int speed, double weight, Color bodyColor, int width, int height, int tankWidth, int tankHeight)
@ -40,7 +41,7 @@ namespace Tank
_Width = tankWidth;
if (_pictureHeight < _Height || _pictureWidth < _Width)
return;
Tank = new EntityBase(speed, weight, bodyColor);
Tank = new EntityArmoVehicle(speed, weight, bodyColor);
}
public bool CanMove(Direction direction)
@ -124,5 +125,7 @@ namespace Tank
g.FillEllipse(ColorBlack, 85 + _startPosX, 42 + _startPosY, 20, 20);
g.FillEllipse(ColorBlack, 110 + _startPosX, 42 + _startPosY, 20, 20);
}
public IMoveableObject GetMoveableObject => new DrawingObjectTank(this);
}
}
}

View File

@ -14,13 +14,13 @@ namespace Tank.DrawningObjects
{
if (Tank != null)
{
Tank = new EntityAddBase(speed, weight, bodyColor, additionalColor, bodyKit, caterpillar, tower);
Tank = new EntityTank(speed, weight, bodyColor, additionalColor, bodyKit, caterpillar, tower);
}
}
public override void DrawTransport(Graphics g)
{
if (Tank is not EntityAddBase ArmoVehicle)
if (Tank is not EntityTank ArmoVehicle)
return;
base.DrawTransport(g);
if (ArmoVehicle.BodyKit)
@ -49,7 +49,7 @@ namespace Tank.DrawningObjects
{
// Орудие
Brush bodyBrush = new SolidBrush(ArmoVehicle.AdditionalColor);
g.FillRectangle(bodyBrush, _startPosX + 111, _startPosY + 17, 70, 5);
g.FillRectangle(bodyBrush, _startPosX + 111, _startPosY + 17, 60, 5);
// Зенитное орудие
Point[] pointgun = { new Point(_startPosX + 44, _startPosY + 13), new Point(_startPosX + 45, _startPosY + 12), new Point(_startPosX + 41, _startPosY + 8), new Point(_startPosX + 41, _startPosY + 7),

View File

@ -6,13 +6,13 @@ using System.Threading.Tasks;
namespace Tank.Entities
{
public class EntityBase
public class EntityArmoVehicle
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color BodyColor { get; private set; }
public double Step => (double)Speed * 100 / Weight;
public EntityBase(int speed, double weight, Color bodyColor)
public EntityArmoVehicle(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;

View File

@ -6,13 +6,13 @@ using System.Threading.Tasks;
namespace Tank.Entities
{
public class EntityAddBase : EntityBase
public class EntityTank : EntityArmoVehicle
{
public Color AdditionalColor { get; private set; }
public bool BodyKit { get; private set; }
public bool Caterpillar { get; private set; }
public bool Tower { get; private set; }
public EntityAddBase(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool caterpillar, bool tower) : base(speed, weight, bodyColor)
public EntityTank(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool caterpillar, bool tower) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
BodyKit = bodyKit;

View File

@ -37,6 +37,7 @@
buttonArmVechicle = new Button();
buttonStep = new Button();
comboBoxStrategy = new ComboBox();
ChooseCar = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxTank).BeginInit();
SuspendLayout();
//
@ -45,7 +46,7 @@
pictureBoxTank.Location = new Point(1, 0);
pictureBoxTank.Margin = new Padding(3, 4, 3, 4);
pictureBoxTank.Name = "pictureBoxTank";
pictureBoxTank.Size = new Size(913, 599);
pictureBoxTank.Size = new Size(913, 571);
pictureBoxTank.TabIndex = 0;
pictureBoxTank.TabStop = false;
//
@ -117,7 +118,7 @@
buttonArmVechicle.Location = new Point(176, 501);
buttonArmVechicle.Margin = new Padding(3, 4, 3, 4);
buttonArmVechicle.Name = "buttonArmVechicle";
buttonArmVechicle.Size = new Size(145, 53);
buttonArmVechicle.Size = new Size(150, 53);
buttonArmVechicle.TabIndex = 15;
buttonArmVechicle.Text = "Создание бронеавтомобиля";
buttonArmVechicle.UseVisualStyleBackColor = true;
@ -143,12 +144,23 @@
comboBoxStrategy.Size = new Size(151, 28);
comboBoxStrategy.TabIndex = 19;
//
// ChooseCar
//
ChooseCar.Location = new Point(807, 120);
ChooseCar.Name = "ChooseCar";
ChooseCar.Size = new Size(94, 52);
ChooseCar.TabIndex = 20;
ChooseCar.Text = "Выбрать технику";
ChooseCar.UseVisualStyleBackColor = true;
ChooseCar.Click += ButtonSelectTank_Click;
//
// FormTank
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
BackColor = SystemColors.ButtonHighlight;
ClientSize = new Size(914, 568);
Controls.Add(ChooseCar);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonStep);
Controls.Add(buttonArmVechicle);
@ -176,5 +188,6 @@
private Button buttonArmVechicle;
private Button buttonStep;
private ComboBox comboBoxStrategy;
private Button ChooseCar;
}
}

View File

@ -7,10 +7,13 @@ namespace Tank
{
private DrawArmoVehicle? _Tank;
private AbstractStrategy? _abstractStrategy;
public DrawArmoVehicle? SelectedTank { get; private set; }
public FormTank()
{
InitializeComponent();
_abstractStrategy = null;
SelectedTank = null;
}
private void Draw()
@ -18,19 +21,30 @@ namespace Tank
if (_Tank == null)
return;
Bitmap bmp = new(pictureBoxTank.Width, pictureBoxTank.Height);
Graphics gr = Graphics.FromImage(bmp);
Bitmap bitmap = new(pictureBoxTank.Width, pictureBoxTank.Height);
Graphics gr = Graphics.FromImage(bitmap);
_Tank?.DrawTransport(gr);
pictureBoxTank.Image = bmp;
pictureBoxTank.Image = bitmap;
}
private void ButtonCreateTank_Click(object sender, EventArgs e)
{
Random rnd = new();
Color mainColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
mainColor = dialog.Color;
}
Color addColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
if (dialog.ShowDialog() == DialogResult.OK)
{
addColor = dialog.Color;
}
_Tank = new DrawTank(rnd.Next(100, 200), rnd.Next(2000, 4000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
mainColor, addColor,
Convert.ToBoolean(rnd.Next(1, 2)), Convert.ToBoolean(rnd.Next(1, 2)), Convert.ToBoolean(rnd.Next(1, 2)),
pictureBoxTank.Width, pictureBoxTank.Height);
_Tank.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
@ -66,9 +80,15 @@ namespace Tank
private void CreateButtonArmoVehicle_Click(object sender, EventArgs e)
{
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;
}
_Tank = new DrawArmoVehicle(rnd.Next(100, 200), rnd.Next(2000, 4000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
pictureBoxTank.Width, pictureBoxTank.Height);
color, pictureBoxTank.Width, pictureBoxTank.Height);
_Tank.SetPosition(rnd.Next(10, 50), rnd.Next(30, 70));
Draw();
@ -102,5 +122,11 @@ namespace Tank
_abstractStrategy = null;
}
}
private void ButtonSelectTank_Click(object sender, EventArgs e)
{
SelectedTank = _Tank;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -18,7 +18,7 @@
<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="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>

135
Tank/Tank/FormTanksCollections.Designer.cs generated Normal file
View File

@ -0,0 +1,135 @@
namespace Tank
{
partial class FormTanksCollections
{
/// <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();
UpdateButton = new Button();
DeleteButton = new Button();
AddButton = new Button();
InputNum = new TextBox();
label1 = new Label();
DrawTank = new PictureBox();
panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit();
SuspendLayout();
//
// panel1
//
panel1.Controls.Add(UpdateButton);
panel1.Controls.Add(DeleteButton);
panel1.Controls.Add(AddButton);
panel1.Controls.Add(InputNum);
panel1.Controls.Add(label1);
panel1.Dock = DockStyle.Right;
panel1.Location = new Point(550, 0);
panel1.Name = "panel1";
panel1.Size = new Size(250, 451);
panel1.TabIndex = 0;
//
// UpdateButton
//
UpdateButton.Location = new Point(10, 241);
UpdateButton.Name = "UpdateButton";
UpdateButton.Size = new Size(229, 37);
UpdateButton.TabIndex = 4;
UpdateButton.Text = "Обновить коллекцию";
UpdateButton.UseVisualStyleBackColor = true;
UpdateButton.Click += ButtonRefreshCollection_Click;
//
// DeleteButton
//
DeleteButton.Location = new Point(10, 176);
DeleteButton.Name = "DeleteButton";
DeleteButton.Size = new Size(229, 37);
DeleteButton.TabIndex = 3;
DeleteButton.Text = "Удалить технику";
DeleteButton.UseVisualStyleBackColor = true;
DeleteButton.Click += ButtonRemoveTank_Click;
//
// AddButton
//
AddButton.Location = new Point(10, 53);
AddButton.Name = "AddButton";
AddButton.Size = new Size(229, 37);
AddButton.TabIndex = 2;
AddButton.Text = "Добавить технику";
AddButton.UseVisualStyleBackColor = true;
AddButton.Click += ButtonAddTank_Click;
//
// InputNum
//
InputNum.Location = new Point(10, 133);
InputNum.Name = "InputNum";
InputNum.Size = new Size(228, 27);
InputNum.TabIndex = 1;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(2, 3);
label1.Name = "label1";
label1.Size = new Size(103, 20);
label1.TabIndex = 0;
label1.Text = "Инструменты";
//
// DrawTank
//
DrawTank.Dock = DockStyle.Fill;
DrawTank.Location = new Point(0, 0);
DrawTank.Name = "DrawTank";
DrawTank.Size = new Size(550, 451);
DrawTank.TabIndex = 1;
DrawTank.TabStop = false;
//
// CollectionsFrame
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 451);
Controls.Add(DrawTank);
Controls.Add(panel1);
Name = "CollectionsFrame";
Text = "CollectionsFrame";
panel1.ResumeLayout(false);
panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)DrawTank).EndInit();
ResumeLayout(false);
}
#endregion
private Panel panel1;
private Button UpdateButton;
private Button DeleteButton;
private Button AddButton;
private TextBox InputNum;
private Label label1;
private PictureBox DrawTank;
}
}

View File

@ -0,0 +1,71 @@
using Tank.DrawningObjects;
using Tank.Generics;
using Tank.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 Tank
{
public partial class FormTanksCollections : Form
{
private readonly TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank> _tanks;
public FormTanksCollections()
{
InitializeComponent();
_tanks = new TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>(DrawTank.Width, DrawTank.Height);
}
private void ButtonAddTank_Click(object sender, EventArgs e)
{
FormTank form = new FormTank();
if (form.ShowDialog() == DialogResult.OK)
{
if (_tanks + form.SelectedTank != -1)
{
MessageBox.Show("Объект добавлен");
DrawTank.Image = _tanks.ShowTanks();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void ButtonRemoveTank_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = -1;
try
{
pos = Convert.ToInt32(InputNum.Text);
}
catch (Exception ex) { }
if (_tanks - pos)
{
MessageBox.Show("Объект удален");
DrawTank.Image = _tanks.ShowTanks();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
DrawTank.Image = _tanks.ShowTanks();
}
}
}

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

@ -44,4 +44,4 @@ namespace Tank.MovementStrategy
}
}
}
}
}

View File

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

91
Tank/Tank/SetGeneric.cs Normal file
View File

@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tank
{
internal class SetGeneric<T> where T : class
{
// Массив объектов, которые храним
private readonly T?[] _places;
// Количество объектов в массиве
public int Count => _places.Length;
// Конструктор
public SetGeneric(int count)
{
_places = new T?[count];
}
//Добавление объекта в набор
public int Insert(T tank)
{
int index = -1;
Review

Правильнее было вызвать Insert(T obj, 0);

Правильнее было вызвать Insert(T obj, 0);
for(int i = 0; i < _places.Length; i++)
{
if (_places[i] == null)
{
index = i; break;
}
}
if (index < 0)
{
return -1;
}
for(int i = index; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = tank;
return 0;
}
// Добавление объекта в набор на конкретную позицию
public bool Insert(T tank, int position)
{
if (position < 0 || position >= Count)
return false;
if (_places[position] == null)
{
_places[position] = tank;
return true;
}
int index = -1;
for(int i = position; i < Count; i++)
{
if (_places[i] == null)
{
index = i; break;
}
}
if (index < 0)
return false;
for(int i = index; index > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = tank;
return true;
}
// Удаление объекта из набора с конкретной позиции
public bool Remove(int position)
{
if (position < 0 || position >= Count)
return false;
_places[position] = null;
return true;
}
// Получение объекта из набора по позиции
public T? Get(int position)
{
if (position < 0 || position >= Count)
return null;
return _places[position];
}
}
}

View File

@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;
using Tank.DrawningObjects;
using Tank.MovementStrategy;
namespace Tank.Generics
{
internal class TanksGenericCollection<T, U>
where T : DrawArmoVehicle
where U : IMoveableObject
{
// Ширина и высота окна прорисовки
private readonly int _pictureWidth;
private readonly int _pictureHeight;
// Размеры занимаемого объектом места
private readonly int _placeSizeWidth = 180;
private readonly int _placeSizeHeight = 90;
// Набор объектов
private readonly SetGeneric<T> _collection;
// Конструктор
public TanksGenericCollection(int pictureWidth, int pictureHeight)
{
int width = pictureWidth / _placeSizeWidth;
int height = pictureHeight / _placeSizeHeight;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_collection = new SetGeneric<T>(width * height);
}
// Перегрузка оператора сложения
public static int? operator +(TanksGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
}
return collect?._collection.Insert(obj);
}
// Перегрузка оператора вычитания
public static bool operator -(TanksGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
if (obj == null)
{
return false;
}
return collect._collection.Remove(pos);
}
// Получение объекта IMoveableObject
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
// Вывод всего набора объектов
public Bitmap ShowTanks()
{
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)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
{
DrawArmoVehicle? tank = _collection.Get(i);
if (tank == null)
continue;
tank.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
tank.DrawTransport(g);
}
}
}
}