lab3 готово

This commit is contained in:
Zakharov_Rostislav 2023-10-12 22:17:48 +04:00
parent 8cb412d042
commit f8341e7075
10 changed files with 555 additions and 8 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectBattleship.Entities;
using ProjectBattleship.MovementStrategy;
namespace ProjectBattleship.DrawingObjects
{

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectBattleship.Entities;
using ProjectBattleship.MovementStrategy;
namespace ProjectBattleship.DrawingObjects
{
@ -41,6 +42,10 @@ namespace ProjectBattleship.DrawingObjects
/// </summary>
private readonly int _shipHeight = 50;
/// <summary>
/// Получение объекта IMoveableObject из объекта DrawingShip
/// </summary>
public IMoveableObject GetMoveableObject => new DrawingObjectShip(this);
/// <summary>
/// Конструктор и инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>

View File

@ -37,6 +37,7 @@
buttonCreateBattleship = new Button();
comboBoxStrategy = new ComboBox();
buttonStep = new Button();
buttonSelectShip = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).BeginInit();
SuspendLayout();
//
@ -142,11 +143,23 @@
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += ButtonStep_Click;
//
// buttonSelectShip
//
buttonSelectShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonSelectShip.Location = new Point(244, 419);
buttonSelectShip.Name = "buttonSelectShip";
buttonSelectShip.Size = new Size(115, 30);
buttonSelectShip.TabIndex = 9;
buttonSelectShip.Text = "Выбрать корабль";
buttonSelectShip.UseVisualStyleBackColor = true;
buttonSelectShip.Click += ButtonSelectShip_Click;
//
// FormBattleship
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
Controls.Add(buttonSelectShip);
Controls.Add(buttonStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateBattleship);
@ -176,5 +189,6 @@
private Button buttonCreateBattleship;
private ComboBox comboBoxStrategy;
private Button buttonStep;
private Button buttonSelectShip;
}
}

View File

@ -17,6 +17,10 @@ namespace ProjectBattleship
/// </summary>
private AbstractStrategy? _abstractStrategy;
/// <summary>
/// Âûáðàííûé êîðàáëü
/// </summary>
public DrawingShip? SelectedShip { get; private set; }
/// <summary>
/// Èíèöèàëèçàöèÿ ôîðìû
/// </summary>
public FormBattleship()
@ -44,10 +48,27 @@ namespace ProjectBattleship
/// <param name="e"></param>
private void ButtonCreateBattleship_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawingShip = new DrawingBattleship(random.Next(100, 300), (double)random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxBattleship.Width, pictureBoxBattleship.Height);
_drawingShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
Random random = new();
Color bodyColor = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
bodyColor = dialog.Color;
}
Color additColor = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
if (dialog.ShowDialog() == DialogResult.OK)
{
additColor = dialog.Color;
}
_drawingShip = new DrawingBattleship(random.Next(100, 300),
random.Next(1000, 3000), bodyColor,
additColor, Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxBattleship.Width, pictureBoxBattleship.Height);
_drawingShip.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
/// <summary>
@ -57,10 +78,19 @@ namespace ProjectBattleship
/// <param name="e"></param>
private void ButtonCreateShip_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawingShip = new DrawingShip(random.Next(100, 300), (double)random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Random random = new();
Color color = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
_drawingShip = new DrawingShip(random.Next(100, 300),
random.Next(1000, 3000), color,
pictureBoxBattleship.Width, pictureBoxBattleship.Height);
_drawingShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
_drawingShip.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
/// <summary>
@ -133,5 +163,15 @@ namespace ProjectBattleship
_abstractStrategy = null;
}
}
/// <summary>
/// Âûáîð êîðàáëÿ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSelectShip_Click(object sender, EventArgs e)
{
SelectedShip = _drawingShip;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -0,0 +1,126 @@
namespace ProjectBattleship
{
partial class FormShipsCollection
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
groupBoxBattleship = new GroupBox();
buttonRefreshCollection = new Button();
buttonRemoveShip = new Button();
maskedTextBoxNumber = new MaskedTextBox();
buttonAddShip = new Button();
pictureBoxCollection = new PictureBox();
groupBoxBattleship.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
// groupBoxBattleship
//
groupBoxBattleship.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
groupBoxBattleship.Controls.Add(buttonRefreshCollection);
groupBoxBattleship.Controls.Add(buttonRemoveShip);
groupBoxBattleship.Controls.Add(maskedTextBoxNumber);
groupBoxBattleship.Controls.Add(buttonAddShip);
groupBoxBattleship.Location = new Point(719, 0);
groupBoxBattleship.Name = "groupBoxBattleship";
groupBoxBattleship.Size = new Size(164, 461);
groupBoxBattleship.TabIndex = 0;
groupBoxBattleship.TabStop = false;
groupBoxBattleship.Text = "Инструменты";
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(6, 302);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(140, 30);
buttonRefreshCollection.TabIndex = 3;
buttonRefreshCollection.Text = "Обновить коллекцию";
buttonRefreshCollection.UseVisualStyleBackColor = true;
buttonRefreshCollection.Click += ButtonRefreshCollection_Click;
//
// buttonRemoveShip
//
buttonRemoveShip.Location = new Point(6, 245);
buttonRemoveShip.Name = "buttonRemoveShip";
buttonRemoveShip.Size = new Size(140, 30);
buttonRemoveShip.TabIndex = 2;
buttonRemoveShip.Text = "Удалить корабль";
buttonRemoveShip.UseVisualStyleBackColor = true;
buttonRemoveShip.Click += ButtonRemoveShip_Click;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(6, 150);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(140, 23);
maskedTextBoxNumber.TabIndex = 1;
//
// buttonAddShip
//
buttonAddShip.Location = new Point(7, 22);
buttonAddShip.Name = "buttonAddShip";
buttonAddShip.Size = new Size(140, 30);
buttonAddShip.TabIndex = 0;
buttonAddShip.Text = "Добавить корабль";
buttonAddShip.UseVisualStyleBackColor = true;
buttonAddShip.Click += ButtonAddShip_Click;
//
// pictureBoxCollection
//
pictureBoxCollection.Dock = DockStyle.Left;
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(713, 461);
pictureBoxCollection.TabIndex = 4;
pictureBoxCollection.TabStop = false;
//
// FormShipsCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
Controls.Add(groupBoxBattleship);
Controls.Add(pictureBoxCollection);
Name = "FormShipsCollection";
StartPosition = FormStartPosition.CenterScreen;
Text = "Набор кораблей";
groupBoxBattleship.ResumeLayout(false);
groupBoxBattleship.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
#endregion
private GroupBox groupBoxBattleship;
private Button buttonAddShip;
private MaskedTextBox maskedTextBoxNumber;
private Button buttonRefreshCollection;
private Button buttonRemoveShip;
private PictureBox pictureBoxCollection;
}
}

View File

@ -0,0 +1,88 @@
using ProjectBattleship.DrawingObjects;
using ProjectBattleship.Generics;
using ProjectBattleship.MovementStrategy;
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 ProjectBattleship
{
/// <summary>
/// Форма для работы с набором объектов класса DrawingCarShip
/// </summary>
public partial class FormShipsCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
private readonly ShipsGenericCollection<DrawingShip, DrawingObjectShip> _ships;
/// <summary>
/// Конструктор
/// </summary>
public FormShipsCollection()
{
InitializeComponent();
_ships = new ShipsGenericCollection<DrawingShip, DrawingObjectShip>(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddShip_Click(object sender, EventArgs e)
{
FormBattleship form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_ships + form.SelectedShip != null)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _ships.ShowShips();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveShip_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_ships - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _ships.ShowShips();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs
e)
{
pictureBoxCollection.Image = _ships.ShowShips();
}
}
}

View File

@ -0,0 +1,120 @@
<?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.
-->
<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">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -11,7 +11,7 @@ namespace ProjectBattleship
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormBattleship());
Application.Run(new FormShipsCollection());
}
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectBattleship.Generics
{
internal class SetGeneric<T>
where T : class
{
private readonly T?[] _places;
public int Count => _places.Length;
public SetGeneric(int count)
{
_places = new T?[count];
}
public bool Insert(T ship)
{
if (_places[Count - 1] != null)
return false;
return Insert(ship, 0);
}
public bool Insert(T ship, int position)
{
if (!(position >= 0 && position < Count))
return false;
if (_places[position] != null)
{
int ind = position;
while (ind < Count && _places[ind] != null)
ind++;
if (ind == Count)
return false;
for (int i = ind - 1; i >= position; i--)
_places[i + 1] = _places[i];
}
_places[position] = ship;
return true;
}
public bool Remove(int position)
{
if (!(position >= 0 && position < Count) || _places[position] == null)
return false;
_places[position] = null;
return true;
}
public T? Get(int position)
{
if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
}
}

View File

@ -0,0 +1,94 @@
using ProjectBattleship.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectBattleship.DrawingObjects;
using System.Drawing;
namespace ProjectBattleship.Generics
{
internal class ShipsGenericCollection<T,U>
where T :DrawingShip
where U: IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 150;
private readonly int _placeSizeHeight = 50;
private readonly SetGeneric<T> _collection;
public ShipsGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static ShipsGenericCollection<T, U>? operator +(ShipsGenericCollection<T, U>? collect, T? obj)
{
if (obj != null)
collect?._collection.Insert(obj);
return collect;
}
public static ShipsGenericCollection<T, U>? operator -(ShipsGenericCollection<T, U>? collect, int pos)
{
T? obj = collect?._collection.Get(pos);
if (obj != null)
collect?._collection.Remove(pos);
return collect;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
public Bitmap ShowShips()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight +
1; ++j)
{
g.DrawLine(pen, i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i *
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
{
DrawingShip? ship = _collection.Get(i);
if (ship != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
ship.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight );
ship.DrawTransport(g);
}
}
}
}
}