Вторая лабораторная. Изменения

This commit is contained in:
Никита Чернышов 2022-10-26 23:20:06 +04:00
parent b2e5bc9cf0
commit 74bd09ed74
11 changed files with 170 additions and 35 deletions

View File

@ -31,13 +31,35 @@ namespace AircraftCarrier
} }
public Bitmap MoveObject(Direction direction) public Bitmap MoveObject(Direction direction)
{ {
// TODO проверка, что объект может переместится в требуемом направлении
if (true) if (true)
{ {
_drawingObjectWarship.MoveObject(direction); _drawingObjectWarship.MoveObject(direction);
} }
(float Left, float Right, float Top, float Bottom) = _drawingObjectWarship.GetCurrentPosition();
if (Check(Left, Right, Top, Bottom) != 0)
{
_drawingObjectWarship.MoveObject(GetOpositDirection(direction));
}
return DrawMapWithObject(); return DrawMapWithObject();
} }
private Direction GetOpositDirection(Direction dir)
{
switch (dir)
{
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 bool SetObjectOnMap() private bool SetObjectOnMap()
{ {
if (_drawingObjectWarship == null || _map == null) if (_drawingObjectWarship == null || _map == null)
@ -47,9 +69,55 @@ namespace AircraftCarrier
int x = _random.Next(0, 10); int x = _random.Next(0, 10);
int y = _random.Next(0, 10); int y = _random.Next(0, 10);
_drawingObjectWarship.SetObject(x, y, _width, _height); _drawingObjectWarship.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки (float Left, float Right, float Top, float Bottom) = _drawingObjectWarship.GetCurrentPosition();
float nowX = Left;
float nowY = Right;
float lenX = Top - Left;
float lenY = Bottom - Right;
while (Check(nowX, nowY, nowX + lenX, nowY + lenY) != 2)
{
int result;
do
{
result = Check(nowX, nowY, nowX + lenX, nowY + lenY);
if (result == 0)
{
_drawingObjectWarship.SetObject((int)nowX, (int)nowY, _width, _height);
return true; return true;
} }
else
{
nowX += _size_x;
}
} while (result != 2);
nowX = x;
nowY += _size_y;
}
return false;
}
private int Check(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);
if (startX < 0 || startY < 0 || endX >= _map.GetLength(0) || endY >= _map.GetLength(0))
{
return 2;
}
for (int i = startX; i <= endX; i++)
{
for (int j = startY; j <= endY; j++)
{
if (_map[i, j] == _barrier)
{
return 1;
}
}
}
return 0;
}
private Bitmap DrawMapWithObject() private Bitmap DrawMapWithObject()
{ {
Bitmap bmp = new(_width, _height); Bitmap bmp = new(_width, _height);

View File

@ -28,7 +28,7 @@ namespace AircraftCarrier
} }
public void DrawningObject(Graphics g) public void DrawningObject(Graphics g)
{ {
// TODO _ship.DrawTransport(g);
} }
} }
} }

View File

@ -38,7 +38,7 @@ namespace AircraftCarrier
Brush br = new SolidBrush(Ship?.BodyColor ?? Color.White); Brush br = new SolidBrush(Ship?.BodyColor ?? Color.White);
if (planeWarship.BodyKit) if (planeWarship.BodyKit)
{ {
// броня носовой части корабля // броня корабля
PointF pointShipArmor1 = new PointF(_startPosX + 167, _startPosY + 25); PointF pointShipArmor1 = new PointF(_startPosX + 167, _startPosY + 25);
PointF pointShipArmor2 = new PointF(_startPosX + 237, _startPosY + 40); PointF pointShipArmor2 = new PointF(_startPosX + 237, _startPosY + 40);
PointF pointShipArmor3 = new PointF(_startPosX + 167, _startPosY + 75); PointF pointShipArmor3 = new PointF(_startPosX + 167, _startPosY + 75);
@ -51,8 +51,19 @@ namespace AircraftCarrier
g.DrawPolygon(pen, PointsShipArmor); g.DrawPolygon(pen, PointsShipArmor);
g.DrawEllipse(pen, _startPosX + 224, _startPosY + 37, 20, 20); g.DrawEllipse(pen, _startPosX + 224, _startPosY + 37, 20, 20);
g.FillEllipse(br, _startPosX + 224, _startPosY + 37, 20, 20); g.FillEllipse(br, _startPosX + 224, _startPosY + 37, 20, 20);
// торпеда
g.DrawRectangle(pen, _startPosX + 30, _startPosY + 8, 105, 15);
g.FillRectangle(br, _startPosX + 30, _startPosY + 8, 105, 15);
g.DrawEllipse(pen, _startPosX + 130, _startPosY + 5, 35, 20);
g.FillEllipse(br, _startPosX + 130, _startPosY + 5, 35, 20);
} }
_startPosX += 22;
_startPosY += 25;
base.DrawTransport(g);
_startPosX -= 22;
_startPosY -= 25;
if (planeWarship.RunWay) if (planeWarship.RunWay)
{ {
//взлетная полоса //взлетная полоса
@ -65,12 +76,6 @@ namespace AircraftCarrier
g.FillRectangle(brYellow, _startPosX + 132, _startPosY + 87, 25, 5); g.FillRectangle(brYellow, _startPosX + 132, _startPosY + 87, 25, 5);
} }
_startPosX += 22;
_startPosY += 25;
base.DrawTransport(g);
_startPosX -= 22;
_startPosY -= 25;
if (planeWarship.СontrolPlace) if (planeWarship.СontrolPlace)
{ {
g.FillEllipse(dopBrush, _startPosX + 127, _startPosY + 30, 10, 10); g.FillEllipse(dopBrush, _startPosX + 127, _startPosY + 30, 10, 10);

View File

@ -191,8 +191,7 @@ namespace AircraftCarrier
/// <returns></returns> /// <returns></returns>
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{ {
return (_startPosX, _startPosY, _startPosX + _shipWidth, _startPosY + return (_startPosX, _startPosY, _startPosX + _shipWidth, _startPosY + _shipHeight);
_shipHeight);
} }
} }
} }

View File

@ -26,7 +26,7 @@ namespace AircraftCarrier
/// <summary> /// <summary>
/// Шаг перемещения корабля /// Шаг перемещения корабля
/// </summary> /// </summary>
public float Step => Speed * 5000 / Weight; public float Step => Speed * 100 / Weight;
/// <summary> /// <summary>
/// Инициализация полей объекта-класса корабля /// Инициализация полей объекта-класса корабля
/// </summary> /// </summary>
@ -38,7 +38,7 @@ namespace AircraftCarrier
{ {
Random rnd = new Random(); Random rnd = new Random();
Speed = speed <= 0 ? rnd.Next(50, 100) : speed; Speed = speed <= 0 ? rnd.Next(50, 100) : speed;
Weight = weight <= 0 ? rnd.Next(20000, 30000) : weight; Weight = weight <= 0 ? rnd.Next(40, 70) : weight;
BodyColor = bodyColor; BodyColor = bodyColor;
} }
} }

View File

@ -144,6 +144,7 @@
// //
// buttonCreateModif // buttonCreateModif
// //
this.buttonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateModif.Location = new System.Drawing.Point(107, 382); this.buttonCreateModif.Location = new System.Drawing.Point(107, 382);
this.buttonCreateModif.Name = "buttonCreateModif"; this.buttonCreateModif.Name = "buttonCreateModif";
this.buttonCreateModif.Size = new System.Drawing.Size(130, 23); this.buttonCreateModif.Size = new System.Drawing.Size(130, 23);
@ -157,11 +158,13 @@
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[] {
"Простая карта"}); "Простая карта",
"Морская карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(8, 8); this.comboBoxSelectorMap.Location = new System.Drawing.Point(8, 8);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23); 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);
// //
// FormMap // FormMap
// //
@ -178,7 +181,7 @@
this.Controls.Add(this.buttonCreate); this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.pictureBoxShip); this.Controls.Add(this.pictureBoxShip);
this.Name = "FormMap"; this.Name = "FormMap";
this.Text = "FormMap"; this.Text = "Карта";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).EndInit();
this.statusStrip1.ResumeLayout(false); this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout(); this.statusStrip1.PerformLayout();

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -38,7 +39,7 @@ namespace AircraftCarrier
private void ButtonCreate_Click(object sender, EventArgs e) private void ButtonCreate_Click(object sender, EventArgs e)
{ {
Random rnd = new(); Random rnd = new();
var ship = new DrawingWarship(rnd.Next(100, 300), rnd.Next(1000, 2000), var ship = new DrawingWarship(rnd.Next(1000, 2000), rnd.Next(15000, 20000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
SetData(ship); SetData(ship);
} }
@ -77,7 +78,7 @@ namespace AircraftCarrier
private void ButtonCreateModif_Click(object sender, EventArgs e) private void ButtonCreateModif_Click(object sender, EventArgs e)
{ {
Random rnd = new(); Random rnd = new();
var ship = new DrawingPlaneWarship(rnd.Next(100, 300), rnd.Next(1000, 2000), var ship = new DrawingPlaneWarship(rnd.Next(1000, 2000), rnd.Next(15000, 20000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
@ -95,8 +96,10 @@ namespace AircraftCarrier
case "Простая карта": case "Простая карта":
_abstractMap = new SimpleMap(); _abstractMap = new SimpleMap();
break; break;
case "Морская карта":
_abstractMap = new SeaMap();
break;
} }
} }
} }
} }

View File

@ -144,6 +144,7 @@
// //
// buttonCreateModif // buttonCreateModif
// //
this.buttonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateModif.Location = new System.Drawing.Point(107, 382); this.buttonCreateModif.Location = new System.Drawing.Point(107, 382);
this.buttonCreateModif.Name = "buttonCreateModif"; this.buttonCreateModif.Name = "buttonCreateModif";
this.buttonCreateModif.Size = new System.Drawing.Size(130, 23); this.buttonCreateModif.Size = new System.Drawing.Size(130, 23);

View File

@ -36,7 +36,7 @@ namespace AircraftCarrier
private void ButtonCreate_Click(object sender, EventArgs e) private void ButtonCreate_Click(object sender, EventArgs e)
{ {
Random rnd = new(); Random rnd = new();
_ship = new DrawingWarship(rnd.Next(50, 100), rnd.Next(20000, 30000), _ship = new DrawingWarship(rnd.Next(1000, 3000), rnd.Next(10000, 20000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
Setdata(); Setdata();
Draw(); Draw();
@ -76,7 +76,7 @@ namespace AircraftCarrier
private void ButtonCreateModif_Click(object sender, EventArgs e) private void ButtonCreateModif_Click(object sender, EventArgs e)
{ {
Random rnd = new(); Random rnd = new();
_ship = new DrawingPlaneWarship(rnd.Next(50, 100), rnd.Next(20000, 30000), _ship = new DrawingPlaneWarship(rnd.Next(1000, 3000), rnd.Next(10000, 20000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0,256)), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0,256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0,2)), Convert.ToBoolean(rnd.Next(0, 2))); Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0,2)), Convert.ToBoolean(rnd.Next(0, 2)));

58
Warship/Warship/SeaMap.cs Normal file
View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AircraftCarrier
{
internal class SeaMap : AbstractMap
{
/// <summary>
/// Цвет участка закрытого
/// </summary>
private readonly Brush barrierColor = new SolidBrush(Color.Red);
/// <summary>
/// Цвет участка открытого
/// </summary>
private readonly Brush roadColor = new SolidBrush(Color.Aqua);
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, 105];
_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 x = _random.Next(5, 95);
int y = _random.Next(5, 95);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
_map[x - 1, y] = _barrier;
_map[x + 1, y] = _barrier;
_map[x, y - 1] = _barrier;
_map[x, y + 1] = _barrier;
counter++;
}
}
}
}
}

View File

@ -21,17 +21,15 @@ namespace AircraftCarrier
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)
{ {
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + g.FillRectangle(barrierColor, i * _size_x, j * _size_y, _size_x, _size_y);
1), j * (_size_y + 1));
} }
protected override void DrawRoadPart(Graphics g, int i, int j) protected override void DrawRoadPart(Graphics g, int i, int j)
{ {
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + g.FillRectangle(roadColor, i * _size_x, j * _size_y, _size_x, _size_y);
1), j * (_size_y + 1));
} }
protected override void GenerateMap() protected override void GenerateMap()
{ {
_map = new int[100, 100]; _map = new int[100, 105];
_size_x = (float)_width / _map.GetLength(0); _size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1); _size_y = (float)_height / _map.GetLength(1);
int counter = 0; int counter = 0;