PIbd-22 Kaznacheeva E.K. LabWork_3 #3

Closed
kaznacheeva wants to merge 5 commits from LabWork_03 into LabWork_02
11 changed files with 519 additions and 23 deletions

View File

@ -33,8 +33,7 @@ namespace Battleship.DrawningObjects
//орудийная башня
if (battleShip.Tower)
{
Brush baseBrush = new SolidBrush(Color.White);
g.FillRectangle(baseBrush, _startPosX + 108, _startPosY + 28, 15, 15);
g.FillRectangle(additionalBrush, _startPosX + 108, _startPosY + 28, 15, 15);
g.DrawRectangle(pen, _startPosX + 108, _startPosY + 28, 15, 15);
Brush gunBrush = new SolidBrush(Color.Black);
g.FillRectangle(gunBrush, _startPosX + 123, _startPosY + 32, 47, 6);
@ -43,11 +42,9 @@ namespace Battleship.DrawningObjects
//отсеки под ракеты
if (battleShip.Section)
{
Brush sectionBrush = new
SolidBrush(Color.Gray);
g.FillRectangle(sectionBrush, _startPosX + 20, _startPosY + 70, 40, 10);
g.FillRectangle(additionalBrush, _startPosX + 20, _startPosY + 70, 40, 10);
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 70, 40, 10);
g.FillRectangle(sectionBrush, _startPosX + 75, _startPosY + 70, 40, 10);
g.FillRectangle(additionalBrush, _startPosX + 75, _startPosY + 70, 40, 10);
g.DrawRectangle(pen, _startPosX + 75, _startPosY + 70, 40, 10);
}
}

View File

@ -4,9 +4,12 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Battleship.Entities;
using Battleship.MovementStrategy;
namespace Battleship.DrawningObjects
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
@ -51,6 +54,11 @@ namespace Battleship.DrawningObjects
/// <returns>true - объект создан, false - проверка не пройдена,
///нельзя создать объект в этих размерах</retu rns>
/// <summary>
/// Получение объекта IMoveableObject из объекта DrawningCar
/// </summary>
public IMoveableObject GetMoveableObject => new DrawningObjectShip(this);
public DrawningShip(int speed, double weight, Color bodyColor, int width, int height)
{
if (width < _buttleshipWidth || height < _buttleshipHeight)

View File

@ -1,6 +1,6 @@
namespace Battleship
{
partial class Battleship
partial class FormBattleship
{
/// <summary>
/// Required designer variable.
@ -37,6 +37,7 @@
this.buttonCreateAdd = new System.Windows.Forms.Button();
this.buttonStep = new System.Windows.Forms.Button();
this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
this.buttonSelectShip = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBattleship)).BeginInit();
this.SuspendLayout();
//
@ -142,11 +143,23 @@
this.comboBoxStrategy.Size = new System.Drawing.Size(121, 23);
this.comboBoxStrategy.TabIndex = 13;
//
// Battleship
// buttonSelectShip
//
this.buttonSelectShip.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonSelectShip.Location = new System.Drawing.Point(297, 415);
this.buttonSelectShip.Name = "buttonSelectShip";
this.buttonSelectShip.Size = new System.Drawing.Size(118, 23);
this.buttonSelectShip.TabIndex = 14;
this.buttonSelectShip.Text = "Выбрать корабль";
this.buttonSelectShip.UseVisualStyleBackColor = true;
this.buttonSelectShip.Click += new System.EventHandler(this.buttonSelectShip_Click);
//
// FormBattleship
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.buttonSelectShip);
this.Controls.Add(this.comboBoxStrategy);
this.Controls.Add(this.buttonStep);
this.Controls.Add(this.buttonCreateAdd);
@ -156,7 +169,7 @@
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.pictureBoxBattleship);
this.Name = "Battleship";
this.Name = "FormBattleship";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBattleship)).EndInit();
this.ResumeLayout(false);
@ -173,5 +186,6 @@
private Button buttonCreateAdd;
private Button buttonStep;
private ComboBox comboBoxStrategy;
private Button buttonSelectShip;
}
}

View File

@ -1,9 +1,10 @@
using Battleship.DrawningObjects;
using Battleship.MovementStrategy;
using System.Drawing;
namespace Battleship
{
public partial class Battleship : Form
public partial class FormBattleship : Form
{
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
@ -13,9 +14,15 @@ namespace Battleship
/// Èíèöèàëèçàöèÿ ôîðìû
/// </summary>
private AbstractStrategy? _abstractStrategy;
public Battleship()
/// <summary>
/// Âûáðàííûé êîðàáëü
/// </summary>
public DrawningShip? SelectedShip { get; private set; }
public FormBattleship()
{
InitializeComponent();
_abstractStrategy = null;
SelectedShip = null;
}
/// Ìåòîä ïðîðèñîâêè
/// </summary>
@ -31,31 +38,56 @@ namespace Battleship
_drawningShip.DrawTransport(gr);
pictureBoxBattleship.Image = bmp;
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ïðîñòîé êîðàáëü"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreate_Click(object sender, EventArgs e)
{
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;
}
_drawningShip = new DrawningShip(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
random.Next(1000, 3000), color,
pictureBoxBattleship.Width, pictureBoxBattleship.Height);
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü âîåííûé êîðàáëü"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateAdd_Click(object sender, EventArgs e)
{
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 addColor = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
if (dialog.ShowDialog() == DialogResult.OK)
{
addColor = dialog.Color;
}
_drawningShip = new DrawningBattleship(random.Next(100, 300),
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)),
random.Next(1000, 3000), bodyColor,
addColor,
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxBattleship.Width, pictureBoxBattleship.Height);
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10,100));
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
@ -120,5 +152,10 @@ namespace Battleship
_abstractStrategy = null;
}
}
private void buttonSelectShip_Click(object sender, EventArgs e)
{
SelectedShip = _drawningShip;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -0,0 +1,129 @@
namespace Battleship
{
partial class FormShipCollection
{
/// <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()
{
this.groupBoxBattleShip = new System.Windows.Forms.GroupBox();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.buttonRefreshCollection = new System.Windows.Forms.Button();
this.buttonRemoveShip = new System.Windows.Forms.Button();
this.buttonAddShip = new System.Windows.Forms.Button();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.groupBoxBattleShip.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.SuspendLayout();
//
// groupBoxBattleShip
//
this.groupBoxBattleShip.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBoxBattleShip.Controls.Add(this.maskedTextBoxNumber);
this.groupBoxBattleShip.Controls.Add(this.buttonRefreshCollection);
this.groupBoxBattleShip.Controls.Add(this.buttonRemoveShip);
this.groupBoxBattleShip.Controls.Add(this.buttonAddShip);
this.groupBoxBattleShip.Location = new System.Drawing.Point(618, 1);
this.groupBoxBattleShip.Name = "groupBoxBattleShip";
this.groupBoxBattleShip.Size = new System.Drawing.Size(183, 450);
this.groupBoxBattleShip.TabIndex = 0;
this.groupBoxBattleShip.TabStop = false;
this.groupBoxBattleShip.Text = "Инструменты";
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(22, 152);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(147, 23);
this.maskedTextBoxNumber.TabIndex = 3;
//
// buttonRefreshCollection
//
this.buttonRefreshCollection.Location = new System.Drawing.Point(22, 265);
this.buttonRefreshCollection.Name = "buttonRefreshCollection";
this.buttonRefreshCollection.Size = new System.Drawing.Size(147, 33);
this.buttonRefreshCollection.TabIndex = 2;
this.buttonRefreshCollection.Text = "Обновить коллекцию";
this.buttonRefreshCollection.UseVisualStyleBackColor = true;
this.buttonRefreshCollection.Click += new System.EventHandler(this.buttonRefreshCollection_Click);
//
// buttonRemoveShip
//
this.buttonRemoveShip.Location = new System.Drawing.Point(22, 217);
this.buttonRemoveShip.Name = "buttonRemoveShip";
this.buttonRemoveShip.Size = new System.Drawing.Size(147, 33);
this.buttonRemoveShip.TabIndex = 1;
this.buttonRemoveShip.Text = "Удалить корабль";
this.buttonRemoveShip.UseVisualStyleBackColor = true;
this.buttonRemoveShip.Click += new System.EventHandler(this.buttonRemoveShip_Click);
//
// buttonAddShip
//
this.buttonAddShip.Location = new System.Drawing.Point(22, 22);
this.buttonAddShip.Name = "buttonAddShip";
this.buttonAddShip.Size = new System.Drawing.Size(147, 33);
this.buttonAddShip.TabIndex = 0;
this.buttonAddShip.Text = "Добавить корабль";
this.buttonAddShip.UseVisualStyleBackColor = true;
this.buttonAddShip.Click += new System.EventHandler(this.buttonAddShip_Click);
//
// pictureBoxCollection
//
this.pictureBoxCollection.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBoxCollection.Location = new System.Drawing.Point(-1, 1);
this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(613, 450);
this.pictureBoxCollection.TabIndex = 0;
this.pictureBoxCollection.TabStop = false;
//
// FormShipCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.pictureBoxCollection);
this.Controls.Add(this.groupBoxBattleShip);
this.Name = "FormShipCollection";
this.Text = "FormShipCollection";
this.groupBoxBattleShip.ResumeLayout(false);
this.groupBoxBattleShip.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.ResumeLayout(false);
}
#endregion
private GroupBox groupBoxBattleShip;
private Button buttonRefreshCollection;
private Button buttonRemoveShip;
private Button buttonAddShip;
private PictureBox pictureBoxCollection;
private MaskedTextBox maskedTextBoxNumber;
}
}

View File

@ -0,0 +1,77 @@
using Battleship.DrawningObjects;
using Battleship.Generics;
using Battleship.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 Battleship
{
public partial class FormShipCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
private readonly ShipGenericCollection<DrawningShip,DrawningObjectShip> _ships;
/// <summary>
/// Конструктор
/// </summary>
public FormShipCollection()
{
InitializeComponent();
_ships = new ShipGenericCollection<DrawningShip, DrawningObjectShip>(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 != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _ships.ShowShips();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
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("Не удалось удалить объект");
}
}
private void buttonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _ships.ShowShips();
}
}
}

View File

@ -0,0 +1,60 @@
<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">
<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 Battleship
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Battleship());
Application.Run(new FormShipCollection());
}
}
}

View File

@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship.Generics
{
internal class SetGeneric<T>
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T?[] _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T?[count];
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public int Insert(T ship)
{
if (_places[Count - 1] != null)
return -1;
return Insert(ship,0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// /// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T ship, int position)
{
if (!(position >= 0 && position < Count))
return -1;
if (_places[position] != null)
{
int ind = position;
while (ind < Count && _places[ind] != null)
ind++;
if (ind == Count)
return -1;
for (int i = ind - 1; i >= position; i--)
_places[i + 1] = _places[i];
}
_places[position] = ship;
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (!(position >= 0 && position < Count) || _places[position] == null)
return false;
_places[position] = null;
return true;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
{
if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
}
}

View File

@ -0,0 +1,86 @@
using Battleship.DrawningObjects;
using Battleship.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship.Generics
{
internal class ShipGenericCollection<T, U>
where T : DrawningShip
where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 185;
private readonly int _placeSizeHeight = 90;
private readonly SetGeneric<T> _collection;
public ShipGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(ShipGenericCollection<T, U>? collect, T? obj)
{
if (obj != null && collect != null)
return collect._collection.Insert(obj);
return -1;
}
public static bool operator -(ShipGenericCollection<T, U>? collect, int pos)
{
T? obj = collect?._collection.Get(pos);
if (obj != null && collect != null)
{
return collect._collection.Remove(pos);
}
return false;
}
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++)
{
DrawningShip? ship = _collection.Get(i);
if(ship != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
ship.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + 5);
ship.DrawTransport(g);
}
}
}
}
}