169 lines
5.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
using static System.Windows.Forms.DataFormats;
namespace Battleship
{
public partial class FormMapWithSetBattleship : Form
{
/// <summary>
/// Объект от класса карты с набором объектов
/// </summary>
private MapWithSetBattleshipGeneric<DrawningObjectBattleship, AbstractMap> _mapBattleshipCollectionGeneric;
/// <summary>
/// Конструктор
/// </summary>
public FormMapWithSetBattleship()
{
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;
}
switch (comboBoxSelectorMap.Text)
{
case "Водная карта":
map = new WaterMap();
break;
}
if (map != null)
{
_mapBattleshipCollectionGeneric = new MapWithSetBattleshipGeneric<DrawningObjectBattleship, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapBattleshipCollectionGeneric = null;
}
}
/// <summary>
/// Добавление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddBattleship_Click(object sender, EventArgs e)
{
if (_mapBattleshipCollectionGeneric == null)
{
return;
}
FormBattleship form = new();
if (form.ShowDialog() == DialogResult.OK)
{
DrawningObjectBattleship battleship = new(form.SelectedBattleship);
if (_mapBattleshipCollectionGeneric + battleship != null )
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
/// <summary>
/// Удаление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveBattleship_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 (_mapBattleshipCollectionGeneric - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Вывод набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapBattleshipCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet();
}
/// <summary>
/// Вывод карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapBattleshipCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapBattleshipCollectionGeneric.ShowOnMap();
}
/// <summary>
/// Перемещение
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapBattleshipCollectionGeneric == 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 = _mapBattleshipCollectionGeneric.MoveObject(dir);
}
}
}