Готовая 3 лабораторная

This commit is contained in:
russell 2023-11-04 16:22:00 +04:00
parent 5b001c5b2f
commit f92019e9c6
9 changed files with 581 additions and 18 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.Entities;
using DumpTruck.MovementStrategy;
namespace DumpTruck.DrawingObjects
{
@ -54,6 +55,10 @@ namespace DumpTruck.DrawingObjects
/// Высота объекта
/// </summary>
public int GetHeight => _truckHeight;
/// <summary>
/// Получение объекта IMoveableObject из объекта DrawingTruck
/// </summary>
public IMoveableObject GetMoveableObject => new DrawningObjectTruck(this);
/// <summary>
/// Конструктор

View File

@ -37,6 +37,7 @@
this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
this.ButtonCreateTruck = new System.Windows.Forms.Button();
this.ButtonStep = new System.Windows.Forms.Button();
this.buttonSelectTruck = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxDumpTruck)).BeginInit();
this.SuspendLayout();
//
@ -143,11 +144,22 @@
this.ButtonStep.UseVisualStyleBackColor = true;
this.ButtonStep.Click += new System.EventHandler(this.ButtonStep_Click);
//
// buttonSelectTruck
//
this.buttonSelectTruck.Location = new System.Drawing.Point(284, 408);
this.buttonSelectTruck.Name = "buttonSelectTruck";
this.buttonSelectTruck.Size = new System.Drawing.Size(75, 30);
this.buttonSelectTruck.TabIndex = 9;
this.buttonSelectTruck.Text = "Выбрать";
this.buttonSelectTruck.UseVisualStyleBackColor = true;
this.buttonSelectTruck.Click += new System.EventHandler(this.buttonSelectTruck_Click);
//
// FormDumpTruck
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.buttonSelectTruck);
this.Controls.Add(this.ButtonStep);
this.Controls.Add(this.ButtonCreateTruck);
this.Controls.Add(this.comboBoxStrategy);
@ -175,5 +187,6 @@
private ComboBox comboBoxStrategy;
private Button ButtonCreateTruck;
private Button ButtonStep;
private Button buttonSelectTruck;
}
}

View File

@ -5,15 +5,19 @@ namespace DumpTruck
{
public partial class FormDumpTruck : Form
{
private DrawingTruck? _drawingTruck;
private AbstractStrategy? _strategy;
public DrawingTruck? SelectedTruck { get; private set; }
public FormDumpTruck()
{
InitializeComponent();
_strategy = null;
SelectedTruck = null;
}
private DrawingTruck? _drawingTruck;
private AbstractStrategy? _abstractStrategy;
private void Draw()
{
if (_drawingTruck == null)
@ -55,11 +59,32 @@ namespace DumpTruck
private void ButtonCreateDumpTruck_Click(object sender, EventArgs e)
{
Random random = new();
Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
bodyColor = dialog.Color;
}
Color dumpBoxColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog secondDialog = new();
if (secondDialog.ShowDialog() == DialogResult.OK)
{
dumpBoxColor = secondDialog.Color;
}
Color tentColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog thirdDialog = new();
if (thirdDialog.ShowDialog() == DialogResult.OK)
{
tentColor = thirdDialog.Color;
}
_drawingTruck = new DrawingDumpTruck(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
bodyColor,
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
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)),
tentColor,
dumpBoxColor,
pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
@ -68,7 +93,13 @@ namespace DumpTruck
private void ButtonCreateTruck_Click(object sender, EventArgs e)
{
Random random = new();
_drawingTruck = new DrawingTruck(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
bodyColor = dialog.Color;
}
_drawingTruck = new DrawingTruck(random.Next(100, 300), random.Next(1000, 3000), bodyColor,
pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
@ -82,33 +113,39 @@ namespace DumpTruck
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
_strategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
if (_strategy == null)
{
return;
}
_abstractStrategy.SetData(new
_strategy.SetData(new
DrawningObjectTruck(_drawingTruck), pictureBoxDumpTruck.Width,
pictureBoxDumpTruck.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
if (_strategy == null)
{
return;
}
_abstractStrategy.MakeStep();
_strategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
if (_strategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
_strategy = null;
}
}
private void buttonSelectTruck_Click(object sender, EventArgs e)
{
SelectedTruck = _drawingTruck;
DialogResult = DialogResult.OK;
}
}
}

137
DumpTruck/FormTruckCollection.Designer.cs generated Normal file
View File

@ -0,0 +1,137 @@
namespace DumpTruck
{
partial class FormTruckCollection
{
/// <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()
{
this.panelTools = new System.Windows.Forms.Panel();
this.buttonRefreshCollection = new System.Windows.Forms.Button();
this.labelTools = new System.Windows.Forms.Label();
this.buttonRemoveTruck = new System.Windows.Forms.Button();
this.buttonAddTruck = new System.Windows.Forms.Button();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.panelTools.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.SuspendLayout();
//
// panelTools
//
this.panelTools.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.panelTools.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panelTools.Controls.Add(this.maskedTextBoxNumber);
this.panelTools.Controls.Add(this.buttonRefreshCollection);
this.panelTools.Controls.Add(this.labelTools);
this.panelTools.Controls.Add(this.buttonRemoveTruck);
this.panelTools.Controls.Add(this.buttonAddTruck);
this.panelTools.Location = new System.Drawing.Point(596, 3);
this.panelTools.Name = "panelTools";
this.panelTools.Size = new System.Drawing.Size(203, 445);
this.panelTools.TabIndex = 0;
//
// buttonRefreshCollection
//
this.buttonRefreshCollection.Location = new System.Drawing.Point(25, 168);
this.buttonRefreshCollection.Name = "buttonRefreshCollection";
this.buttonRefreshCollection.Size = new System.Drawing.Size(150, 30);
this.buttonRefreshCollection.TabIndex = 3;
this.buttonRefreshCollection.Text = "Обновить коллекцию";
this.buttonRefreshCollection.UseVisualStyleBackColor = true;
this.buttonRefreshCollection.Click += new System.EventHandler(this.buttonRefreshCollection_Click);
//
// labelTools
//
this.labelTools.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.labelTools.AutoSize = true;
this.labelTools.Location = new System.Drawing.Point(62, 0);
this.labelTools.Name = "labelTools";
this.labelTools.Size = new System.Drawing.Size(83, 15);
this.labelTools.TabIndex = 0;
this.labelTools.Text = "Инструменты";
//
// buttonRemoveTruck
//
this.buttonRemoveTruck.Location = new System.Drawing.Point(25, 109);
this.buttonRemoveTruck.Name = "buttonRemoveTruck";
this.buttonRemoveTruck.Size = new System.Drawing.Size(150, 30);
this.buttonRemoveTruck.TabIndex = 2;
this.buttonRemoveTruck.Text = "Удалить грузовик";
this.buttonRemoveTruck.UseVisualStyleBackColor = true;
this.buttonRemoveTruck.Click += new System.EventHandler(this.buttonRemoveTruck_Click);
//
// buttonAddTruck
//
this.buttonAddTruck.Location = new System.Drawing.Point(25, 30);
this.buttonAddTruck.Name = "buttonAddTruck";
this.buttonAddTruck.Size = new System.Drawing.Size(150, 30);
this.buttonAddTruck.TabIndex = 1;
this.buttonAddTruck.Text = "Добавить грузовик";
this.buttonAddTruck.UseVisualStyleBackColor = true;
this.buttonAddTruck.Click += new System.EventHandler(this.buttonAddTruck_Click);
//
// pictureBoxCollection
//
this.pictureBoxCollection.Location = new System.Drawing.Point(2, 3);
this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(588, 445);
this.pictureBoxCollection.TabIndex = 1;
this.pictureBoxCollection.TabStop = false;
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(25, 75);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 23);
this.maskedTextBoxNumber.TabIndex = 4;
//
// FormTruckCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.pictureBoxCollection);
this.Controls.Add(this.panelTools);
this.Name = "FormTruckCollection";
this.Text = "FormTruckCollection";
this.panelTools.ResumeLayout(false);
this.panelTools.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.ResumeLayout(false);
}
#endregion
private Panel panelTools;
private Button buttonAddTruck;
private Label labelTools;
private Button buttonRefreshCollection;
private Button buttonRemoveTruck;
private PictureBox pictureBoxCollection;
private MaskedTextBox maskedTextBoxNumber;
}
}

View File

@ -0,0 +1,77 @@
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 DumpTruck.Generics;
using DumpTruck.DrawingObjects;
using DumpTruck.MovementStrategy;
namespace DumpTruck
{
/// <summary>
/// Форма для работы с набором объектов класса DrawingTruck
/// </summary>
public partial class FormTruckCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
private readonly TrucksGenericCollection<DrawingTruck, DrawningObjectTruck> _trucks;
/// <summary>
/// Конструктор
/// </summary>
public FormTruckCollection()
{
InitializeComponent();
_trucks = new TrucksGenericCollection<DrawingTruck, DrawningObjectTruck>(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
private void buttonAddTruck_Click(object sender, EventArgs e)
{
FormDumpTruck form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_trucks + form.SelectedTruck != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _trucks.ShowTrucks();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void buttonRemoveTruck_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_trucks - pos)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _trucks.ShowTrucks();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void buttonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _trucks.ShowTrucks();
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<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

@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.Generics
{
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetGeneric<T>
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T?[] _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T?[count];
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="truck">Добавляемый грузовик</param>
/// <returns></returns>
public int Insert(T truck)
{
return Insert(truck, 0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="truck">Добавляемый грузовик</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T truck, int position)
{
if (position < 0 || position >= Count) return -1;
int index = -1;
for (int i = position; i < Count; i++)
{
if (_places[i] == null)
{
index = i;
break;
}
}
if (index < 0) return -1;
for (int i = index; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = truck;
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position >= Count) return false;
_places[position] = null;
return true;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
{
if (position < 0 || position >= Count) return null;
return _places[position];
}
}
}

View File

@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.DrawingObjects;
using DumpTruck.MovementStrategy;
namespace DumpTruck.Generics
{
/// <summary>
/// Параметризованный класс для набора объектов DrawingTruck
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class TrucksGenericCollection<T, U>
where T : DrawingTruck
where U : IMoveableObject
{
/// <summary>
/// Ширина окна прорисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна прорисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 180;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 100;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetGeneric<T> _collection;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
public TrucksGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(TrucksGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
}
return collect._collection.Insert(obj);
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(TrucksGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
{
return collect._collection.Remove(pos);
}
return false;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowTrucks()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
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);
}
}
/// <summary>
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
{
T? obj = _collection.Get(i);
if (obj != null)
{
obj.SetPosition(i % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, i / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
obj.DrawTransport(g);
}
}
}
}
}

View File

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