From 7156ff90951240d3b83ae89a960cd73bace08979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Tue, 4 Oct 2022 22:41:50 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB=20=D1=84?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D1=83.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/AbstractMap.cs | 87 ++++++++++++++++ WarmlyShip/WarmlyShip/FormMap.Designer.cs | 51 +++++++++ WarmlyShip/WarmlyShip/FormMap.cs | 20 ++++ WarmlyShip/WarmlyShip/FormMap.resx | 120 ++++++++++++++++++++++ WarmlyShip/WarmlyShip/SimpleMap.cs | 50 +++++++++ 5 files changed, 328 insertions(+) create mode 100644 WarmlyShip/WarmlyShip/AbstractMap.cs create mode 100644 WarmlyShip/WarmlyShip/FormMap.Designer.cs create mode 100644 WarmlyShip/WarmlyShip/FormMap.cs create mode 100644 WarmlyShip/WarmlyShip/FormMap.resx create mode 100644 WarmlyShip/WarmlyShip/SimpleMap.cs diff --git a/WarmlyShip/WarmlyShip/AbstractMap.cs b/WarmlyShip/WarmlyShip/AbstractMap.cs new file mode 100644 index 0000000..b488da9 --- /dev/null +++ b/WarmlyShip/WarmlyShip/AbstractMap.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip +{ + internal abstract class AbstractMap + { + private IDrawningObject _drawningObject = null; + + protected int[,] _map = null; + protected int _width; + protected int _height; + protected float _size_x; + protected float _size_y; + protected readonly Random _random = new(); + protected readonly int _freeRoad = 0; + protected readonly int _barrier = 1; + + public Bitmap CreateMap(int width, int height, IDrawningObject drawningObject) + { + _width = width; + _height = height; + _drawningObject = drawningObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + + public Bitmap MoveObject(Direction direction) + { + // TODO проверка, что объект может переместится в требуемомнаправлении + if (true) + { + _drawningObject.MoveObject(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); + // TODO првоерка, что объект не "накладывается" на закрытые участки + return true; + } + 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); + } + +} +} diff --git a/WarmlyShip/WarmlyShip/FormMap.Designer.cs b/WarmlyShip/WarmlyShip/FormMap.Designer.cs new file mode 100644 index 0000000..820357b --- /dev/null +++ b/WarmlyShip/WarmlyShip/FormMap.Designer.cs @@ -0,0 +1,51 @@ +namespace WarmlyShip +{ + partial class FormMap + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "FormMap"; + } + + #endregion + + private PictureBox pictureBox; + private StatusStrip statusStrip; + private ToolStripStatusLabel toolStripStatusSpeed; + private ToolStripStatusLabel toolStripStatusWeight; + private ToolStripStatusLabel toolStripStatusLabelBodyColor; + private Button buttonRight; + private Button buttonUp; + private Button buttonLeft; + private Button buttonDown; + private Button ButtonCreate; + private Button buttonCreateModif; + } +} \ No newline at end of file diff --git a/WarmlyShip/WarmlyShip/FormMap.cs b/WarmlyShip/WarmlyShip/FormMap.cs new file mode 100644 index 0000000..1463ce6 --- /dev/null +++ b/WarmlyShip/WarmlyShip/FormMap.cs @@ -0,0 +1,20 @@ +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 WarmlyShip +{ + public partial class FormMap : Form + { + public FormMap() + { + InitializeComponent(); + } + } +} diff --git a/WarmlyShip/WarmlyShip/FormMap.resx b/WarmlyShip/WarmlyShip/FormMap.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/WarmlyShip/WarmlyShip/FormMap.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/WarmlyShip/WarmlyShip/SimpleMap.cs b/WarmlyShip/WarmlyShip/SimpleMap.cs new file mode 100644 index 0000000..af1828d --- /dev/null +++ b/WarmlyShip/WarmlyShip/SimpleMap.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip +{ + 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++; + } + } + } + } +}