bool broblem

This commit is contained in:
Александр Разживин 2022-10-10 03:13:45 +04:00
parent 5ae5e17d51
commit efa7099a7a
6 changed files with 305 additions and 88 deletions

View File

@ -55,7 +55,6 @@ namespace Stormtrooper
}
public Bitmap MoveObject(Direction direction)
{
// TODO проверка, что объект может переместится в требуемом направлении
bool place = false;
switch (direction)
{
@ -87,7 +86,6 @@ namespace Stormtrooper
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawningObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки
while (!CheckEnoughPlace(0, 0))
{
x += 10;

View File

@ -28,12 +28,124 @@
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonRemoveStormtrooper = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddStormtrooper = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.buttonShowOnMap);
this.groupBox1.Controls.Add(this.buttonShowStorage);
this.groupBox1.Controls.Add(this.buttonRemoveStormtrooper);
this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.buttonAddStormtrooper);
this.groupBox1.Controls.Add(this.comboBoxSelectorMap);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(550, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(250, 450);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(41, 344);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(142, 29);
this.buttonShowOnMap.TabIndex = 5;
this.buttonShowOnMap.Text = "Посмотреть карту";
this.buttonShowOnMap.UseVisualStyleBackColor = true;
this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(41, 250);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(185, 29);
this.buttonShowStorage.TabIndex = 4;
this.buttonShowStorage.Text = "Посмотреть хранилище";
this.buttonShowStorage.UseVisualStyleBackColor = true;
this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
//
// buttonRemoveStormtrooper
//
this.buttonRemoveStormtrooper.Location = new System.Drawing.Point(41, 194);
this.buttonRemoveStormtrooper.Name = "buttonRemoveStormtrooper";
this.buttonRemoveStormtrooper.Size = new System.Drawing.Size(94, 29);
this.buttonRemoveStormtrooper.TabIndex = 3;
this.buttonRemoveStormtrooper.Text = "Удалить";
this.buttonRemoveStormtrooper.UseVisualStyleBackColor = true;
this.buttonRemoveStormtrooper.Click += new System.EventHandler(this.ButtonRemoveStormtrooper_Click);
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(41, 146);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(125, 27);
this.maskedTextBoxPosition.TabIndex = 2;
//
// buttonAddStormtrooper
//
this.buttonAddStormtrooper.Location = new System.Drawing.Point(41, 91);
this.buttonAddStormtrooper.Name = "buttonAddStormtrooper";
this.buttonAddStormtrooper.Size = new System.Drawing.Size(94, 29);
this.buttonAddStormtrooper.TabIndex = 1;
this.buttonAddStormtrooper.Text = "Добавить";
this.buttonAddStormtrooper.UseVisualStyleBackColor = true;
this.buttonAddStormtrooper.Click += new System.EventHandler(this.ButtonAddStormtrooper_Click);
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Location = new System.Drawing.Point(41, 35);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(151, 28);
this.comboBoxSelectorMap.TabIndex = 0;
//
// 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(550, 450);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// FormMapWithSetStormtroopers
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Name = "FormMapWithSetStormtroopers";
this.Text = "FormMapWithSetStormtroopers";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
private GroupBox groupBox1;
private Button buttonShowOnMap;
private Button buttonShowStorage;
private Button buttonRemoveStormtrooper;
private MaskedTextBox maskedTextBoxPosition;
private Button buttonAddStormtrooper;
private ComboBox comboBoxSelectorMap;
private PictureBox pictureBox;
}
}

View File

@ -7,14 +7,159 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.DataFormats;
namespace Stormtrooper
{
public partial class FormMapWithSetStormtroopers : Form
{
/// <summary>
/// Объект от класса карты с набором объектов
/// </summary>
private MapWithSetStormtroopersGeneric<DrawningObjectStormtrooper, AbstractMap> _mapCarsCollectionGeneric;
/// <summary>
/// Конструктор
/// </summary>
public FormMapWithSetStormtroopers()
{
InitializeComponent();
}
/// <summary>
/// Выбор карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender,
EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
}
if (map != null)
{
_mapCarsCollectionGeneric = new
MapWithSetStormtroopersGeneric<DrawningObjectStormtrooper, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapCarsCollectionGeneric = null;
}
}
/// <summary>
/// Добавление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddStormtrooper_Click(object sender, EventArgs e)
{
if (_mapCarsCollectionGeneric == null)
{
return;
}
FormStormtrooper form = new();
if (form.ShowDialog() == DialogResult.OK)
{
DrawningObjectStormtrooper car = new(form.SelectedStormtrooper);
if (_mapCarsCollectionGeneric + car)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapCarsCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
/// <summary>
/// Удаление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveStormtrooper_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapCarsCollectionGeneric - pos)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapCarsCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Вывод набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapCarsCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapCarsCollectionGeneric.ShowSet();
}
/// <summary>
/// Вывод карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapCarsCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapCarsCollectionGeneric.ShowOnMap();
}
/// <summary>
/// Перемещение
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapCarsCollectionGeneric == null)
{
return;
}
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
dir = Direction.Up;
break;
case "buttonDown":
dir = Direction.Down;
break;
case "buttonLeft":
dir = Direction.Left;
break;
case "buttonRight":
dir = Direction.Right;
break;
}
pictureBox.Image = _mapCarsCollectionGeneric.MoveObject(dir);
}
}
}

View File

@ -1,64 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<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">

View File

@ -29,7 +29,7 @@ namespace Stormtrooper
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetStormtroopersGeneric<T> _setCars;
private readonly SetStormtroopersGeneric<T> _setStormtroopers;
/// <summary>
/// Карта
/// </summary>
@ -44,7 +44,7 @@ namespace Stormtrooper
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setCars = new SetStormtroopersGeneric<T>(width * height);
_setStormtroopers = new SetStormtroopersGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
@ -57,7 +57,7 @@ namespace Stormtrooper
/// <returns></returns>
public static bool operator + (MapWithSetStormtroopersGeneric<T, U> map, T car)
{
return map._setCars.Insert(car);
return map._setStormtroopers.Insert(car);
}
/// <summary>
/// Перегрузка оператора вычитания
@ -68,7 +68,7 @@ namespace Stormtrooper
public static bool operator - (MapWithSetStormtroopersGeneric<T, U> map, int
position)
{
return map._setCars.Remove(position);
return map._setStormtroopers.Remove(position);
}
/// <summary>
/// Вывод всего набора объектов
@ -89,9 +89,9 @@ namespace Stormtrooper
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setCars.Count; i++)
for (int i = 0; i < _setStormtroopers.Count; i++)
{
var car = _setCars.Get(i);
var car = _setStormtroopers.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
@ -117,18 +117,18 @@ namespace Stormtrooper
/// </summary>
private void Shaking()
{
int j = _setCars.Count - 1;
for (int i = 0; i < _setCars.Count; i++)
int j = _setStormtroopers.Count - 1;
for (int i = 0; i < _setStormtroopers.Count; i++)
{
if (_setCars.Get(i) == null)
if (_setStormtroopers.Get(i) == null)
{
for (; j > i; j--)
{
var car = _setCars.Get(j);
var car = _setStormtroopers.Get(j);
if (car != null)
{
_setCars.Insert(car, i);
_setCars.Remove(j);
_setStormtroopers.Insert(car, i);
_setStormtroopers.Remove(j);
break;
}
}
@ -163,10 +163,18 @@ namespace Stormtrooper
/// <param name="g"></param>
private void DrawCars(Graphics g)
{
for (int i = 0; i < _setCars.Count; i++)
int x = 5;
int y = 5;
for (int i = 0; i < _setStormtroopers.Count; i++)
{
// TODO установка позиции
_setCars.Get(i)?.DrawningObject(g);
_setStormtroopers.Get(i)?.SetObject(x, y, _pictureWidth, _pictureHeight);
_setStormtroopers.Get(i)?.DrawningObject(g);
x += _placeSizeWidth;
if (x + _placeSizeWidth > _pictureWidth)
{
y += _placeSizeHeight;
x = 5;
}
}
}

View File

@ -21,7 +21,7 @@ namespace Stormtrooper
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetCarsGeneric(int count)
public SetStormtroopersGeneric(int count)
{
_places = new T[count];
}
@ -30,10 +30,17 @@ namespace Stormtrooper
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public bool Insert(T car)
public int Insert(T stormtrooper)
{
// TODO вставка в начало набора
return true;
for (int i = 0; i < _places.Length; i++)
{
if (_places[i] == null)
{
_places[i] = stormtrooper;
return i;
}
}
return -1;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -45,10 +52,13 @@ namespace Stormtrooper
{
// TODO проверка позиции
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции
_places[position] = car;
// проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции
// hz kak sdelat s bool
_places[position] = car;
return true;
}
/// <summary>
@ -60,6 +70,7 @@ namespace Stormtrooper
{
// TODO проверка позиции
// TODO удаление объекта из массива, присовив элементу массива значение null
// hz kak sdelat s bool
return true;
}
/// <summary>
@ -69,7 +80,10 @@ namespace Stormtrooper
/// <returns></returns>
public T Get(int position)
{
// TODO проверка позиции
if (position < 0 || position > _places.Length)
{
return null;
}
return _places[position];
}