Лабораторная работа 2

This commit is contained in:
Кашин Максим 2022-09-21 10:27:38 +04:00
parent 7425d24b87
commit ac95e05cf4
6 changed files with 168 additions and 27 deletions

View File

@ -18,6 +18,47 @@ namespace GasolineTanker
protected readonly int _freeRoad = 0; protected readonly int _freeRoad = 0;
protected readonly int _barrier = 1; 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 starCellX = (int)(Left / _size_x);
int starCellY = (int)(Right / _size_y);
int endCellX = (int)(Top / _size_x);
int endCellY = (int)(Bottom / _size_y);
if (starCellX < 0 || starCellY < 0 || endCellX >= _map.GetLength(1) || endCellY >= _map.GetLength(0))
{
return 2;
}
for (int x = starCellX; x <= endCellX; x++)
{
for (int y = starCellY; y <= endCellY; y++)
{
if (_map[x, y] == _barrier)
{
return 1;
}
}
}
return 0;
}
public Bitmap CreateMap(int width, int height, IDrawingObject drawningObject) public Bitmap CreateMap(int width, int height, IDrawingObject drawningObject)
{ {
_width = width; _width = width;
@ -30,13 +71,18 @@ namespace GasolineTanker
} }
return DrawMapWithObject(); return DrawMapWithObject();
} }
public Bitmap MoveObject(Direction direction) public Bitmap MoveObject(Direction direction)
{ {
// TODO проверка, что объект может переместится в требуемом направлении
if (true) if (true)
{ {
_drawningObject.MoveObject(direction); _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(); return DrawMapWithObject();
} }
private bool SetObjectOnMap() private bool SetObjectOnMap()
@ -48,8 +94,31 @@ namespace GasolineTanker
int x = _random.Next(0, 10); int x = _random.Next(0, 10);
int y = _random.Next(0, 10); int y = _random.Next(0, 10);
_drawningObject.SetObject(x, y, _width, _height); _drawningObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки (float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition();
return true; float nowX = Left;
float nowY = Right;
float lenX = Top - Left;
float lenY = Bottom - Right;
while (CheckCollision(nowX, nowY, nowX + lenX, nowY + lenY) != 2)
{
int resout;
do
{
resout = CheckCollision(nowX, nowY, nowX + lenX, nowY + lenY);
if (resout == 0)
{
_drawningObject.SetObject((int)nowX, (int)nowY, _width, _height);
return true;
}
else
{
nowX += _size_x;
}
} while (resout != 2);
nowX = x;
nowY += _size_y;
}
return false;
} }
private Bitmap DrawMapWithObject() private Bitmap DrawMapWithObject()
{ {

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class BoxMap : AbstractMap
{
/// <summary>
/// Цвет участка закрытого
/// </summary>
private readonly Brush barrierColor = new SolidBrush(Color.Blue);
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 < 10)
{
int sx = _random.Next(0, 100);
int sy = _random.Next(0, 100);
int ex = sx + _random.Next(0, 100 - sx);
int ey = sy + _random.Next(0, 100 - sy);
for (int i = sx; i <= ex; ++i)
{
_map[i, ey] = _barrier;
}
for (int i = sy; i <= ey; i++)
{
_map[sx, i] = _barrier;
}
counter++;
}
}
}
}

View File

@ -27,6 +27,8 @@ namespace GasolineTanker
{ {
Brush brOrange = new SolidBrush(Color.Orange); Brush brOrange = new SolidBrush(Color.Orange);
g.FillRectangle(brOrange, _startPosX + 25, _startPosY + 5, 100, 35); g.FillRectangle(brOrange, _startPosX + 25, _startPosY + 5, 100, 35);
Brush brRed = new SolidBrush(Color.Red);
g.FillRectangle(brRed, _startPosX + 80, _startPosY, 10, 5);
} }
_startPosX += 10; _startPosX += 10;

View File

@ -34,7 +34,7 @@ namespace GasolineTanker
void IDrawingObject.DrawningObject(Graphics g) void IDrawingObject.DrawningObject(Graphics g)
{ {
// TODO _gasolineTanker.DrawTransport(g);
} }
} }
} }

View File

@ -48,8 +48,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,36 +61,38 @@
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;
@ -100,9 +103,10 @@
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 +116,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 +129,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,18 +142,20 @@
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(869, 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 // ButtonCreateImproved
// //
this.ButtonCreateImproved.Location = new System.Drawing.Point(93, 393); this.ButtonCreateImproved.Location = new System.Drawing.Point(106, 524);
this.ButtonCreateImproved.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.ButtonCreateImproved.Name = "ButtonCreateImproved"; this.ButtonCreateImproved.Name = "ButtonCreateImproved";
this.ButtonCreateImproved.Size = new System.Drawing.Size(75, 23); this.ButtonCreateImproved.Size = new System.Drawing.Size(86, 31);
this.ButtonCreateImproved.TabIndex = 7; this.ButtonCreateImproved.TabIndex = 7;
this.ButtonCreateImproved.Text = "Improved"; this.ButtonCreateImproved.Text = "Improved";
this.ButtonCreateImproved.UseVisualStyleBackColor = true; this.ButtonCreateImproved.UseVisualStyleBackColor = true;
@ -158,18 +166,20 @@
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true; this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] { this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Simple map"}); "Simple map",
this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12); "Box map"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(14, 16);
this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23); this.comboBoxSelectorMap.Size = new System.Drawing.Size(138, 28);
this.comboBoxSelectorMap.TabIndex = 8; this.comboBoxSelectorMap.TabIndex = 8;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.comboBoxSelectorMap_SelectedIndexChanged); this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.comboBoxSelectorMap_SelectedIndexChanged);
// //
// FormMap // FormMap
// //
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.comboBoxSelectorMap); this.Controls.Add(this.comboBoxSelectorMap);
this.Controls.Add(this.ButtonCreateImproved); this.Controls.Add(this.ButtonCreateImproved);
this.Controls.Add(this.keyRight); this.Controls.Add(this.keyRight);
@ -179,6 +189,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 = "FormMap"; this.Name = "FormMap";
this.Text = "Map"; this.Text = "Map";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).EndInit();

View File

@ -75,6 +75,9 @@ namespace GasolineTanker
case "Simple map": case "Simple map":
_abstractMap = new SimpleMap(); _abstractMap = new SimpleMap();
break; break;
case "Box map":
_abstractMap = new BoxMap();
break;
} }
} }