Lab3 WarmlyShip Barsukov

This commit is contained in:
frog24 2023-11-17 22:36:23 +04:00
parent a89f92cdc3
commit 99de878d24
10 changed files with 482 additions and 13 deletions

View File

@ -26,7 +26,6 @@ namespace ProjectWarmlyShip.MovementStrategy
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != Status.InProgress)
@ -44,9 +43,7 @@ namespace ProjectWarmlyShip.MovementStrategy
protected bool MoveRight() => MoveTo(DirectionType.Right);
protected bool MoveUp() => MoveTo(DirectionType.Up);
protected bool MoveDown() => MoveTo(DirectionType.Down);
protected ObjectParametrs? GetObjectParametrs => _movebleObject?.GetObjectPosition;
protected int? GetStep()
{
if (_state != Status.InProgress)

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectWarmlyShip.Entities;
using ProjectWarmlyShip.MovementStrategy;
namespace ProjectWarmlyShip.DrawingObjects
{
@ -16,7 +17,7 @@ namespace ProjectWarmlyShip.DrawingObjects
protected int _startPosY;
protected readonly int _shipWidth = 100;
protected readonly int _shipHeight = 30;
public IMoveableObject GetMoveableObject => new DrawingObjectShip(this);
public DrawingShip(int speed, double weight, Color mainColor, int width, int heigth)
{
if (width <= _shipWidth || heigth <= _shipHeight)
@ -70,7 +71,6 @@ namespace ProjectWarmlyShip.DrawingObjects
_ => false,
};
}
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityShip == null)

View File

@ -0,0 +1,123 @@
namespace ProjectWarmlyShip
{
partial class FormShipCollection
{
/// <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()
{
pictureBoxCollection = new PictureBox();
Tools = new GroupBox();
maskedTextBoxNumber = new TextBox();
buttonRefresh = new Button();
buttonDeleteShip = new Button();
buttonAddShip = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
Tools.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(700, 460);
pictureBoxCollection.TabIndex = 0;
pictureBoxCollection.TabStop = false;
//
// Tools
//
Tools.Controls.Add(maskedTextBoxNumber);
Tools.Controls.Add(buttonRefresh);
Tools.Controls.Add(buttonDeleteShip);
Tools.Controls.Add(buttonAddShip);
Tools.Location = new Point(700, 0);
Tools.Name = "Tools";
Tools.Size = new Size(180, 460);
Tools.TabIndex = 1;
Tools.TabStop = false;
Tools.Text = "Tools";
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(10, 120);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(165, 23);
maskedTextBoxNumber.TabIndex = 4;
//
// buttonRefresh
//
buttonRefresh.Location = new Point(10, 205);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(165, 50);
buttonRefresh.TabIndex = 2;
buttonRefresh.Text = "Refresh";
buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += ButtonRefreshCollection;
//
// buttonDeleteShip
//
buttonDeleteShip.Location = new Point(10, 150);
buttonDeleteShip.Name = "buttonDeleteShip";
buttonDeleteShip.Size = new Size(165, 50);
buttonDeleteShip.TabIndex = 1;
buttonDeleteShip.Text = "Delete Ship";
buttonDeleteShip.UseVisualStyleBackColor = true;
buttonDeleteShip.Click += ButtonRemoveShip_Click;
//
// buttonAddShip
//
buttonAddShip.Location = new Point(10, 20);
buttonAddShip.Name = "buttonAddShip";
buttonAddShip.Size = new Size(165, 50);
buttonAddShip.TabIndex = 0;
buttonAddShip.Text = "Add Ship";
buttonAddShip.UseVisualStyleBackColor = true;
buttonAddShip.Click += ButtonAddShip_Click;
//
// FormShipCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
Controls.Add(Tools);
Controls.Add(pictureBoxCollection);
Name = "FormShipCollection";
Text = "FormShipCollection";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
Tools.ResumeLayout(false);
Tools.PerformLayout();
ResumeLayout(false);
}
#endregion
private PictureBox pictureBoxCollection;
private GroupBox Tools;
private TextBox maskedTextBoxNumber;
private Button buttonRefresh;
private Button buttonDeleteShip;
private Button buttonAddShip;
}
}

View File

@ -0,0 +1,67 @@
using ProjectWarmlyShip.DrawingObjects;
using ProjectWarmlyShip.Generics;
using ProjectWarmlyShip.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;
using ProjectWarmlyShip.DrawingObjects;
using ProjectWarmlyShip.Generics;
using ProjectWarmlyShip.MovementStrategy;
namespace ProjectWarmlyShip
{
public partial class FormShipCollection : Form
{
private readonly ShipsGenericCollection<DrawingShip, DrawingObjectShip> _ships;
public FormShipCollection()
{
InitializeComponent();
_ships = new ShipsGenericCollection<DrawingShip, DrawingObjectShip>
(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
private void ButtonAddShip_Click(object sender, EventArgs e)
{
WarmlyShipForm form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_ships + form.SelectedShip != -1)
{
MessageBox.Show("Object Inserted");
pictureBoxCollection.Image = _ships.ShowShips();
}
else
{
MessageBox.Show("Object Not Inserted");
}
}
}
private void ButtonRemoveShip_Click(Object sender, EventArgs e)
{
if (MessageBox.Show("Delete Object?", "Delete", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_ships - pos != false)
{
MessageBox.Show("Object Deleted");
pictureBoxCollection.Image = _ships.ShowShips();
}
else
{
MessageBox.Show("Object Not Deleted");
}
}
private void ButtonRefreshCollection(object sender, EventArgs e)
{
pictureBoxCollection.Image = _ships.ShowShips();
}
}
}

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

@ -37,6 +37,7 @@
button1 = new Button();
comboBoxStrategy = new ComboBox();
buttonStep = new Button();
buttonSelectShip = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyShip).BeginInit();
SuspendLayout();
//
@ -53,9 +54,9 @@
// buttonCreate
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 426);
buttonCreate.Location = new Point(10, 425);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(75, 23);
buttonCreate.Size = new Size(75, 25);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Create Ship";
buttonCreate.UseVisualStyleBackColor = true;
@ -112,9 +113,9 @@
// button1
//
button1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
button1.Location = new Point(93, 426);
button1.Location = new Point(90, 425);
button1.Name = "button1";
button1.Size = new Size(120, 23);
button1.Size = new Size(120, 25);
button1.TabIndex = 6;
button1.Text = "Create Warmly Ship";
button1.UseVisualStyleBackColor = true;
@ -141,11 +142,23 @@
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += ButtonStep_Click;
//
// buttonSelectShip
//
buttonSelectShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonSelectShip.Location = new Point(215, 425);
buttonSelectShip.Name = "buttonSelectShip";
buttonSelectShip.Size = new Size(75, 25);
buttonSelectShip.TabIndex = 10;
buttonSelectShip.Text = "Select";
buttonSelectShip.UseVisualStyleBackColor = true;
buttonSelectShip.Click += ButtonSelectShip_Click;
//
// WarmlyShipForm
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
Controls.Add(buttonSelectShip);
Controls.Add(buttonStep);
Controls.Add(comboBoxStrategy);
Controls.Add(button1);
@ -174,5 +187,6 @@
private Button button1;
private ComboBox comboBoxStrategy;
private Button buttonStep;
private Button buttonSelectShip;
}
}

View File

@ -7,9 +7,12 @@ namespace ProjectWarmlyShip
{
private DrawingShip? _drawingShip;
private AbstractStrategy? _abstractStrategy;
public DrawingShip SelectedShip { get; set; }
public WarmlyShipForm()
{
InitializeComponent();
_abstractStrategy = null;
SelectedShip = null;
}
private void Draw()
{
@ -26,10 +29,21 @@ namespace ProjectWarmlyShip
private void ButtonCreateWarmlyShip_Click(object sender, EventArgs e)
{
Random random = new Random();
Color color = Color.FromArgb(random.Next(0, 256));
ColorDialog dialog = new ColorDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
Color opt_color = Color.FromArgb(random.Next(0, 256));
if (dialog.ShowDialog() == DialogResult.OK)
{
opt_color = dialog.Color;
}
_drawingShip = new DrawingWarmlyShip(
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)),
color,
opt_color,
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxWarmlyShip.Width,
@ -40,10 +54,17 @@ namespace ProjectWarmlyShip
private void ButtonCreateShip_Click(object sender, EventArgs e)
{
Random random = new Random();
Color color = Color.FromArgb(random.Next(0, 256));
ColorDialog dialog = new ColorDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
_drawingShip = new DrawingShip(
random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
color,
pictureBoxWarmlyShip.Width,
pictureBoxWarmlyShip.Height);
_drawingShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
@ -110,5 +131,10 @@ namespace ProjectWarmlyShip
_abstractStrategy = null;
}
}
private void ButtonSelectShip_Click(object sender, EventArgs e)
{
SelectedShip = _drawingShip;
DialogResult = DialogResult.OK;
}
}
}

View File

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

View File

@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectWarmlyShip.Generics
{
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 ship)
{
if (_places[0] == null)
{
_places[0] = ship;
}
else
{
int nullIndex = 0;
for (int i = 0; i < _places.Length; i++)
{
if (_places[i] == null)
{
nullIndex = i;
break;
}
}
if (nullIndex == 0)
{
return -1;
}
for (int i = nullIndex; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = ship;
}
return 0;
}
public int Insert(T ship, int position)
{
if (position >= Count)
{
return -1;
}
if (_places[position] == null)
{
_places[position] = ship;
}
else
{
int nullIndex = position;
for (int i = position; i < _places.Length; i++)
{
if (_places[i] == null)
{
nullIndex = i;
break;
}
}
if (nullIndex == position)
{
return -1;
}
for (int i = nullIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = ship;
}
return position;
}
public bool Remove(int position)
{
if (position >= Count)
{
return false;
}
_places[position] = null;
return true;
}
public T? Get(int position)
{
return _places[position];
}
}
}

View File

@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectWarmlyShip.DrawingObjects;
using ProjectWarmlyShip.MovementStrategy;
namespace ProjectWarmlyShip.Generics
{
internal class ShipsGenericCollection<T, U>
where T : DrawingShip
where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 140;
private readonly int _placeSizeHeight = 80;
private readonly SetGeneric<T> _collection;
public ShipsGenericCollection(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 +(ShipsGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
}
return collect?._collection.Insert(obj);
}
public static bool operator -(ShipsGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
{
collect._collection.Remove(pos);
return true;
}
return false;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
public Bitmap ShowShips()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackgroud(gr);
DrawObjects(gr);
return bmp;
}
private void DrawBackgroud(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++)
{
DrawingShip? ship = _collection.Get(i);
if (ship != null)
{
ship.SetPosition((width - (i % width) - 1) * _placeSizeWidth, (i / height) * _placeSizeHeight + 15);
ship.DrawTrasport(g);
}
}
}
}
}