labwork03

This commit is contained in:
Danil Markov 2022-10-23 17:01:38 +04:00
parent 6fa8023411
commit 969d383e4a
13 changed files with 560 additions and 254 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace ContainerShip
{
internal enum Direction
public enum Direction
{
None = 0,
Up = 1,

View File

@ -25,7 +25,7 @@ namespace ContainerShip
}
public void SetObject(int x, int y, int width, int height)
{
_ship.SetPosition(x, y, width, height);
_ship?.SetPosition(x, y, width, height);
}
void IDrawingObject.DrawingObject(Graphics g)

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace ContainerShip
{
internal class DrawingShip
public class DrawingShip
{
public EntityShip Ship { get; protected set; }
protected float _startPosX;

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace ContainerShip
{
internal class EntityShip
public class EntityShip
{
public int Speed { get; private set; }
public float Weight { get; private set; }

View File

@ -1,93 +0,0 @@
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 ContainerShip
{
public partial class FormMap : Form
{
private AbstractMap _abstractMap;
public FormMap()
{
InitializeComponent();
_abstractMap = new SimpleMap();
}
private void SetData(DrawingShip ship)
{
Random rnd = new();
toolStripStatusSpeed.Text = $"Скорость: {ship.Ship.Speed}";
toolStripStatusWeight.Text = $"Вес: {ship.Ship.Weight}";
toolStripStatusBodyColor.Text = $"Цвет: {ship.Ship.BodyColor.Name}";
pictureBoxShip.Image = _abstractMap.CreateMap(pictureBoxShip.Width, pictureBoxShip.Height,
new DrawingObjectShip(ship));
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
var ship = new DrawingShip(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256),
rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
SetData(ship);
}
private void ButtonMove_Click(object sender, EventArgs e)
{
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
dir = Direction.Up;
break;
case "buttonDown":
dir = Direction.Down;
break;
case "buttonLeft":
dir = Direction.Left;
break;
case "buttonRight":
dir = Direction.Right;
break;
}
pictureBoxShip.Image = _abstractMap?.MoveObject(dir);
}
private void ButtonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new();
var ship = new DrawingContainerShip(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(255, rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(255, rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData(ship);
}
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxSelectorMap.Text)
{
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Острова":
_abstractMap = new IslandsMap();
break;
case "Скалы":
_abstractMap = new RocksMap();
break;
}
}
}
}

View File

@ -1,6 +1,6 @@
namespace ContainerShip
{
partial class FormMap
partial class FormMapWithSetShip
{
/// <summary>
/// Required designer variable.
@ -19,7 +19,7 @@
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
@ -28,131 +28,49 @@
/// </summary>
private void InitializeComponent()
{
this.pictureBoxShip = new System.Windows.Forms.PictureBox();
this.buttonCreate = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.statusStrip = new System.Windows.Forms.StatusStrip();
this.toolStripStatusSpeed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusBodyColor = new System.Windows.Forms.ToolStripStatusLabel();
this.buttonCreateModif = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit();
this.statusStrip.SuspendLayout();
this.buttonAddShip = new System.Windows.Forms.Button();
this.buttonRemoveShip = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// pictureBoxShip
// groupBox1
//
this.pictureBoxShip.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxShip.Location = new System.Drawing.Point(0, 0);
this.pictureBoxShip.Margin = new System.Windows.Forms.Padding(0);
this.pictureBoxShip.Name = "pictureBoxShip";
this.pictureBoxShip.Size = new System.Drawing.Size(800, 428);
this.pictureBoxShip.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxShip.TabIndex = 3;
this.pictureBoxShip.TabStop = false;
this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.comboBoxSelectorMap);
this.groupBox1.Controls.Add(this.buttonAddShip);
this.groupBox1.Controls.Add(this.buttonRemoveShip);
this.groupBox1.Controls.Add(this.buttonShowStorage);
this.groupBox1.Controls.Add(this.buttonShowOnMap);
this.groupBox1.Controls.Add(this.buttonDown);
this.groupBox1.Controls.Add(this.buttonLeft);
this.groupBox1.Controls.Add(this.buttonRight);
this.groupBox1.Controls.Add(this.buttonUp);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(600, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(200, 450);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
//
// buttonCreate
// maskedTextBoxPosition
//
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(12, 402);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(75, 23);
this.buttonCreate.TabIndex = 4;
this.buttonCreate.Text = "Создать";
this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.ButtonCreate_Click);
//
// buttonUp
//
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(722, 359);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 5;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(758, 395);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 6;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonLeft
//
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(686, 395);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 7;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonDown
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(722, 395);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 8;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// statusStrip
//
this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusSpeed,
this.toolStripStatusWeight,
this.toolStripStatusBodyColor});
this.statusStrip.Location = new System.Drawing.Point(0, 428);
this.statusStrip.Name = "statusStrip";
this.statusStrip.Size = new System.Drawing.Size(800, 22);
this.statusStrip.TabIndex = 9;
//
// toolStripStatusSpeed
//
this.toolStripStatusSpeed.Name = "toolStripStatusSpeed";
this.toolStripStatusSpeed.Size = new System.Drawing.Size(65, 17);
this.toolStripStatusSpeed.Text = "Скорость: ";
//
// toolStripStatusWeight
//
this.toolStripStatusWeight.Name = "toolStripStatusWeight";
this.toolStripStatusWeight.Size = new System.Drawing.Size(32, 17);
this.toolStripStatusWeight.Text = "Вес: ";
//
// toolStripStatusBodyColor
//
this.toolStripStatusBodyColor.Name = "toolStripStatusBodyColor";
this.toolStripStatusBodyColor.Size = new System.Drawing.Size(39, 17);
this.toolStripStatusBodyColor.Text = "Цвет: ";
//
// buttonCreateModif
//
this.buttonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateModif.Location = new System.Drawing.Point(94, 402);
this.buttonCreateModif.Name = "buttonCreateModif";
this.buttonCreateModif.Size = new System.Drawing.Size(110, 23);
this.buttonCreateModif.TabIndex = 10;
this.buttonCreateModif.Text = "Модификация";
this.buttonCreateModif.UseVisualStyleBackColor = true;
this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 185);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23);
this.maskedTextBoxPosition.TabIndex = 19;
//
// comboBoxSelectorMap
//
@ -162,48 +80,138 @@
"Простая карта",
"Острова",
"Скалы"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12);
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 22);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23);
this.comboBoxSelectorMap.TabIndex = 11;
this.comboBoxSelectorMap.Size = new System.Drawing.Size(188, 23);
this.comboBoxSelectorMap.TabIndex = 18;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// FormMap
// buttonAddShip
//
this.buttonAddShip.Location = new System.Drawing.Point(6, 135);
this.buttonAddShip.Name = "buttonAddShip";
this.buttonAddShip.Size = new System.Drawing.Size(188, 23);
this.buttonAddShip.TabIndex = 17;
this.buttonAddShip.Text = "Добавить корабль";
this.buttonAddShip.UseVisualStyleBackColor = true;
this.buttonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click);
//
// buttonRemoveShip
//
this.buttonRemoveShip.Location = new System.Drawing.Point(6, 214);
this.buttonRemoveShip.Name = "buttonRemoveShip";
this.buttonRemoveShip.Size = new System.Drawing.Size(188, 23);
this.buttonRemoveShip.TabIndex = 15;
this.buttonRemoveShip.Text = "Удалить корабль";
this.buttonRemoveShip.UseVisualStyleBackColor = true;
this.buttonRemoveShip.Click += new System.EventHandler(this.ButtonRemoveShip_Click);
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(6, 296);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(188, 23);
this.buttonShowStorage.TabIndex = 14;
this.buttonShowStorage.Text = "Посмотреть хранилище";
this.buttonShowStorage.UseVisualStyleBackColor = true;
this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(6, 325);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(188, 23);
this.buttonShowOnMap.TabIndex = 13;
this.buttonShowOnMap.Text = "Посмотреть карту";
this.buttonShowOnMap.UseVisualStyleBackColor = true;
this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
//
// buttonDown
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(89, 408);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 12;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonLeft
//
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(53, 408);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 11;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(125, 408);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 10;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonUp
//
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(89, 372);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 9;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(600, 450);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// FormMapWithSetShip
//
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.comboBoxSelectorMap);
this.Controls.Add(this.buttonCreateModif);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.pictureBoxShip);
this.Controls.Add(this.statusStrip);
this.Name = "FormMap";
this.Text = "FormMap";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).EndInit();
this.statusStrip.ResumeLayout(false);
this.statusStrip.PerformLayout();
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Name = "FormMapWithSetShip";
this.Text = "FormMapWithSetShip";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private PictureBox pictureBoxShip;
private Button buttonCreate;
private Button buttonUp;
private Button buttonRight;
private Button buttonLeft;
private GroupBox groupBox1;
private PictureBox pictureBox;
private Button buttonAddShip;
private Button buttonRemoveShip;
private Button buttonShowStorage;
private Button buttonShowOnMap;
private Button buttonDown;
private StatusStrip statusStrip;
private ToolStripStatusLabel toolStripStatusSpeed;
private ToolStripStatusLabel toolStripStatusWeight;
private ToolStripStatusLabel toolStripStatusBodyColor;
private Button buttonCreateModif;
private Button buttonLeft;
private Button buttonRight;
private Button buttonUp;
private ComboBox comboBoxSelectorMap;
private MaskedTextBox maskedTextBoxPosition;
}
}

View File

@ -0,0 +1,139 @@
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 ContainerShip
{
public partial class FormMapWithSetShip : Form
{
private MapWithSetShipGeneric<DrawingObjectShip, AbstractMap> _mapShipCollectionGeneric;
public FormMapWithSetShip()
{
InitializeComponent();
}
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Острова":
map = new IslandsMap();
break;
case "Скалы":
map = new RocksMap();
break;
}
if (map != null)
{
_mapShipCollectionGeneric = new
MapWithSetShipGeneric<DrawingObjectShip, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapShipCollectionGeneric = null;
}
}
private void ButtonAddShip_Click(object sender, EventArgs e)
{
if (_mapShipCollectionGeneric == null)
{
return;
}
FormShip form = new();
if (form.ShowDialog() == DialogResult.OK)
{
DrawingObjectShip ship = new(form.SelectedShip);
if (_mapShipCollectionGeneric + ship)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapShipCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void ButtonRemoveShip_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapShipCollectionGeneric - pos)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapShipCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapShipCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapShipCollectionGeneric.ShowSet();
}
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapShipCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapShipCollectionGeneric.ShowOnMap();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapShipCollectionGeneric == null)
{
return;
}
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
dir = Direction.Up;
break;
case "buttonDown":
dir = Direction.Down;
break;
case "buttonLeft":
dir = Direction.Left;
break;
case "buttonRight":
dir = Direction.Right;
break;
}
pictureBox.Image = _mapShipCollectionGeneric.MoveObject(dir);
}
}
}

View File

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

View File

@ -39,6 +39,7 @@
this.toolStripStatusWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusBodyColor = new System.Windows.Forms.ToolStripStatusLabel();
this.buttonCreateModif = new System.Windows.Forms.Button();
this.buttonSelectShip = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit();
this.statusStrip.SuspendLayout();
this.SuspendLayout();
@ -151,11 +152,22 @@
this.buttonCreateModif.UseVisualStyleBackColor = true;
this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click);
//
// buttonSelectShip
//
this.buttonSelectShip.Location = new System.Drawing.Point(605, 399);
this.buttonSelectShip.Name = "buttonSelectShip";
this.buttonSelectShip.Size = new System.Drawing.Size(75, 23);
this.buttonSelectShip.TabIndex = 11;
this.buttonSelectShip.Text = "Выбрать";
this.buttonSelectShip.UseVisualStyleBackColor = true;
this.buttonSelectShip.Click += new System.EventHandler(this.buttonSelectShip_Click);
//
// FormShip
//
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.buttonSelectShip);
this.Controls.Add(this.buttonCreateModif);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
@ -166,7 +178,6 @@
this.Controls.Add(this.statusStrip);
this.Name = "FormShip";
this.Text = "FormShip";
this.Load += new System.EventHandler(this.FormShip_Load);
this.Resize += new System.EventHandler(this.PictureBoxShip_Resize);
((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).EndInit();
this.statusStrip.ResumeLayout(false);
@ -189,5 +200,6 @@
private ToolStripStatusLabel toolStripStatusWeight;
private ToolStripStatusLabel toolStripStatusBodyColor;
private Button buttonCreateModif;
private Button buttonSelectShip;
}
}

View File

@ -14,6 +14,8 @@ namespace ContainerShip
{
private DrawingShip _ship;
public DrawingShip SelectedShip { get; private set; }
public FormShip()
{
InitializeComponent();
@ -84,9 +86,10 @@ namespace ContainerShip
Draw();
}
private void FormShip_Load(object sender, EventArgs e)
private void buttonSelectShip_Click(object sender, EventArgs e)
{
SelectedShip = _ship;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContainerShip
{
internal class MapWithSetShipGeneric<T, U>
where T : class, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 210;
private readonly int _placeSizeHeight = 90;
private readonly SetShipGeneric<T> _setShip;
private readonly U _map;
public MapWithSetShipGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setShip = new SetShipGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static bool operator +(MapWithSetShipGeneric<T, U> map, T car)
{
return map._setShip.Insert(car);
}
public static bool operator -(MapWithSetShipGeneric<T, U> map, int
position)
{
return map._setShip.Remove(position);
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawShip(gr);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setShip.Count; i++)
{
var car = _setShip.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
}
return new(_pictureWidth, _pictureHeight);
}
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
private void Shaking()
{
int j = _setShip.Count - 1;
for (int i = 0; i < _setShip.Count; i++)
{
if (_setShip.Get(i) == null)
{
for (; j > i; j--)
{
var car = _setShip.Get(j);
if (car != null)
{
_setShip.Insert(car, i);
_setShip.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
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 DrawShip(Graphics g)
{
int yNumOfPlaces = _pictureHeight / _placeSizeHeight;
int xNumOfPlaces = _pictureWidth / _placeSizeWidth;
int RowIndex = yNumOfPlaces - 1;
int ColumnIndex = 0;
for (int i = 0; i < _setShip.Count; i++)
{
if (_setShip.Get(i) != null)
{
(float Left, float Top, float Right, float Bottom) = _setShip.Get(i).GetCurrentPosition();
_setShip.Get(i).SetObject(ColumnIndex * _placeSizeWidth,
RowIndex * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top)),
_pictureWidth, _pictureHeight);
_setShip.Get(i).DrawingObject(g);
}
if (ColumnIndex == xNumOfPlaces - 1)
{
ColumnIndex = 0;
RowIndex--;
}
else
{
ColumnIndex++;
}
}
}
}
}

View File

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

View File

@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContainerShip
{
internal class SetShipGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
public SetShipGeneric(int count)
{
_places = new T[count];
}
public bool Insert(T ship)
{
return Insert(ship, 0);
}
public bool Insert(T ship, int position)
{
if (position < 0 || position > _places.Length)
{
return false;
}
int emptyCellIndex = -1;
if (_places[position] != null)
{
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null)
{
emptyCellIndex = i;
break;
}
}
if (emptyCellIndex != -1)
{
for (int i = emptyCellIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
}
}
else
{
_places[position] = ship;
return true;
}
if (emptyCellIndex == -1)
{
return false;
}
else
{
_places[position] = ship;
return true;
}
}
public bool Remove(int position)
{
if (position >= Count || position < 0)
{
return false;
}
T removedObject = _places[position];
_places[position] = null;
return true;
}
public T Get(int position)
{
if (position >= Count || position < 0)
{
return default(T);
}
return _places[position];
}
}
}