Compare commits

..

1 Commits

18 changed files with 46 additions and 1099 deletions

View File

@ -1,151 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal abstract class AbstractMap
{
private IDrawingObject _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;
private Direction GetOpositDirection(Direction d)
{
switch (d)
{
case Direction.None:
return Direction.None;
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 int CheckCollision(float Left, float Right, float Top, float Bottom)
{
int starCoordinateX = (int)(Left / _size_x);
int starCoordinateY = (int)(Right / _size_y);
int endCoordinateX = (int)(Top / _size_x);
int endCoordinateY = (int)(Bottom / _size_y);
if (starCoordinateX < 0 || starCoordinateY < 0 || endCoordinateX >= _map.GetLength(1) || endCoordinateY >= _map.GetLength(0))
{
return 2;
}
for (int x = starCoordinateX; x <= endCoordinateX; x++)
{
for (int y = starCoordinateY; y <= endCoordinateY; y++)
{
if (_map[x, y] == _barrier)
{
return 1;
}
}
}
return 0;
}
public Bitmap CreateMap(int width, int height, IDrawingObject drawningObject)
{
_width = width;
_height = height;
_drawningObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Bitmap MoveObject(Direction direction)
{
if (true)
{
_drawningObject.MoveObject(direction);
}
(float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition();
if (CheckCollision(Left, Right, Top, Bottom) != 0)
{
_drawningObject.MoveObject(GetOpositDirection(direction));
}
return DrawMapWithObject();
}
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();
while (CheckCollision(Left, Right, Top, Bottom) != 2)
{
int resoult;
do
{
resoult = CheckCollision(Left, Right, Top, Bottom);
if (resoult == 0)
{
_drawningObject.SetObject((int)Left, (int)Right, _width, _height);
return true;
}
else
{
Left += _size_x;
}
} while (resoult != 2);
Left = x;
Right += _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,9 +6,8 @@ using System.Threading.Tasks;
namespace GasolineTanker namespace GasolineTanker
{ {
public enum Direction internal enum Direction
{ {
None = 0,
Up = 1, Up = 1,
Down = 2, Down = 2,
Left = 3, Left = 3,

View File

@ -6,26 +6,20 @@ using System.Threading.Tasks;
namespace GasolineTanker namespace GasolineTanker
{ {
public class DrawingGasolineTanker internal class DrawingGasolineTanker
{ {
public EnityGasolineTanker GasolineTanker { get; protected set; } public EnityGasolineTanker GasolineTanker { get; private set; }
protected float _startPosX; private float _startPosX;
protected float _startPosY; private float _startPosY;
private int? _pictureWidth = null; private int? _pictureWidth = null;
private int? _pictureHeight = null; private int? _pictureHeight = null;
private readonly int _gasolineTankerWidth = 160; private readonly int _gasolineTankerWidth = 160;
private readonly int _gasolineTankerHeight = 55; private readonly int _gasolineTankerHeight = 55;
public DrawingGasolineTanker(int speed, float weight, Color bodyColor) public void Init(int speed, float weight, Color bodyColor)
{ {
GasolineTanker = new EnityGasolineTanker(speed, weight, bodyColor); GasolineTanker = new EnityGasolineTanker();
} GasolineTanker.Init(speed, weight, bodyColor);
protected DrawingGasolineTanker(int speed, float weight, Color bodyColor, int gasolineTankerWidth, int gasolineTankerHeight) :
this(speed, weight, bodyColor)
{
_gasolineTankerWidth = gasolineTankerWidth;
_gasolineTankerHeight = gasolineTankerHeight;
} }
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y, int width, int height)
@ -77,7 +71,7 @@ namespace GasolineTanker
break; break;
} }
} }
public virtual void DrawTransport(Graphics g) public void DrawTransport(Graphics g)
{ {
if (_startPosX < 0 || _startPosY < 0 if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue) || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
@ -127,10 +121,5 @@ namespace GasolineTanker
_startPosY = _pictureHeight.Value - _gasolineTankerHeight; _startPosY = _pictureHeight.Value - _gasolineTankerHeight;
} }
} }
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _gasolineTankerWidth, _startPosY + _gasolineTankerHeight);
}
} }
} }

View File

@ -1,47 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class DrawingImprovedGasolineTanker : DrawingGasolineTanker
{
public DrawingImprovedGasolineTanker(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool ornamentWheels) :
base(speed, weight, bodyColor, 160, 55)
{
GasolineTanker = new EntityImprovedGasolineTanker(speed, weight, bodyColor, dopColor, bodyKit, ornamentWheels);
}
public override void DrawTransport(Graphics g)
{
if (GasolineTanker is not EntityImprovedGasolineTanker improvedGasolineTanker)
{
return;
}
if (improvedGasolineTanker.BodyKit)
{
Brush dopBrush = new SolidBrush(improvedGasolineTanker.DopColor);
g.FillRectangle(dopBrush, _startPosX + 25, _startPosY + 5, 100, 35);
Brush brRed = new SolidBrush(Color.Red);
g.FillRectangle(brRed, _startPosX + 80, _startPosY, 10, 5);
}
_startPosX += 10;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
if (improvedGasolineTanker.OrnamentWheels)
{
Pen penGray = new(Color.Gray);
Brush brBlack = new SolidBrush(Color.Gray);
g.DrawEllipse(penGray, _startPosX + 140, _startPosY + 50, 20, 5);
g.DrawEllipse(penGray, _startPosX + 40, _startPosY + 50, 20, 5);
g.DrawEllipse(penGray, _startPosX + 20, _startPosY + 50, 20, 5);
}
}
}
}

View File

@ -1,40 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class DrawingObjectGasolineTanker : IDrawingObject
{
private DrawingGasolineTanker _gasolineTanker = null;
public DrawingObjectGasolineTanker(DrawingGasolineTanker gasolineTanker)
{
_gasolineTanker = gasolineTanker;
}
public float Step => _gasolineTanker?.GasolineTanker?.Step ?? 0;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _gasolineTanker?.GetCurrentPosition() ?? default;
}
public void MoveObject(Direction direction)
{
_gasolineTanker?.MoveTransport(direction);
}
public void SetObject(int x, int y, int width, int height)
{
_gasolineTanker.SetPosition(x, y, width, height);
}
void IDrawingObject.DrawningObject(Graphics g)
{
_gasolineTanker.DrawTransport(g);
}
}
}

View File

@ -6,13 +6,13 @@ using System.Threading.Tasks;
namespace GasolineTanker namespace GasolineTanker
{ {
public class EnityGasolineTanker internal class EnityGasolineTanker
{ {
public int Speed { get; private set; } public int Speed { get; private set; }
public float Weight { get; private set; } public float Weight { get; private set; }
public Color BodyColor { get; private set; } public Color BodyColor { get; private set; }
public float Step => Speed * 100 / Weight; public float Step => Speed * 100 / Weight;
public EnityGasolineTanker(int speed, float weight, Color bodyColor) public void Init(int speed, float weight, Color bodyColor)
{ {
Random rnd = new Random(); Random rnd = new Random();
Speed = speed <= 0 ? rnd.Next(50, 100) : speed; Speed = speed <= 0 ? rnd.Next(50, 100) : speed;

View File

@ -1,22 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class EntityImprovedGasolineTanker : EnityGasolineTanker
{
public Color DopColor { get; private set; }
public bool BodyKit { get; private set; }
public bool OrnamentWheels { get; private set; }
public EntityImprovedGasolineTanker(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool ornamentWheels) :
base(speed, weight, bodyColor)
{
DopColor = dopColor;
BodyKit = bodyKit;
OrnamentWheels = ornamentWheels;
}
}
}

View File

@ -38,8 +38,6 @@
this.keyUp = new System.Windows.Forms.Button(); this.keyUp = new System.Windows.Forms.Button();
this.keyLeft = new System.Windows.Forms.Button(); this.keyLeft = new System.Windows.Forms.Button();
this.keyRight = new System.Windows.Forms.Button(); this.keyRight = new System.Windows.Forms.Button();
this.ButtonCreateImproved = new System.Windows.Forms.Button();
this.buttonChoose = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).BeginInit();
this.statusStrip1.SuspendLayout(); this.statusStrip1.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
@ -48,8 +46,9 @@
// //
this.pictureBoxGasolineTanker.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxGasolineTanker.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxGasolineTanker.Location = new System.Drawing.Point(0, 0); this.pictureBoxGasolineTanker.Location = new System.Drawing.Point(0, 0);
this.pictureBoxGasolineTanker.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.pictureBoxGasolineTanker.Name = "pictureBoxGasolineTanker"; this.pictureBoxGasolineTanker.Name = "pictureBoxGasolineTanker";
this.pictureBoxGasolineTanker.Size = new System.Drawing.Size(807, 428); this.pictureBoxGasolineTanker.Size = new System.Drawing.Size(922, 574);
this.pictureBoxGasolineTanker.TabIndex = 0; this.pictureBoxGasolineTanker.TabIndex = 0;
this.pictureBoxGasolineTanker.TabStop = false; this.pictureBoxGasolineTanker.TabStop = false;
// //
@ -60,49 +59,52 @@
this.toolStripStatusSpeed, this.toolStripStatusSpeed,
this.toolStripStatusWeight, this.toolStripStatusWeight,
this.toolStripStatusBodyColor}); this.toolStripStatusBodyColor});
this.statusStrip1.Location = new System.Drawing.Point(0, 428); this.statusStrip1.Location = new System.Drawing.Point(0, 574);
this.statusStrip1.Name = "statusStrip1"; this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(807, 22); this.statusStrip1.Padding = new System.Windows.Forms.Padding(1, 0, 16, 0);
this.statusStrip1.Size = new System.Drawing.Size(922, 26);
this.statusStrip1.TabIndex = 1; this.statusStrip1.TabIndex = 1;
this.statusStrip1.Text = "statusStrip1"; this.statusStrip1.Text = "statusStrip1";
// //
// toolStripStatusSpeed // toolStripStatusSpeed
// //
this.toolStripStatusSpeed.Name = "toolStripStatusSpeed"; this.toolStripStatusSpeed.Name = "toolStripStatusSpeed";
this.toolStripStatusSpeed.Size = new System.Drawing.Size(39, 17); this.toolStripStatusSpeed.Size = new System.Drawing.Size(51, 20);
this.toolStripStatusSpeed.Text = "Speed"; this.toolStripStatusSpeed.Text = "Speed";
// //
// toolStripStatusWeight // toolStripStatusWeight
// //
this.toolStripStatusWeight.Name = "toolStripStatusWeight"; this.toolStripStatusWeight.Name = "toolStripStatusWeight";
this.toolStripStatusWeight.Size = new System.Drawing.Size(45, 17); this.toolStripStatusWeight.Size = new System.Drawing.Size(56, 20);
this.toolStripStatusWeight.Text = "Weight"; this.toolStripStatusWeight.Text = "Weight";
// //
// toolStripStatusBodyColor // toolStripStatusBodyColor
// //
this.toolStripStatusBodyColor.Name = "toolStripStatusBodyColor"; this.toolStripStatusBodyColor.Name = "toolStripStatusBodyColor";
this.toolStripStatusBodyColor.Size = new System.Drawing.Size(36, 17); this.toolStripStatusBodyColor.Size = new System.Drawing.Size(45, 20);
this.toolStripStatusBodyColor.Text = "Color"; this.toolStripStatusBodyColor.Text = "Color";
// //
// buttonCreate // buttonCreate
// //
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 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, 393); this.buttonCreate.Location = new System.Drawing.Point(14, 524);
this.buttonCreate.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonCreate.Name = "buttonCreate"; this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(75, 23); this.buttonCreate.Size = new System.Drawing.Size(86, 31);
this.buttonCreate.TabIndex = 2; this.buttonCreate.TabIndex = 2;
this.buttonCreate.Text = "Create"; this.buttonCreate.Text = "Create";
this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.ButtonCreate_Click_1); this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click_1);
// //
// keyDown // keyDown
// //
this.keyDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.keyDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyDown.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyDown; this.keyDown.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyDown;
this.keyDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.keyDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyDown.Location = new System.Drawing.Point(724, 386); this.keyDown.Location = new System.Drawing.Point(827, 515);
this.keyDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.keyDown.Name = "keyDown"; this.keyDown.Name = "keyDown";
this.keyDown.Size = new System.Drawing.Size(30, 30); this.keyDown.Size = new System.Drawing.Size(34, 40);
this.keyDown.TabIndex = 3; this.keyDown.TabIndex = 3;
this.keyDown.UseVisualStyleBackColor = true; this.keyDown.UseVisualStyleBackColor = true;
this.keyDown.Click += new System.EventHandler(this.ButtonMove_Click); this.keyDown.Click += new System.EventHandler(this.ButtonMove_Click);
@ -112,9 +114,10 @@
this.keyUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.keyUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyUp.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyUp; this.keyUp.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyUp;
this.keyUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.keyUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyUp.Location = new System.Drawing.Point(724, 350); this.keyUp.Location = new System.Drawing.Point(827, 467);
this.keyUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.keyUp.Name = "keyUp"; this.keyUp.Name = "keyUp";
this.keyUp.Size = new System.Drawing.Size(30, 30); this.keyUp.Size = new System.Drawing.Size(34, 40);
this.keyUp.TabIndex = 4; this.keyUp.TabIndex = 4;
this.keyUp.UseVisualStyleBackColor = true; this.keyUp.UseVisualStyleBackColor = true;
this.keyUp.Click += new System.EventHandler(this.ButtonMove_Click); this.keyUp.Click += new System.EventHandler(this.ButtonMove_Click);
@ -124,9 +127,10 @@
this.keyLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.keyLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyLeft.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyLeft; this.keyLeft.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyLeft;
this.keyLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.keyLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyLeft.Location = new System.Drawing.Point(688, 386); this.keyLeft.Location = new System.Drawing.Point(786, 515);
this.keyLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.keyLeft.Name = "keyLeft"; this.keyLeft.Name = "keyLeft";
this.keyLeft.Size = new System.Drawing.Size(30, 30); this.keyLeft.Size = new System.Drawing.Size(34, 40);
this.keyLeft.TabIndex = 5; this.keyLeft.TabIndex = 5;
this.keyLeft.UseVisualStyleBackColor = true; this.keyLeft.UseVisualStyleBackColor = true;
this.keyLeft.Click += new System.EventHandler(this.ButtonMove_Click); this.keyLeft.Click += new System.EventHandler(this.ButtonMove_Click);
@ -136,41 +140,19 @@
this.keyRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.keyRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyRight.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyRight; this.keyRight.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyRight;
this.keyRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.keyRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyRight.Location = new System.Drawing.Point(760, 386); this.keyRight.Location = new System.Drawing.Point(868, 515);
this.keyRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.keyRight.Name = "keyRight"; this.keyRight.Name = "keyRight";
this.keyRight.Size = new System.Drawing.Size(30, 30); this.keyRight.Size = new System.Drawing.Size(34, 40);
this.keyRight.TabIndex = 6; this.keyRight.TabIndex = 6;
this.keyRight.UseVisualStyleBackColor = true; this.keyRight.UseVisualStyleBackColor = true;
this.keyRight.Click += new System.EventHandler(this.ButtonMove_Click); this.keyRight.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// ButtonCreateImproved
//
this.ButtonCreateImproved.Location = new System.Drawing.Point(93, 393);
this.ButtonCreateImproved.Name = "ButtonCreateImproved";
this.ButtonCreateImproved.Size = new System.Drawing.Size(75, 23);
this.ButtonCreateImproved.TabIndex = 7;
this.ButtonCreateImproved.Text = "Improved";
this.ButtonCreateImproved.UseVisualStyleBackColor = true;
this.ButtonCreateImproved.Click += new System.EventHandler(this.ButtonCreateImproved_Click);
//
// buttonChoose
//
this.buttonChoose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonChoose.Location = new System.Drawing.Point(598, 390);
this.buttonChoose.Name = "buttonChoose";
this.buttonChoose.Size = new System.Drawing.Size(75, 23);
this.buttonChoose.TabIndex = 8;
this.buttonChoose.Text = "Choose";
this.buttonChoose.UseVisualStyleBackColor = true;
this.buttonChoose.Click += new System.EventHandler(this.ButtonChoose_Click);
//
// FormGasolineTanker // FormGasolineTanker
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(807, 450); this.ClientSize = new System.Drawing.Size(922, 600);
this.Controls.Add(this.buttonChoose);
this.Controls.Add(this.ButtonCreateImproved);
this.Controls.Add(this.keyRight); this.Controls.Add(this.keyRight);
this.Controls.Add(this.keyLeft); this.Controls.Add(this.keyLeft);
this.Controls.Add(this.keyUp); this.Controls.Add(this.keyUp);
@ -178,6 +160,7 @@
this.Controls.Add(this.buttonCreate); this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.pictureBoxGasolineTanker); this.Controls.Add(this.pictureBoxGasolineTanker);
this.Controls.Add(this.statusStrip1); this.Controls.Add(this.statusStrip1);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "FormGasolineTanker"; this.Name = "FormGasolineTanker";
this.Text = "Gasoline tanker"; this.Text = "Gasoline tanker";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).EndInit();
@ -200,7 +183,5 @@
private Button keyUp; private Button keyUp;
private Button keyLeft; private Button keyLeft;
private Button keyRight; private Button keyRight;
private Button ButtonCreateImproved;
private Button buttonChoose;
} }
} }

View File

@ -3,7 +3,6 @@ namespace GasolineTanker
public partial class FormGasolineTanker : Form public partial class FormGasolineTanker : Form
{ {
private DrawingGasolineTanker _gasolineTanker; private DrawingGasolineTanker _gasolineTanker;
public DrawingGasolineTanker SelectedGasolineTanker { get; private set; }
public FormGasolineTanker() public FormGasolineTanker()
{ {
InitializeComponent(); InitializeComponent();
@ -15,33 +14,22 @@ namespace GasolineTanker
_gasolineTanker?.DrawTransport(gr); _gasolineTanker?.DrawTransport(gr);
pictureBoxGasolineTanker.Image = bmp; pictureBoxGasolineTanker.Image = bmp;
} }
private void SetData()
{
Random rnd = new();
_gasolineTanker.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
toolStripStatusSpeed.Text = $"Speed {_gasolineTanker.GasolineTanker.Speed}";
toolStripStatusWeight.Text = $"Weight {_gasolineTanker.GasolineTanker.Weight}";
toolStripStatusBodyColor.Text = $"Color {_gasolineTanker.GasolineTanker.BodyColor.Name}";
}
private void PictureBoxCar_Resize(object sender, EventArgs e) private void PictureBoxCar_Resize(object sender, EventArgs e)
{ {
_gasolineTanker?.ChangeBorders(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height); _gasolineTanker?.ChangeBorders(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
Draw(); Draw();
} }
private void ButtonCreate_Click_1(object sender, EventArgs e) private void buttonCreate_Click_1(object sender, EventArgs e)
{ {
Random rnd = new(); Random rnd = new();
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256),rnd.Next(0, 256)); _gasolineTanker = new DrawingGasolineTanker();
ColorDialog dialog = new(); _gasolineTanker.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
if (dialog.ShowDialog() == DialogResult.OK) _gasolineTanker.SetPosition(rnd.Next(10, 100),rnd.Next(50, 100), pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
{ toolStripStatusSpeed.Text = $"Speed {_gasolineTanker.GasolineTanker.Speed}";
color = dialog.Color; toolStripStatusWeight.Text = $"Weight {_gasolineTanker.GasolineTanker.Weight}";
} toolStripStatusBodyColor.Text = $"Color {_gasolineTanker.GasolineTanker.BodyColor.Name}";
_gasolineTanker = new DrawingGasolineTanker(rnd.Next(100, 300), rnd.Next(1000, 2000),color);
SetData();
Draw(); Draw();
} }
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
@ -71,31 +59,5 @@ namespace GasolineTanker
_gasolineTanker?.ChangeBorders(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height); _gasolineTanker?.ChangeBorders(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
Draw(); Draw();
} }
private void ButtonCreateImproved_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 colorDop = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialogDop = new();
if (dialogDop.ShowDialog() == DialogResult.OK)
{
colorDop = dialogDop.Color;
}
_gasolineTanker = new DrawingImprovedGasolineTanker(rnd.Next(100, 300), rnd.Next(1000, 2000), color, colorDop, Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData();
Draw();
}
private void ButtonChoose_Click(object sender, EventArgs e)
{
SelectedGasolineTanker = _gasolineTanker;
DialogResult = DialogResult.OK;
}
} }
} }

View File

@ -1,214 +0,0 @@
namespace GasolineTanker
{
partial class FormMapWithSetGasolineTanker
{
/// <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.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.keyRight = new System.Windows.Forms.Button();
this.keyLeft = new System.Windows.Forms.Button();
this.keyUp = new System.Windows.Forms.Button();
this.keyDown = new System.Windows.Forms.Button();
this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonRemoveGasolineTanker = new System.Windows.Forms.Button();
this.buttonAddGasolineTanker = 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.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.keyRight);
this.groupBoxTools.Controls.Add(this.keyLeft);
this.groupBoxTools.Controls.Add(this.keyUp);
this.groupBoxTools.Controls.Add(this.keyDown);
this.groupBoxTools.Controls.Add(this.buttonShowOnMap);
this.groupBoxTools.Controls.Add(this.buttonShowStorage);
this.groupBoxTools.Controls.Add(this.buttonRemoveGasolineTanker);
this.groupBoxTools.Controls.Add(this.buttonAddGasolineTanker);
this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(600, 0);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(200, 450);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Tools";
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(10, 113);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(184, 23);
this.maskedTextBoxPosition.TabIndex = 11;
//
// keyRight
//
this.keyRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyRight.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyRight;
this.keyRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyRight.Location = new System.Drawing.Point(122, 408);
this.keyRight.Name = "keyRight";
this.keyRight.Size = new System.Drawing.Size(30, 30);
this.keyRight.TabIndex = 10;
this.keyRight.UseVisualStyleBackColor = true;
this.keyRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// keyLeft
//
this.keyLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyLeft.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyLeft;
this.keyLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyLeft.Location = new System.Drawing.Point(50, 408);
this.keyLeft.Name = "keyLeft";
this.keyLeft.Size = new System.Drawing.Size(30, 30);
this.keyLeft.TabIndex = 9;
this.keyLeft.UseVisualStyleBackColor = true;
this.keyLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// keyUp
//
this.keyUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyUp.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyUp;
this.keyUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyUp.Location = new System.Drawing.Point(86, 372);
this.keyUp.Name = "keyUp";
this.keyUp.Size = new System.Drawing.Size(30, 30);
this.keyUp.TabIndex = 8;
this.keyUp.UseVisualStyleBackColor = true;
this.keyUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// keyDown
//
this.keyDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyDown.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyDown;
this.keyDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyDown.Location = new System.Drawing.Point(86, 408);
this.keyDown.Name = "keyDown";
this.keyDown.Size = new System.Drawing.Size(30, 30);
this.keyDown.TabIndex = 7;
this.keyDown.UseVisualStyleBackColor = true;
this.keyDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(8, 326);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(186, 34);
this.buttonShowOnMap.TabIndex = 4;
this.buttonShowOnMap.Text = "Show on map";
this.buttonShowOnMap.UseVisualStyleBackColor = true;
this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(8, 241);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(186, 34);
this.buttonShowStorage.TabIndex = 3;
this.buttonShowStorage.Text = "Show storage";
this.buttonShowStorage.UseVisualStyleBackColor = true;
this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
//
// buttonRemoveGasolineTanker
//
this.buttonRemoveGasolineTanker.Location = new System.Drawing.Point(10, 142);
this.buttonRemoveGasolineTanker.Name = "buttonRemoveGasolineTanker";
this.buttonRemoveGasolineTanker.Size = new System.Drawing.Size(186, 34);
this.buttonRemoveGasolineTanker.TabIndex = 2;
this.buttonRemoveGasolineTanker.Text = "Remove gasoline tanker";
this.buttonRemoveGasolineTanker.UseVisualStyleBackColor = true;
this.buttonRemoveGasolineTanker.Click += new System.EventHandler(this.ButtonRemoveGasolineTanker_Click);
//
// buttonAddGasolineTanker
//
this.buttonAddGasolineTanker.Location = new System.Drawing.Point(8, 73);
this.buttonAddGasolineTanker.Name = "buttonAddGasolineTanker";
this.buttonAddGasolineTanker.Size = new System.Drawing.Size(186, 34);
this.buttonAddGasolineTanker.TabIndex = 1;
this.buttonAddGasolineTanker.Text = "Add gasoline tanker";
this.buttonAddGasolineTanker.UseVisualStyleBackColor = true;
this.buttonAddGasolineTanker.Click += new System.EventHandler(this.ButtonAddGasolineTanker_Click);
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Simple map",
"Long map"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 22);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(188, 23);
this.comboBoxSelectorMap.TabIndex = 12;
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.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(600, 450);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// FormMapWithSetGasolineTanker
//
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.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Name = "FormMapWithSetGasolineTanker";
this.Text = "FormMapWithSetGasolineTanker";
this.groupBoxTools.ResumeLayout(false);
this.groupBoxTools.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
private GroupBox groupBoxTools;
private Button buttonShowOnMap;
private Button buttonShowStorage;
private Button buttonRemoveGasolineTanker;
private Button buttonAddGasolineTanker;
private ComboBox comboBoxSelectorMap;
private PictureBox pictureBox;
private Button keyRight;
private Button keyLeft;
private Button keyUp;
private Button keyDown;
private MaskedTextBox maskedTextBoxPosition;
}
}

View File

@ -1,131 +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 GasolineTanker
{
public partial class FormMapWithSetGasolineTanker : Form
{
private MapWithSetGasolienTankerGeneric<DrawingObjectGasolineTanker, AbstractMap> _mapGasolineTankerCollectionGeneric;
public FormMapWithSetGasolineTanker()
{
InitializeComponent();
}
private void ButtonAddGasolineTanker_Click(object sender, EventArgs e)
{
if (_mapGasolineTankerCollectionGeneric == null)
{
return;
}
FormGasolineTanker form = new();
if (form.ShowDialog() == DialogResult.OK)
{
DrawingObjectGasolineTanker GasolineTanker = new(form.SelectedGasolineTanker);
if (_mapGasolineTankerCollectionGeneric + GasolineTanker >= 0)
{
MessageBox.Show("Object added");
pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Failed to add object");
}
}
}
private void ButtonRemoveGasolineTanker_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
}
if (MessageBox.Show("Delete object?", "Removal", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapGasolineTankerCollectionGeneric - pos != null)
{
MessageBox.Show("Object deleted");
pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Failed to delete object");
}
}
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapGasolineTankerCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowSet();
}
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapGasolineTankerCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowOnMap();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapGasolineTankerCollectionGeneric == null)
{
return;
}
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "keyUp":
dir = Direction.Up;
break;
case "keyDown":
dir = Direction.Down;
break;
case "keyLeft":
dir = Direction.Left;
break;
case "keyRight":
dir = Direction.Right;
break;
}
pictureBox.Image = _mapGasolineTankerCollectionGeneric.MoveObject(dir);
}
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
{
case "Simple map":
map = new SimpleMap();
break;
case "Long map":
map = new LongMap();
break;
}
if (map != null)
{
_mapGasolineTankerCollectionGeneric = new MapWithSetGasolienTankerGeneric<DrawingObjectGasolineTanker, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapGasolineTankerCollectionGeneric = null;
}
}
}
}

View File

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

@ -1,17 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal interface IDrawingObject
{
public float Step { get; }
void SetObject(int x, int y, int width, int height);
void MoveObject(Direction direction);
void DrawningObject(Graphics g);
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
}
}

View File

@ -1,49 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class LongMap : 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, _size_x, _size_y);
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, _size_x, _size_y);
}
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 < 5)
{
int xStart = _random.Next(0, 100);
int xEnd = _random.Next(80, 100);
for (int i = xStart; i <= xEnd; ++i)
{
_map[i, xStart] = _barrier;
}
counter++;
}
}
}
}

View File

@ -1,126 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class MapWithSetGasolienTankerGeneric<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 SetGasolineTankerGeneric<T> _setGasolineTanker;
private readonly U _map;
public MapWithSetGasolienTankerGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setGasolineTanker = new SetGasolineTankerGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static int operator +(MapWithSetGasolienTankerGeneric<T, U> map, T gasolineTanker)
{
return map._setGasolineTanker.Insert(gasolineTanker);
}
public static T operator -(MapWithSetGasolienTankerGeneric<T, U> map, int position)
{
return map._setGasolineTanker.Remove(position);
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawGasolineTanker(gr);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setGasolineTanker.Count; i++)
{
var gasolineTanker = _setGasolineTanker.Get(i);
if (gasolineTanker != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, gasolineTanker);
}
}
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 = _setGasolineTanker.Count - 1;
for (int i = 0; i < _setGasolineTanker.Count; i++)
{
if (_setGasolineTanker.Get(i) == null)
{
for (; j > i; j--)
{
var gasolineTanker = _setGasolineTanker.Get(j);
if (gasolineTanker != null)
{
_setGasolineTanker.Insert(gasolineTanker, i);
_setGasolineTanker.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 5);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.DrawLine(pen, i * _placeSizeWidth + 10, j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2);
g.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20);
g.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20);
}
}
}
private void DrawGasolineTanker(Graphics g)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _setGasolineTanker.Count; i++)
{
if (_setGasolineTanker.Get(i) != null)
{
_setGasolineTanker.Get(i).SetObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
_setGasolineTanker.Get(i)?.DrawningObject(g);
}
}
}
}
}

View File

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

View File

@ -1,80 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class SetGasolineTankerGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
public SetGasolineTankerGeneric(int count)
{
_places = new T[count];
}
public int Insert(T gasolineTanker)
{
if (_places[Count - 1] == null)
{
for (int i = Count - 1; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = gasolineTanker;
return 0;
}
return -1;
}
public int Insert(T gasolineTanker, int position)
{
if (position < 0 || position >= Count) return -1;
if (_places[position] == null)
{
_places[position] = gasolineTanker;
return position;
}
else
{
if (_places[Count - 1] == null)
{
for (int i = Count - 1; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = gasolineTanker;
return position;
}
return -1;
}
}
public T Remove(int position)
{
if (position < 0 || position >= Count)
return null;
if (_places[position] != null)
{
T removed = _places[position];
_places[position] = null;
if (position < Count - 1)
{
for (int k = position; k < Count - 1; k++)
{
_places[k] = _places[k + 1];
}
}
return removed;
}
return null;
}
public T Get(int position)
{
if (position >= Count || position < 0)
return null;
return _places[position];
}
}
}

View File

@ -1,47 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class SimpleMap : 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 < 50)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}