Lab Work 2

This commit is contained in:
Кашин Максим 2022-09-22 23:45:36 +04:00
parent ac95e05cf4
commit 74bedf29c8
4 changed files with 51 additions and 71 deletions

View File

@ -38,17 +38,17 @@ namespace GasolineTanker
private int CheckCollision(float Left, float Right, float Top, float Bottom) private int CheckCollision(float Left, float Right, float Top, float Bottom)
{ {
int starCellX = (int)(Left / _size_x); int starCoordinateX = (int)(Left / _size_x);
int starCellY = (int)(Right / _size_y); int starCoordinateY = (int)(Right / _size_y);
int endCellX = (int)(Top / _size_x); int endCoordinateX = (int)(Top / _size_x);
int endCellY = (int)(Bottom / _size_y); int endCoordinateY = (int)(Bottom / _size_y);
if (starCellX < 0 || starCellY < 0 || endCellX >= _map.GetLength(1) || endCellY >= _map.GetLength(0)) if (starCoordinateX < 0 || starCoordinateY < 0 || endCoordinateX >= _map.GetLength(1) || endCoordinateY >= _map.GetLength(0))
{ {
return 2; return 2;
} }
for (int x = starCellX; x <= endCellX; x++) for (int x = starCoordinateX; x <= endCoordinateX; x++)
{ {
for (int y = starCellY; y <= endCellY; y++) for (int y = starCoordinateY; y <= endCoordinateY; y++)
{ {
if (_map[x, y] == _barrier) if (_map[x, y] == _barrier)
{ {
@ -95,28 +95,25 @@ namespace GasolineTanker
int y = _random.Next(0, 10); int y = _random.Next(0, 10);
_drawningObject.SetObject(x, y, _width, _height); _drawningObject.SetObject(x, y, _width, _height);
(float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition(); (float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition();
float nowX = Left;
float nowY = Right; while (CheckCollision(Left, Right, Top, Bottom) != 2)
float lenX = Top - Left;
float lenY = Bottom - Right;
while (CheckCollision(nowX, nowY, nowX + lenX, nowY + lenY) != 2)
{ {
int resout; int resoult;
do do
{ {
resout = CheckCollision(nowX, nowY, nowX + lenX, nowY + lenY); resoult = CheckCollision(Left, Right, Top, Bottom);
if (resout == 0) if (resoult == 0)
{ {
_drawningObject.SetObject((int)nowX, (int)nowY, _width, _height); _drawningObject.SetObject((int)Left, (int)Right, _width, _height);
return true; return true;
} }
else else
{ {
nowX += _size_x; Left += _size_x;
} }
} while (resout != 2); } while (resoult != 2);
nowX = x; Left = x;
nowY += _size_y; Right += _size_y;
} }
return false; return false;
} }

View File

@ -48,9 +48,8 @@
// //
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(922, 574); this.pictureBoxGasolineTanker.Size = new System.Drawing.Size(807, 428);
this.pictureBoxGasolineTanker.TabIndex = 0; this.pictureBoxGasolineTanker.TabIndex = 0;
this.pictureBoxGasolineTanker.TabStop = false; this.pictureBoxGasolineTanker.TabStop = false;
// //
@ -61,38 +60,36 @@
this.toolStripStatusSpeed, this.toolStripStatusSpeed,
this.toolStripStatusWeight, this.toolStripStatusWeight,
this.toolStripStatusBodyColor}); this.toolStripStatusBodyColor});
this.statusStrip1.Location = new System.Drawing.Point(0, 574); this.statusStrip1.Location = new System.Drawing.Point(0, 428);
this.statusStrip1.Name = "statusStrip1"; this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Padding = new System.Windows.Forms.Padding(1, 0, 16, 0); this.statusStrip1.Size = new System.Drawing.Size(807, 22);
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(51, 20); this.toolStripStatusSpeed.Size = new System.Drawing.Size(39, 17);
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(56, 20); this.toolStripStatusWeight.Size = new System.Drawing.Size(45, 17);
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(45, 20); this.toolStripStatusBodyColor.Size = new System.Drawing.Size(36, 17);
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(14, 524); this.buttonCreate.Location = new System.Drawing.Point(12, 393);
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(86, 31); this.buttonCreate.Size = new System.Drawing.Size(75, 23);
this.buttonCreate.TabIndex = 2; this.buttonCreate.TabIndex = 2;
this.buttonCreate.Text = "Create"; this.buttonCreate.Text = "Create";
this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreate.UseVisualStyleBackColor = true;
@ -103,10 +100,9 @@
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(827, 515); this.keyDown.Location = new System.Drawing.Point(724, 386);
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(34, 40); this.keyDown.Size = new System.Drawing.Size(30, 30);
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);
@ -116,10 +112,9 @@
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(827, 467); this.keyUp.Location = new System.Drawing.Point(724, 350);
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(34, 40); this.keyUp.Size = new System.Drawing.Size(30, 30);
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);
@ -129,10 +124,9 @@
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(786, 515); this.keyLeft.Location = new System.Drawing.Point(688, 386);
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(34, 40); this.keyLeft.Size = new System.Drawing.Size(30, 30);
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);
@ -142,20 +136,18 @@
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(869, 515); this.keyRight.Location = new System.Drawing.Point(760, 386);
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(34, 40); this.keyRight.Size = new System.Drawing.Size(30, 30);
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(106, 524); this.ButtonCreateImproved.Location = new System.Drawing.Point(93, 393);
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(86, 31); this.ButtonCreateImproved.Size = new System.Drawing.Size(75, 23);
this.ButtonCreateImproved.TabIndex = 7; this.ButtonCreateImproved.TabIndex = 7;
this.ButtonCreateImproved.Text = "Improved"; this.ButtonCreateImproved.Text = "Improved";
this.ButtonCreateImproved.UseVisualStyleBackColor = true; this.ButtonCreateImproved.UseVisualStyleBackColor = true;
@ -167,19 +159,18 @@
this.comboBoxSelectorMap.FormattingEnabled = true; this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] { this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Simple map", "Simple map",
"Box map"}); "Long map"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(14, 16); this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12);
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(138, 28); this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23);
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(8F, 20F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(922, 600); this.ClientSize = new System.Drawing.Size(807, 450);
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);
@ -189,7 +180,6 @@
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,8 +75,8 @@ namespace GasolineTanker
case "Simple map": case "Simple map":
_abstractMap = new SimpleMap(); _abstractMap = new SimpleMap();
break; break;
case "Box map": case "Long map":
_abstractMap = new BoxMap(); _abstractMap = new LongMap();
break; break;
} }
} }

View File

@ -6,12 +6,9 @@ using System.Threading.Tasks;
namespace GasolineTanker namespace GasolineTanker
{ {
internal class BoxMap : AbstractMap internal class LongMap : AbstractMap
{ {
/// <summary> private readonly Brush barrierColor = new SolidBrush(Color.Black);
/// Цвет участка закрытого
/// </summary>
private readonly Brush barrierColor = new SolidBrush(Color.Blue);
private readonly Brush roadColor = new SolidBrush(Color.Gray); private readonly Brush roadColor = new SolidBrush(Color.Gray);
protected override void DrawBarrierPart(Graphics g, int i, int j) protected override void DrawBarrierPart(Graphics g, int i, int j)
{ {
@ -34,20 +31,16 @@ namespace GasolineTanker
_map[i, j] = _freeRoad; _map[i, j] = _freeRoad;
} }
} }
while (counter < 10) while (counter < 5)
{ {
int sx = _random.Next(0, 100); int xStart = _random.Next(0, 100);
int sy = _random.Next(0, 100); int xEnd = _random.Next(80, 100);
int ex = sx + _random.Next(0, 100 - sx);
int ey = sy + _random.Next(0, 100 - sy); for (int i = xStart; i <= xEnd; ++i)
for (int i = sx; i <= ex; ++i)
{ {
_map[i, ey] = _barrier; _map[i, xStart] = _barrier;
}
for (int i = sy; i <= ey; i++)
{
_map[sx, i] = _barrier;
} }
counter++; counter++;
} }
} }