Youdakova D.R. PIbd-21 LabWork06 #6

Closed
Daniya_Youdakova wants to merge 6 commits from LabWork06 into LabWork05
16 changed files with 279 additions and 37 deletions

View File

@ -45,7 +45,6 @@ namespace AircraftCarrier
yNumOfCells = (int)Math.Ceiling(_drawningObject.Step / _size_y);
xObjOffset = (int)(Left / _size_x);
yObjOffset = (int)Math.Floor(Top / _size_y);
for (int i = 0; i < yNumOfCells; i++)
{
if (!roadIsClear)
@ -71,7 +70,6 @@ namespace AircraftCarrier
yNumOfCells = (int)Math.Ceiling(_drawningObject.Step / _size_y);
xObjOffset = (int)(Left / _size_x);
yObjOffset = (int)Math.Ceiling(Bottom / _size_y);
for (int i = 0; i < yNumOfCells; i++)
{
if (!roadIsClear)
@ -123,6 +121,7 @@ namespace AircraftCarrier
yNumOfCells = (int)Math.Ceiling((Bottom - Top) / _size_y);
xObjOffset = (int)(Right / _size_x);
yObjOffset = (int)Math.Ceiling(Top / _size_y);
for (int i = 0; i < yNumOfCells; i++)
{
if (!roadIsClear)
@ -158,13 +157,11 @@ namespace AircraftCarrier
}
int x = _random.Next(0, 10);
int y = _random.Next(50, 100);
(float Left, float Top, float Right, float Bottom) = _drawningObject.GetCurrentPosition();
int xNumOfCells = (int)Math.Ceiling(Right / _size_x) - (int)Math.Floor(Left / _size_x);
int yNumOfCells = (int)Math.Ceiling(Bottom / _size_y) - (int)Math.Floor(Top / _size_y);
int xObjOffset = (int)(x / _size_x);
int yObjOffset = (int)(y / _size_y);
while (y < _height - (Bottom - Top))
{
while (x < _width - (Right - Left))
@ -181,7 +178,6 @@ namespace AircraftCarrier
y += (int)_size_y;
yObjOffset = (int)(y / _size_y);
}
return false;
}
private bool AreaIsFree(int xNumOfCells, int yNumOfCells, int xObjOffset, int yObjOffset)

View File

@ -52,7 +52,6 @@ namespace AircraftCarrier
{
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeigth = height;
}
@ -127,15 +126,24 @@ namespace AircraftCarrier
{
_startPosX = _pictureWidth.Value - _aircraftcarrierWidth;
}
if (_startPosY + _aircraftcarrierHeight > _pictureHeigth)
{
_startPosY = _pictureHeigth.Value - _aircraftcarrierHeight;
}
}
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _aircraftcarrierWidth, _startPosY + _aircraftcarrierHeight);
}
public void ReturnColor(Color returnColor)
{
if (AircraftCarrier is not EntityModernAircraftCarrier modernAircraftCarrier)
{
return;
}
modernAircraftCarrier.DopColor = returnColor;
}
}
}

View File

@ -14,14 +14,6 @@ dopColor, bool flightDeck, bool hangarDeck, bool route) :
{
AircraftCarrier = new EntityModernAircraftCarrier(speed, weight, bodyColor, dopColor, flightDeck, hangarDeck, route);
}
public void ReturnColor(Color returnColor)
{
if (AircraftCarrier is not EntityModernAircraftCarrier modernAircraftCarrier)
{
return;
}
modernAircraftCarrier.DopColor = returnColor;
}
public override void DrawTransport(Graphics g)
{
if (AircraftCarrier is not EntityModernAircraftCarrier modernAircraftCarrier)

View File

@ -30,5 +30,8 @@ namespace AircraftCarrier
{
_aircraftcarrier.DrawTransport(g);
}
public string GetInfo() => _aircraftcarrier?.AircraftCarrier.ToString();
public static IDrawningObject Create(string data) => new DrawningObjectAircraftCarrier(data.CreateDrawningAircraftCarrier());
}
}

View File

@ -8,6 +8,7 @@ namespace AircraftCarrier
{
public class EntityAircraftCarrier
{
private static readonly char _separatorForObject = ':';
public int Speed { get; private set; }
public float Weight { get; private set; }
public Color BodyColor { get; set; }
@ -19,5 +20,10 @@ namespace AircraftCarrier
Weight = weight <= 0 ? rnd.Next(40, 70) : weight;
BodyColor = bodyColor;
}
public override string ToString()
{
var str = $"{Speed}{_separatorForObject}{Weight}{_separatorForObject}{BodyColor.Name}";
return str;
}
}
}

View File

@ -4,11 +4,13 @@ using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using static System.Reflection.Metadata.BlobBuilder;
namespace AircraftCarrier
{
internal class EntityModernAircraftCarrier : EntityAircraftCarrier
{
private static readonly char _separatorForObject = ':';
public Color DopColor { get; set; }
public bool FlightDeck { get; private set; }
public bool HangarDeck { get; private set; }
@ -20,5 +22,10 @@ namespace AircraftCarrier
HangarDeck = hangarDeck;
Route = route;
}
public override string ToString()
{
var str = base.ToString();
return $"{str}{_separatorForObject}{DopColor.Name}{_separatorForObject}{FlightDeck}{_separatorForObject}{HangarDeck}{_separatorForObject}{Route}";
}
}
}

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier
{
internal static class ExtentionAircraftCarrier
{
private static readonly char _separatorForObject = ':';
public static DrawningAircraftCarrier CreateDrawningAircraftCarrier(this string info)
{
string[] strs = info.Split(_separatorForObject);
if (strs.Length == 3)
{
return new DrawningAircraftCarrier(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
}
if (strs.Length == 7)
{
return new DrawningModernAircraftCarrier(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]),
Color.FromName(strs[3]), Convert.ToBoolean(strs[4]),
Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]));
}
return null;
}
public static string GetDataForSave(this DrawningAircraftCarrier drawningAircraftCarrier)
{
var aircraftcarrier = drawningAircraftCarrier.AircraftCarrier;
var str =
$"{aircraftcarrier.Speed}{_separatorForObject}{aircraftcarrier.Weight}{_separatorForObject}{aircraftcarrier.BodyColor.Name}";
if (aircraftcarrier is not EntityModernAircraftCarrier modernAircraftCarrier)
{
return str;
}
return
$"{str}{_separatorForObject}{modernAircraftCarrier.DopColor.Name}{_separatorForObject}{modernAircraftCarrier.FlightDeck}{_separatorForObject}{modernAircraftCarrier.HangarDeck}{_separatorForObject}{modernAircraftCarrier.Route}";
}
}
}

View File

@ -90,7 +90,6 @@
this.labelModifiedObject.TabIndex = 9;
this.labelModifiedObject.Text = "Продвинутый";
this.labelModifiedObject.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//this.labelModifiedObject.Click += new System.EventHandler(this.labelModifiedObject_Click);
this.labelModifiedObject.MouseDown += new System.Windows.Forms.MouseEventHandler(this.LabelObject_MouseDown);
//
// labelSimpleObject
@ -331,7 +330,6 @@
this.buttonCancel.TabIndex = 4;
this.buttonCancel.Text = "Отмена";
this.buttonCancel.UseVisualStyleBackColor = true;
//this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// FormAircraftCarrierConfig
//

View File

@ -62,17 +62,21 @@ namespace AircraftCarrier
{
e.Effect = DragDropEffects.None;
}
}
private void PanelObject_DragDrop(object sender, DragEventArgs e)
{
switch (e.Data.GetData(DataFormats.Text).ToString())
{
case "labelSimpleObject":
_aircraftcarrier = new DrawningAircraftCarrier((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, Color.White);
_aircraftcarrier = new DrawningAircraftCarrier((int)numericUpDownSpeed.Value,
(int)numericUpDownWeight.Value, Color.White);
break;
case "labelModifiedObject":
_aircraftcarrier = new DrawningModernAircraftCarrier((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, Color.White, Color.Black,
checkBoxFlightDeck.Checked, checkBoxHangarDeck.Checked, checkBoxRoute.Checked);
_aircraftcarrier = new DrawningModernAircraftCarrier((int)numericUpDownSpeed.Value,
(int)numericUpDownWeight.Value, Color.White, Color.Black,
checkBoxFlightDeck.Checked, checkBoxHangarDeck.Checked,
checkBoxRoute.Checked);
break;
}
DrawAircraftCarrier();

View File

@ -46,9 +46,16 @@
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddAircraftCarrier = new System.Windows.Forms.Button();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.menuStrip = new System.Windows.Forms.MenuStrip();
this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.groupBox1.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.menuStrip.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
@ -64,9 +71,9 @@
this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.buttonAddAircraftCarrier);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(593, 0);
this.groupBox1.Location = new System.Drawing.Point(593, 28);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(250, 519);
this.groupBox1.Size = new System.Drawing.Size(250, 491);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
@ -224,13 +231,55 @@
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Location = new System.Drawing.Point(0, 28);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(593, 519);
this.pictureBox.Size = new System.Drawing.Size(593, 491);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//this.pictureBox.Click += new System.EventHandler(this.pictureBox1_Click);
//
// menuStrip
//
this.menuStrip.ImageScalingSize = new System.Drawing.Size(20, 20);
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.файлToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(843, 28);
this.menuStrip.TabIndex = 2;
//this.menuStrip.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.menuStrip_ItemClicked);
//
// файлToolStripMenuItem
//
this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.SaveToolStripMenuItem,
this.LoadToolStripMenuItem});
this.файлToolStripMenuItem.Name = айлToolStripMenuItem";
this.файлToolStripMenuItem.Size = new System.Drawing.Size(59, 24);
this.файлToolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(177, 26);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(177, 26);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
// openFileDialog
//
this.openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
this.saveFileDialog.Filter = "txt file |*.txt";
//
// FormMapWithSetAircraftCarriers
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
@ -238,6 +287,8 @@
this.ClientSize = new System.Drawing.Size(843, 519);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.menuStrip);
this.MainMenuStrip = this.menuStrip;
this.Name = "FormMapWithSetAircraftCarriers";
this.Text = "Карта с набором объектов";
this.groupBox1.ResumeLayout(false);
@ -245,7 +296,10 @@
this.groupBoxMaps.ResumeLayout(false);
this.groupBoxMaps.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
@ -268,5 +322,11 @@
private Button buttonDeleteMap;
private Button buttonAddMap;
private TextBox textBoxNewMapName;
private MenuStrip menuStrip;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -69,6 +69,11 @@ pictureBox.Height);
MessageBoxIcon.Error);
return;
}
if (textBoxNewMapName.Text.Contains('|') || textBoxNewMapName.Text.Contains(':') || textBoxNewMapName.Text.Contains(';'))
{
MessageBox.Show("Присутствуют недопустимые символы", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_mapsCollection.AddMap(textBoxNewMapName.Text,
_mapsDict[comboBoxSelectorMap.Text]);
ReloadMaps();
@ -210,5 +215,36 @@ string.Empty] - pos != null)
}
pictureBox.Image = _mapAircraftCarriersCollectionGeneric.MoveObject(dir);
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.SaveData(saveFileDialog.FileName))
{
MessageBox.Show("Сохранение прошло успешно", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не сохранилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.LoadData(openFileDialog.FileName))
{
MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
ReloadMaps();
}
else
{
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}

View File

@ -57,4 +57,13 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>144, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>311, 17</value>
</metadata>
</root>

View File

@ -13,5 +13,6 @@ namespace AircraftCarrier
void MoveObject(Direction direction);
void DrawningObject(Graphics g);
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
string GetInfo();
}
}

View File

@ -111,6 +111,26 @@ namespace AircraftCarrier
}
return new(_pictureWidth, _pictureHeight);
}
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var aircraftcarrier in _setAircraftCarriers.GetAircraftCarriers())
{
data += $"{aircraftcarrier.GetInfo()}{separatorData}";
}
return data;
}
/// <summary>
/// Загрузка списка из массива строк
/// </summary>
/// <param name="records"></param>
public void LoadData(string[] records)
{
foreach (var rec in records)
{
_setAircraftCarriers.Insert(DrawningObjectAircraftCarrier.Create(rec) as T);
}
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>
@ -164,10 +184,8 @@ namespace AircraftCarrier
{
int yNumOfPlaces = _pictureHeight / _placeSizeHeight;
int xNumOfPlaces = _pictureWidth / _placeSizeWidth;
int rowNum = yNumOfPlaces - 1;
int columnNum = 0;
for (int i = 0; i < _setAircraftCarriers.Count; i++)
{
if (_setAircraftCarriers[i] != null)

View File

@ -11,7 +11,7 @@ namespace AircraftCarrier
/// <summary>
/// Словарь (хранилище) с картами
/// </summary>
readonly Dictionary<string, MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier,
readonly Dictionary<string, MapWithSetAircraftCarriersGeneric<IDrawningObject,
AbstractMap>> _mapStorages;
/// <summary>
/// Возвращение списка названий карт
@ -30,10 +30,11 @@ namespace AircraftCarrier
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
private readonly char separatorDict = '|';
private readonly char separatorData = ';';
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string,
MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier, AbstractMap>>();
_mapStorages = new Dictionary<string,MapWithSetAircraftCarriersGeneric<IDrawningObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -63,7 +64,7 @@ namespace AircraftCarrier
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier, AbstractMap> this[string
public MapWithSetAircraftCarriersGeneric<IDrawningObject, AbstractMap> this[string
ind]
{
get
@ -72,7 +73,7 @@ namespace AircraftCarrier
return mapWithSetAircraftCarriersGeneric;
}
}
public MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier, AbstractMap> this[int index]
public MapWithSetAircraftCarriersGeneric<IDrawningObject, AbstractMap> this[int index]
{
get
{
@ -88,5 +89,66 @@ namespace AircraftCarrier
}
}
}
/// <summary>
/// Сохранение информации по автомобилям в хранилище в файл
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns></returns>
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = new(filename))
{
sw.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorages)
{
sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
}
}
return true;
}
/// <summary>
/// Загрузка нформации по автомобилям на парковках из файла
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = new(filename))
{
string str = "";
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
//очищаем записи
_mapStorages.Clear();
while ((str = sr.ReadLine()) != null)
{
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "HardMap":
map = new ComplexMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetAircraftCarriersGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
return true;
}
}
}

View File

@ -55,6 +55,7 @@ namespace AircraftCarrier
_places.Insert(position, AircraftCarrier);
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
@ -75,7 +76,6 @@ namespace AircraftCarrier
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T this[int position]
{
get
@ -86,14 +86,15 @@ namespace AircraftCarrier
{
Insert(value, position);
}
}
public IEnumerable<T> GetCars()
public IEnumerable<T> GetAircraftCarriers()
{
foreach (var car in _places)
foreach (var aircraftcarrier in _places)
{
if (car != null)
if (aircraftcarrier != null)
{
yield return car;
yield return aircraftcarrier;
}
else
{
@ -101,6 +102,5 @@ namespace AircraftCarrier
}
}
}
}
}