Compare commits

...

7 Commits

20 changed files with 1370 additions and 30 deletions

View File

@ -0,0 +1,145 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
internal abstract class AbstractMap
{
private IDrawningObject _drawningObject = null;
protected int[,] _map = null;
protected int _width;
protected int _height;
protected float _size_x;
protected float _size_y;
protected readonly Random _random = new();
protected readonly int _freeRoad = 0;
protected readonly int _barrier = 1;
public Bitmap CreateMap(int width, int height, IDrawningObject drawningObject)
{
_width = width;
_height = height;
_drawningObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public bool CheckAround(float Left, float Right, float Top, float Bottom)
{
int startX = (int)(Left / _size_x);
int startY = (int)(Right / _size_y);
int endX = (int)(Top / _size_x);
int endY = (int)(Bottom / _size_y);
for (int i = startX; i <= endX; i++)
{
for (int j = startY; j <= endY; j++)
{
if (_map[i, j] == _barrier)
{
return true;
}
}
}
return false;
}
public Bitmap MoveObject(Direction direction)
{
_drawningObject.MoveObject(direction);
(float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition();
if (CheckAround(Left, Right, Top, Bottom))
{
_drawningObject.MoveObject(MoveObjectBack(direction));
}
return DrawMapWithObject();
}
private Direction MoveObjectBack(Direction direction)
{
switch (direction)
{
case Direction.Up:
return Direction.Down;
case Direction.Down:
return Direction.Up;
case Direction.Left:
return Direction.Right;
case Direction.Right:
return Direction.Left;
}
return Direction.None;
}
private bool SetObjectOnMap()
{
if (_drawningObject == null || _map == null)
{
return false;
}
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawningObject.SetObject(x, y, _width, _height);
(float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition();
if (!CheckAround(Left, Right, Top, Bottom)) return true;
float startX = Left;
float startY = Right;
float lengthX = Top - Left;
float lengthY = Bottom - Right;
while (CheckAround(startX, startY, startX + lengthX, startY + lengthY))
{
bool result;
do
{
result = CheckAround(startX, startY, startX + lengthX, startY + lengthY);
if (!result)
{
_drawningObject.SetObject((int)startX, (int)startY, _width, _height);
return true;
}
else
{
startX += _size_x;
}
} while (result);
startX = x;
startY += _size_y;
}
return false;
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);
if (_drawningObject == null || _map == null)
{
return bmp;
}
Graphics gr = Graphics.FromImage(bmp);
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
if (_map[i, j] == _freeRoad)
{
DrawRoadPart(gr, i, j);
}
else if (_map[i, j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawningObject.DrawningObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}
}

View File

@ -6,8 +6,9 @@ using System.Threading.Tasks;
namespace ProjectLocomotive
{
internal enum Direction
public enum Direction
{
None = 0,
Up = 1,
Down = 2,
Left = 3,

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
internal class DrawningElectroLocomotive : DrawningLocomotive
{
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="electroLines">Признак наличия "рогов" для подключения</param>
/// <param name="electroBattery">Признак наличия отсека электро-батарей</param>
public DrawningElectroLocomotive(int speed, float weight, Color bodyColor, Color
dopColor, bool electroLines, bool electroBattery) :
base(speed, weight, bodyColor, 110, 60)
{
Locomotivе = new EntityElectricLocomotive(speed, weight, bodyColor, dopColor, electroLines,
electroBattery);
}
public override void DrawTransport(Graphics g)
{
if (Locomotivе is not EntityElectricLocomotive elLocc)
{
return;
}
Pen pen = new(Color.Black);
Brush dopBrush = new SolidBrush(elLocc.DopColor);
if (elLocc.ElectroLines)
{
g.DrawLine(pen, _startPosX + 20, _startPosY, _startPosX + 5, _startPosY - 12);
g.DrawLine(pen, _startPosX + 20, _startPosY, _startPosX + 35, _startPosY - 12);
g.DrawLine(pen, _startPosX + 70, _startPosY, _startPosX + 55, _startPosY - 12);
g.DrawLine(pen, _startPosX + 70, _startPosY, _startPosX + 85, _startPosY - 12);
}
base.DrawTransport(g);
if (elLocc.ElectroBattery)
{
Brush brblack = new SolidBrush(Color.Black);
g.FillRectangle(brblack, _startPosX + 40, _startPosY + 25, 15, 5);
g.FillRectangle(brblack, _startPosX + 60, _startPosY + 25, 15, 5);
g.FillRectangle(brblack, _startPosX + 5, _startPosY + 25, 15, 5);
}
}
}
}

View File

@ -9,20 +9,20 @@ namespace ProjectLocomotive
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
internal class DrawningLocomotive
public class DrawningLocomotive
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityLocomotive Locomotivе { private set; get; }
public EntityLocomotive Locomotivе { get; protected set; }
/// <summary>
/// Левая координата отрисовки локомотива
/// </summary>
private float _startPosX;
protected float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки локомотива
/// </summary>
private float _startPosY;
protected float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
@ -45,10 +45,24 @@ namespace ProjectLocomotive
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес локомотива</param>
/// <param name="bodyColor">Цвет кузова</param>
public void Init(int speed, float weight, Color bodyColor)
public DrawningLocomotive(int speed, float weight, Color bodyColor)
{
Locomotivе = new EntityLocomotive();
Locomotivе.Init(speed, weight, bodyColor);
Locomotivе = new EntityLocomotive(speed, weight, bodyColor);
}
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="carWidth">Ширина отрисовки автомобиля</param>
/// <param name="carHeight">Высота отрисовки автомобиля</param>
protected DrawningLocomotive(int speed, float weight, Color bodyColor, int
carWidth, int carHeight) :
this(speed, weight, bodyColor)
{
_LocWidth = carWidth;
_LocHeight = carHeight;
}
/// <summary>
/// Установка позиции автомобиля
@ -119,7 +133,7 @@ namespace ProjectLocomotive
/// Отрисовка автомобиля
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
@ -153,6 +167,8 @@ namespace ProjectLocomotive
g.DrawLine(pen, _startPosX + 30, _startPosY + 8, _startPosX + 30, _startPosY + 25);
g.DrawLine(pen, _startPosX + 30, _startPosY + 25, _startPosX + 20, _startPosY + 25);
g.DrawLine(pen, _startPosX + 20, _startPosY + 25, _startPosX + 20, _startPosY + 6);
}
/// <summary>
/// Смена границ формы отрисовки
@ -178,5 +194,9 @@ namespace ProjectLocomotive
_startPosY = _pictureHeight.Value - _LocHeight;
}
}
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _LocWidth, _startPosY + _LocHeight);
}
}
}

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
internal class DrawningObject : IDrawningObject
{
private DrawningLocomotive _loc = null;
public DrawningObject(DrawningLocomotive loc)
{
_loc = loc;
}
public float Step => _loc?.Locomotivе?.Step ?? 0;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _loc?.GetCurrentPosition() ?? default;
}
public void MoveObject(Direction direction)
{
_loc?.MoveTransport(direction);
}
public void SetObject(int x, int y, int width, int height)
{
_loc.SetPosition(x, y, width, height);
}
void IDrawningObject.DrawningObject(Graphics g)
{
// TODO
_loc.DrawTransport(g);
}
}
}

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
internal class EntityElectricLocomotive : EntityLocomotive
{
/// <summary>
/// Дополнительный цвет
/// </summary>
public Color DopColor { get; private set; }
/// <summary>
/// Признак наличия "рогов" для подключения
/// </summary>
public bool ElectroLines { get; private set; }
/// <summary>
/// Признак наличия отсека электро-батарей
/// </summary>
public bool ElectroBattery { get; private set; }
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="electroLines">Признак наличия "рогов" для подключения</param>
/// <param name="electroBattery">Признак наличия отсека электро-батарей</param>
public EntityElectricLocomotive(int speed, float weight, Color bodyColor, Color
dopColor, bool electroLines, bool electroBattery) :
base(speed, weight, bodyColor)
{
DopColor = dopColor;
ElectroLines = electroLines = true;
ElectroBattery = electroBattery = true;
}
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace ProjectLocomotive
{
internal class EntityLocomotive
public class EntityLocomotive
{
/// <summary>
/// Скорость
@ -31,7 +31,7 @@ namespace ProjectLocomotive
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <returns></returns>
public void Init(int speed, float weight, Color bodyColor)
public EntityLocomotive(int speed, float weight, Color bodyColor)
{
Random rnd = new();
Speed = speed <= 0 ? rnd.Next(50, 150) : speed;

View File

@ -38,6 +38,8 @@
this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel();
this.statusStrip = new System.Windows.Forms.StatusStrip();
this.buttonCreateModif = new System.Windows.Forms.Button();
this.buttonSelectLocomotive = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotive)).BeginInit();
this.statusStrip.SuspendLayout();
this.SuspendLayout();
@ -88,19 +90,6 @@
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
this.buttonLeft.Resize += new System.EventHandler(this.PictureBoxLocomotive_Resize);
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ProjectLocomotive.Properties.Resources.right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(711, 375);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 5;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
this.buttonRight.Resize += new System.EventHandler(this.PictureBoxLocomotive_Resize);
//
// buttonDown
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
@ -114,6 +103,19 @@
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
this.buttonDown.Resize += new System.EventHandler(this.PictureBoxLocomotive_Resize);
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ProjectLocomotive.Properties.Resources.right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(711, 375);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 5;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
this.buttonRight.Resize += new System.EventHandler(this.PictureBoxLocomotive_Resize);
//
// toolStripStatusLabelSpeed
//
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
@ -144,11 +146,33 @@
this.statusStrip.Size = new System.Drawing.Size(800, 32);
this.statusStrip.TabIndex = 1;
//
// buttonCreateModif
//
this.buttonCreateModif.Location = new System.Drawing.Point(120, 375);
this.buttonCreateModif.Name = "buttonCreateModif";
this.buttonCreateModif.Size = new System.Drawing.Size(138, 30);
this.buttonCreateModif.TabIndex = 7;
this.buttonCreateModif.Text = "Модификация";
this.buttonCreateModif.UseVisualStyleBackColor = true;
this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click);
//
// buttonSelectLocomotive
//
this.buttonSelectLocomotive.Location = new System.Drawing.Point(495, 370);
this.buttonSelectLocomotive.Name = "buttonSelectLocomotive";
this.buttonSelectLocomotive.Size = new System.Drawing.Size(104, 36);
this.buttonSelectLocomotive.TabIndex = 8;
this.buttonSelectLocomotive.Text = "Выбрать";
this.buttonSelectLocomotive.UseVisualStyleBackColor = true;
this.buttonSelectLocomotive.Click += new System.EventHandler(this.buttonSelectLocomotive_Click);
//
// FormLocomotive
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.buttonSelectLocomotive);
this.Controls.Add(this.buttonCreateModif);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
@ -178,5 +202,7 @@
private ToolStripStatusLabel toolStripStatusLabelWeight;
private ToolStripStatusLabel toolStripStatusLabelBodyColor;
private StatusStrip statusStrip;
private Button buttonCreateModif;
private Button buttonSelectLocomotive;
}
}

View File

@ -3,6 +3,10 @@ namespace ProjectLocomotive
public partial class FormLocomotive : Form
{
private DrawningLocomotive _elloc;
/// <summary>
/// Âûáðàííûé îáúåêò
/// </summary>
public DrawningLocomotive SelectedLocomotive { get; private set; }
public FormLocomotive()
{
@ -20,6 +24,17 @@ namespace ProjectLocomotive
pictureBoxLocomotive.Image = bmp;
}
/// <summary>
/// Ìåòîä óñòàíîâêè äàííûõ
/// </summary>
private void SetData()
{
Random rnd = new();
_elloc.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxLocomotive.Width, pictureBoxLocomotive.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_elloc.Locomotivå.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_elloc.Locomotivå.Weight}";
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_elloc.Locomotivå.BodyColor.Name}";
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
/// </summary>
/// <param name="sender"></param>
@ -28,12 +43,16 @@ namespace ProjectLocomotive
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_elloc = new DrawningLocomotive();
_elloc.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
//_elloc = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
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;
}
_elloc = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), color);
_elloc.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxLocomotive.Width, pictureBoxLocomotive.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_elloc.Locomotivå.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_elloc.Locomotivå.Weight}";
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_elloc.Locomotivå.BodyColor.Name}";
SetData();
Draw();
}
/// <summary>
@ -72,5 +91,45 @@ namespace ProjectLocomotive
_elloc?.ChangeBorders(pictureBoxLocomotive.Width, pictureBoxLocomotive.Height);
Draw();
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ìîäèôèêàöèÿ"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateModif_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;
}
Color dopColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialogDop = new();
if (dialogDop.ShowDialog() == DialogResult.OK)
{
dopColor = dialogDop.Color;
}
_elloc = new DrawningElectroLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000),
color,
dopColor,
//Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
//Color.FromArgb(rnd.Next(1, 100), rnd.Next(1, 100), rnd.Next(1, 100)),
Convert.ToBoolean(rnd.Next(0, 1)), Convert.ToBoolean(rnd.Next(0, 1)));
SetData();
Draw();
}
private void pictureBoxLocomotive_Click(object sender, EventArgs e)
{
}
private void buttonSelectLocomotive_Click(object sender, EventArgs e)
{
SelectedLocomotive = _elloc;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -0,0 +1,226 @@
namespace ProjectLocomotive
{
partial class FormMapWithSetLocomotives
{
/// <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.groupBoxTools = new System.Windows.Forms.GroupBox();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonRemoveLocomotive = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddLocomotive = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBoxTools.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.buttonLeft);
this.groupBoxTools.Controls.Add(this.buttonRight);
this.groupBoxTools.Controls.Add(this.buttonDown);
this.groupBoxTools.Controls.Add(this.buttonUp);
this.groupBoxTools.Controls.Add(this.buttonShowOnMap);
this.groupBoxTools.Controls.Add(this.buttonShowStorage);
this.groupBoxTools.Controls.Add(this.buttonRemoveLocomotive);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonAddLocomotive);
this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(725, 0);
this.groupBoxTools.Margin = new System.Windows.Forms.Padding(4);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Padding = new System.Windows.Forms.Padding(4);
this.groupBoxTools.Size = new System.Drawing.Size(275, 630);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
//
// buttonLeft
//
this.buttonLeft.BackgroundImage = global::ProjectLocomotive.Properties.Resources.left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(52, 558);
this.buttonLeft.Margin = new System.Windows.Forms.Padding(4);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(50, 50);
this.buttonLeft.TabIndex = 7;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonRight
//
this.buttonRight.BackgroundImage = global::ProjectLocomotive.Properties.Resources.right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(168, 558);
this.buttonRight.Margin = new System.Windows.Forms.Padding(4);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(50, 50);
this.buttonRight.TabIndex = 7;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonDown
//
this.buttonDown.BackgroundImage = global::ProjectLocomotive.Properties.Resources.down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(110, 558);
this.buttonDown.Margin = new System.Windows.Forms.Padding(4);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(50, 50);
this.buttonDown.TabIndex = 7;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonUp
//
this.buttonUp.BackgroundImage = global::ProjectLocomotive.Properties.Resources.up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(110, 500);
this.buttonUp.Margin = new System.Windows.Forms.Padding(4);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(50, 50);
this.buttonUp.TabIndex = 6;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(9, 352);
this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(4);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(259, 36);
this.buttonShowOnMap.TabIndex = 5;
this.buttonShowOnMap.Text = "Посмотреть карту";
this.buttonShowOnMap.UseVisualStyleBackColor = true;
this.buttonShowOnMap.Click += new System.EventHandler(this.buttonShowOnMap_Click);
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(9, 309);
this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(4);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(259, 36);
this.buttonShowStorage.TabIndex = 4;
this.buttonShowStorage.Text = "Посмотреть хранилище";
this.buttonShowStorage.UseVisualStyleBackColor = true;
this.buttonShowStorage.Click += new System.EventHandler(this.buttonShowStorage_Click);
//
// buttonRemoveLocomotive
//
this.buttonRemoveLocomotive.Location = new System.Drawing.Point(9, 264);
this.buttonRemoveLocomotive.Margin = new System.Windows.Forms.Padding(4);
this.buttonRemoveLocomotive.Name = "buttonRemoveLocomotive";
this.buttonRemoveLocomotive.Size = new System.Drawing.Size(259, 38);
this.buttonRemoveLocomotive.TabIndex = 3;
this.buttonRemoveLocomotive.Text = "Удалить локомотив";
this.buttonRemoveLocomotive.UseVisualStyleBackColor = true;
this.buttonRemoveLocomotive.Click += new System.EventHandler(this.buttonRemoveLocomotive_Click);
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(8, 199);
this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(4);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(259, 31);
this.maskedTextBoxPosition.TabIndex = 2;
//
// buttonAddLocomotive
//
this.buttonAddLocomotive.Location = new System.Drawing.Point(8, 120);
this.buttonAddLocomotive.Margin = new System.Windows.Forms.Padding(4);
this.buttonAddLocomotive.Name = "buttonAddLocomotive";
this.buttonAddLocomotive.Size = new System.Drawing.Size(260, 36);
this.buttonAddLocomotive.TabIndex = 1;
this.buttonAddLocomotive.Text = "Добавить локомотив";
this.buttonAddLocomotive.UseVisualStyleBackColor = true;
this.buttonAddLocomotive.Click += new System.EventHandler(this.buttonAddLocomotive_Click);
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Simple Map",
"Spike Map",
"Rail Map"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(8, 49);
this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(4);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(259, 33);
this.comboBoxSelectorMap.TabIndex = 0;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.comboBoxSelectorMap_SelectedIndexChanged);
//
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Margin = new System.Windows.Forms.Padding(4);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(725, 630);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// FormMapWithSetLocomotives
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1000, 630);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "FormMapWithSetLocomotives";
this.Text = "Карта с набором объектов";
this.groupBoxTools.ResumeLayout(false);
this.groupBoxTools.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
private GroupBox groupBoxTools;
private PictureBox pictureBox;
private ComboBox comboBoxSelectorMap;
private Button buttonAddLocomotive;
private MaskedTextBox maskedTextBoxPosition;
private Button buttonRemoveLocomotive;
private Button buttonShowStorage;
private Button buttonShowOnMap;
private Button buttonUp;
private Button buttonDown;
private Button buttonRight;
private Button buttonLeft;
}
}

View File

@ -0,0 +1,137 @@
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 ProjectLocomotive
{
public partial class FormMapWithSetLocomotives : Form
{
/// Объект от класса карты с набором объектов
private MapWithSetLocomotivesGeneric<DrawningObject, AbstractMap> _mapLocomotivesCollectionGeneric;
public FormMapWithSetLocomotives()
{
InitializeComponent();
}
/// Выбор карты
private void comboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
{
case "Simple Map":
map = new SimpleMap();
break;
case "Spike Map":
map = new SpikeMap();
break;
case "Rail Map":
map = new RailroadMap();
break;
}
if (map != null)
{
_mapLocomotivesCollectionGeneric = new MapWithSetLocomotivesGeneric<DrawningObject, AbstractMap>
(pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapLocomotivesCollectionGeneric = null;
}
}
/// Добавление объекта
private void buttonAddLocomotive_Click(object sender, EventArgs e)
{
if (_mapLocomotivesCollectionGeneric == null)
{
return;
}
FormLocomotive form = new();
if (form.ShowDialog() == DialogResult.OK)
{
DrawningObject locomotive = new(form.SelectedLocomotive);
if (_mapLocomotivesCollectionGeneric + locomotive != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapLocomotivesCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
/// Удаление объекта
private void buttonRemoveLocomotive_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 (_mapLocomotivesCollectionGeneric - pos is not null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapLocomotivesCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// Вывод набора
private void buttonShowStorage_Click(object sender, EventArgs e)
{
if (_mapLocomotivesCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapLocomotivesCollectionGeneric.ShowSet();
}
/// Вывод карты
private void buttonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapLocomotivesCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapLocomotivesCollectionGeneric.ShowOnMap();
}
/// Перемещение
private void buttonMove_Click(object sender, EventArgs e)
{
if (_mapLocomotivesCollectionGeneric == 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 = _mapLocomotivesCollectionGeneric.MoveObject(dir);
}
}
}

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,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
/// <summary>
/// Интерфейс для работы с объектом, прорисовываемым на форме
/// </summary>
internal interface IDrawningObject
{
/// <summary>
/// Шаг перемещения объекта
/// </summary>
public float Step { get; }
/// <summary>
/// Установка позиции объекта
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина полотна</param>
/// <param name="height">Высота полотна</param>
void SetObject(int x, int y, int width, int height);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
/// <returns></returns>
void MoveObject(Direction direction);
/// <summary>
/// Отрисовка объекта
/// </summary>
/// <param name="g"></param>
void DrawningObject(Graphics g);
/// <summary>
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
}
}

View File

@ -0,0 +1,175 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
internal class MapWithSetLocomotivesGeneric<T, U>
where T : class, IDrawningObject
where U : AbstractMap
{
/// Ширина окна отрисовки
private readonly int _pictureWidth;
/// Высота окна отрисовки
private readonly int _pictureHeight;
/// Размер занимаемого объектом места (ширина)
private readonly int _placeSizeWidth = 210;
/// Размер занимаемого объектом места (высота)
private readonly int _placeSizeHeight = 90;
/// Набор объектов
private readonly SetLocomotivesGeneric<T> _setLocomotives;
/// Карта
private readonly U _map;
/// Конструктор
public MapWithSetLocomotivesGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setLocomotives = new SetLocomotivesGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
/// Перегрузка оператора сложения
public static int operator +(MapWithSetLocomotivesGeneric<T, U> map, T locomotive)
{
return map._setLocomotives.Insert(locomotive);
}
/// Перегрузка оператора вычитания
public static T operator -(MapWithSetLocomotivesGeneric<T, U> map, int position)
{
return map._setLocomotives.Remove(position);
}
/// Вывод всего набора объектов
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawLocomotives(gr);
return bmp;
}
/// Просмотр объекта на карте
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setLocomotives.Count; i++)
{
var locomotive = _setLocomotives.Get(i);
if (locomotive != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
}
}
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 = _setLocomotives.Count - 1;
for (int i = 0; i < _setLocomotives.Count; i++)
{
if (_setLocomotives.Get(i) == null)
{
for (; j > i; j--)
{
var locomotive = _setLocomotives.Get(j);
if (locomotive != null)
{
_setLocomotives.Insert(locomotive, i);
_setLocomotives.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);
//}
Pen pen;
for (int j = _placeSizeHeight; j < _pictureHeight; j += _placeSizeHeight)
{
//нижняя линия рельс
pen = new(Color.Black, 5);
g.DrawLine(pen, 0, j, _pictureWidth, j);
for (int i = 0; i < _pictureWidth; i += 20)
{
g.DrawLine(pen, i, j, i, j + 10);
}
g.DrawLine(pen, 0, j + 10, _pictureWidth, j + 10);
//верхняя линия рельс
pen = new(Color.DarkGray, 4);
g.DrawLine(pen, 0, j - 20, _pictureWidth, j - 20);
for (int i = 0; i < _pictureWidth; i += 20)
{
g.DrawLine(pen, i, j - 20, i, j - 10);
}
g.DrawLine(pen, 0, j - 10, _pictureWidth, j - 10);
//фонари
for (int i = _placeSizeWidth; i < _pictureWidth; i += _placeSizeWidth)
{
pen = new(Color.Black, 10);
g.DrawLine(pen, i, j - _placeSizeHeight + 20, i, j);
pen = new(Color.Yellow, 20);
g.DrawLine(pen, i, j - _placeSizeHeight + 18, i, j - _placeSizeHeight + 38);
}
}
}
/// Метод прорисовки объектов
private void DrawLocomotives(Graphics g)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int curWidth = 0;
int curHeight = 0;
for (int i = 0; i < _setLocomotives.Count; i++)
{
// установка позиции
//_setLocomotives.Get(i)?.SetObject(curWidth * _placeSizeWidth, curHeight * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setLocomotives.Get(i)?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 470, _pictureWidth, _pictureHeight);
_setLocomotives.Get(i)?.DrawningObject(g);
if (curWidth < width) curWidth++;
else
{
curWidth = 0;
curHeight--;
}
}
}
}
}

View File

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

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
internal class RailroadMap : AbstractMap
{
/// Цвет участка закрытого
private readonly Brush barrierColor = new SolidBrush(Color.Black);
/// Цвет участка открытого
private readonly Brush roadColor = new SolidBrush(Color.Gray);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x +
1), j * (_size_y + 1));
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x +
1), j * (_size_y + 1));
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int counter = 0;
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}
while (counter < 1)
{
int y = _random.Next(0, 95);
for (int x = 0; x < 99; x++)
{
_map[x, y] = _barrier;
_map[x, y + 5] = _barrier;
if (x % 5 == 0)
{
_map[x, y + 1] = _barrier;
_map[x, y + 2] = _barrier;
_map[x, y + 3] = _barrier;
_map[x, y + 4] = _barrier;
}
}
counter += 1;
}
}
}
}

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
internal class SeaMap : AbstractMap
{
private readonly Brush barrierColor = new SolidBrush(Color.White);
private readonly Brush roadColor = new SolidBrush(Color.Blue);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int counter = 0;
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}
while (counter < 25)
{
int x = _random.Next(0, 97);
int y = _random.Next(0, 97);
if (_map[x, y] == _freeRoad)
{
_map[x, y + 1] = _barrier;
_map[x + 1, y + 1] = _barrier;
_map[x + 2, y + 1] = _barrier;
_map[x + 1, y] = _barrier;
counter++;
}
}
}
}
}

View File

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetLocomotivesGeneric<T>
where T : class
{
private readonly T[] _places;
/// Количество объектов в массиве
public int Count => _places.Length;
/// Конструктор
public SetLocomotivesGeneric(int count)
{
_places = new T[count];
}
/// Добавление объекта в набор
public int Insert(T locomotive)
{
return Insert(locomotive, 0);
}
/// Добавление объекта в набор на конкретную позицию
public int Insert(T locomotive, int position)
{
if (position >= _places.Length || position < 0) return -1;
if (_places[position] == null)
{
_places[position] = locomotive;
return position;
}
int emptyEl = -1;
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null)
{
emptyEl = i;
break;
}
}
if (emptyEl == -1)
{
return -1;
}
for (int i = emptyEl; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = locomotive;
return position;
}
/// Удаление объекта из набора с конкретной позиции
public T Remove(int position)
{
if (position >= _places.Length || position < 0) return null;
_places[position] = null;
T result = _places[position];
return result;
}
/// Получение объекта из набора по позиции
public T Get(int position)
{
if (position >= _places.Length || position < 0)
{
return null;
}
return _places[position];
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
/// <summary>
/// Простая реализация абсрактного класса AbstractMap
/// </summary>
internal class SimpleMap : AbstractMap
{
/// <summary>
/// Цвет участка закрытого
/// </summary>
private readonly Brush barrierColor = new SolidBrush(Color.Black);
/// <summary>
/// Цвет участка открытого
/// </summary>
private readonly Brush roadColor = new SolidBrush(Color.Gray);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int counter = 0;
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}
while (counter < 50)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
internal class SpikeMap : AbstractMap
{
/// Цвет участка закрытого
private readonly Brush barrierColor = new SolidBrush(Color.Black);
/// Цвет участка открытого
private readonly Brush roadColor = new SolidBrush(Color.Gray);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x +
1), j * (_size_y + 1));
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x +
1), j * (_size_y + 1));
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int counter = 0;
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}
while (counter < 15)
{
int x = _random.Next(1, 99);
int y = _random.Next(1, 99);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
if (_map[x + 1, y] == _freeRoad) _map[x + 1, y] = _barrier;
if (_map[x - 1, y] == _freeRoad) _map[x - 1, y] = _barrier;
if (_map[x, y + 1] == _freeRoad) _map[x, y + 1] = _barrier;
if (_map[x, y - 1] == _freeRoad) _map[x, y - 1] = _barrier;
counter++;
}
}
}
}
}