3 лаба

This commit is contained in:
Казначеева Елизавета 2023-10-25 07:54:01 +04:00
parent b05fca00fe
commit 1fdfd6ec66
5 changed files with 83 additions and 63 deletions

View File

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

View File

@ -29,11 +29,11 @@
private void InitializeComponent() private void InitializeComponent()
{ {
this.groupBoxBattleShip = new System.Windows.Forms.GroupBox(); this.groupBoxBattleShip = new System.Windows.Forms.GroupBox();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.buttonAddShip = new System.Windows.Forms.Button();
this.buttonRemoveShip = new System.Windows.Forms.Button();
this.buttonRefreshCollection = new System.Windows.Forms.Button(); this.buttonRefreshCollection = new System.Windows.Forms.Button();
this.maskedTextBoxNumber = new System.Windows.Forms.TextBox(); this.buttonRemoveShip = new System.Windows.Forms.Button();
this.buttonAddShip = new System.Windows.Forms.Button();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.groupBoxBattleShip.SuspendLayout(); this.groupBoxBattleShip.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
@ -53,36 +53,12 @@
this.groupBoxBattleShip.TabStop = false; this.groupBoxBattleShip.TabStop = false;
this.groupBoxBattleShip.Text = "Инструменты"; this.groupBoxBattleShip.Text = "Инструменты";
// //
// pictureBoxCollection // maskedTextBoxNumber
// //
this.pictureBoxCollection.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) this.maskedTextBoxNumber.Location = new System.Drawing.Point(22, 152);
| System.Windows.Forms.AnchorStyles.Left) this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
| System.Windows.Forms.AnchorStyles.Right))); this.maskedTextBoxNumber.Size = new System.Drawing.Size(147, 23);
this.pictureBoxCollection.Location = new System.Drawing.Point(-1, 1); this.maskedTextBoxNumber.TabIndex = 3;
this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(613, 450);
this.pictureBoxCollection.TabIndex = 0;
this.pictureBoxCollection.TabStop = false;
//
// 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);
//
// 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);
// //
// buttonRefreshCollection // buttonRefreshCollection
// //
@ -94,12 +70,36 @@
this.buttonRefreshCollection.UseVisualStyleBackColor = true; this.buttonRefreshCollection.UseVisualStyleBackColor = true;
this.buttonRefreshCollection.Click += new System.EventHandler(this.buttonRefreshCollection_Click); this.buttonRefreshCollection.Click += new System.EventHandler(this.buttonRefreshCollection_Click);
// //
// maskedTextBoxNumber // buttonRemoveShip
// //
this.maskedTextBoxNumber.Location = new System.Drawing.Point(22, 166); this.buttonRemoveShip.Location = new System.Drawing.Point(22, 217);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.buttonRemoveShip.Name = "buttonRemoveShip";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(147, 23); this.buttonRemoveShip.Size = new System.Drawing.Size(147, 33);
this.maskedTextBoxNumber.TabIndex = 3; 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 // FormShipCollection
// //
@ -120,10 +120,10 @@
#endregion #endregion
private GroupBox groupBoxBattleShip; private GroupBox groupBoxBattleShip;
private TextBox maskedTextBoxNumber;
private Button buttonRefreshCollection; private Button buttonRefreshCollection;
private Button buttonRemoveShip; private Button buttonRemoveShip;
private Button buttonAddShip; private Button buttonAddShip;
private PictureBox pictureBoxCollection; private PictureBox pictureBoxCollection;
private MaskedTextBox maskedTextBoxNumber;
} }
} }

View File

@ -37,7 +37,7 @@ namespace Battleship
FormBattleship form = new(); FormBattleship form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (_ships + form.SelectedShip) if (_ships + form.SelectedShip != -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _ships.ShowShips(); pictureBoxCollection.Image = _ships.ShowShips();
@ -52,7 +52,7 @@ namespace Battleship
private void buttonRemoveShip_Click(object sender, EventArgs e) private void buttonRemoveShip_Click(object sender, EventArgs e)
{ {
if (MessageBox.Show("Удалить объект?", "Удаление", if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{ {
return; return;
} }
@ -72,5 +72,6 @@ namespace Battleship
{ {
pictureBoxCollection.Image = _ships.ShowShips(); pictureBoxCollection.Image = _ships.ShowShips();
} }
} }
} }

View File

@ -34,10 +34,12 @@ namespace Battleship.Generics
/// </summary> /// </summary>
/// <param name="car">Добавляемый автомобиль</param> /// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns> /// <returns></returns>
public bool Insert(T car) public int Insert(T ship)
{ {
// TODO вставка в начало набора // TODO вставка в начало набора
return true; if (_places[Count - 1] != null)
return -1;
return Insert(ship,0);
} }
/// <summary> /// <summary>
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
@ -45,14 +47,27 @@ namespace Battleship.Generics
/// <param name="car">Добавляемый автомобиль</param> /// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param> /// <param name="position">Позиция</param>
/// <returns></returns> /// <returns></returns>
public bool Insert(T car, int position) public int Insert(T ship, int position)
{ {
// TODO проверка позиции // TODO проверка позиции
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то // TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// проверка, что после вставляемого элемента в массиве есть пустой элемент // проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции_places[position] = car; // TODO вставка по позиции_places[position] = car;
return true; 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>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
@ -63,7 +78,10 @@ namespace Battleship.Generics
{ {
// TODO проверка позиции // TODO проверка позиции
// TODO удаление объекта из массива, присвоив элементу массива значение null // TODO удаление объекта из массива, присвоив элементу массива значение null
return true; if (!(position >= 0 && position < Count) || _places[position] == null)
return false;
_places[position] = null;
return true;
} }
/// <summary> /// <summary>
/// Получение объекта из набора по позиции /// Получение объекта из набора по позиции
@ -73,6 +91,8 @@ namespace Battleship.Generics
public T? Get(int position) public T? Get(int position)
{ {
// TODO проверка позиции // TODO проверка позиции
if (!(position >= 0 && position < Count))
return null;
return _places[position]; return _places[position];
} }
} }

View File

@ -14,8 +14,8 @@ namespace Battleship.Generics
{ {
private readonly int _pictureWidth; private readonly int _pictureWidth;
private readonly int _pictureHeight; private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 175; private readonly int _placeSizeWidth = 15;
private readonly int _placeSizeHeight = 80; private readonly int _placeSizeHeight = 95;
private readonly SetGeneric<T> _collection; private readonly SetGeneric<T> _collection;
public ShipGenericCollection(int picWidth, int picHeight) public ShipGenericCollection(int picWidth, int picHeight)
{ {
@ -25,13 +25,11 @@ namespace Battleship.Generics
_pictureHeight = picHeight; _pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height); _collection = new SetGeneric<T>(width * height);
} }
public static bool operator +(ShipGenericCollection<T, U>? collect, T? obj) public static int operator +(ShipGenericCollection<T, U>? collect, T? obj)
{ {
if (obj == null) if (obj != null && collect != null)
{ return collect._collection.Insert(obj);
return false; return -1;
}
return collect?._collection.Insert(obj) ?? false;
} }
public static bool operator -(ShipGenericCollection<T, U>? collect, int pos) public static bool operator -(ShipGenericCollection<T, U>? collect, int pos)
{ {
@ -74,9 +72,13 @@ namespace Battleship.Generics
{ {
for (int i = 0; i < _collection.Count; i++) for (int i = 0; i < _collection.Count; i++)
{ {
// TODO получение объекта DrawningShip? ship = _collection.Get(i);
// TODO установка позиции if(ship != null)
// TODO прорисовка объекта {
int inRow = _pictureWidth / _placeSizeWidth;
ship.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + 5);
ship.DrawTransport(g);
}
} }
} }
} }