PIBD-14_Boyko_M_S_Cursach_DFS/Cursach/MainForm.cs

162 lines
5.0 KiB
C#
Raw Permalink Normal View History

2024-05-16 00:37:58 +04:00
using System.Text;
using Cursach.Parameters;
using Cursach.States;
namespace Cursach
{
public partial class MainForm : Form
{
private Visualizator? visualizator;
2024-05-22 20:29:11 +04:00
private StatesStorage? _manager;
2024-05-16 00:37:58 +04:00
public MainForm()
{
InitializeComponent();
visualizator = new Visualizator();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void buttonCreate_Click(object sender, EventArgs e)
{
FormCreate formCreate = new FormCreate();
formCreate.parameterDelegate += AddData;
formCreate.Show();
}
private void buttonNext_Click(object sender, EventArgs e)
{
2024-05-22 20:29:11 +04:00
if (_manager == null)
2024-05-16 00:37:58 +04:00
{
MessageBox.Show("Сначала нужно создать граф");
return;
}
2024-05-22 20:29:11 +04:00
if (_manager.NextState())
2024-05-16 00:37:58 +04:00
{
UpdateVizualization();
}
else
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("Обход графа в ширину завершен\n");
stringBuilder.Append("Результат:\n");
2024-05-22 20:29:11 +04:00
foreach (var node in _manager.GetCurrentState().visited)
2024-05-16 00:37:58 +04:00
{
stringBuilder.Append("- " + node + "\n");
}
MessageBox.Show(stringBuilder.ToString());
}
}
private void buttonPrev_Click(object sender, EventArgs e)
{
2024-05-22 20:29:11 +04:00
if (_manager == null)
2024-05-16 00:37:58 +04:00
{
MessageBox.Show("Сначала нужно создать граф");
return;
}
2024-05-22 20:29:11 +04:00
if (_manager.PrevState())
2024-05-16 00:37:58 +04:00
{
UpdateVizualization();
}
else
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("Выхода нет\n");
MessageBox.Show(stringBuilder.ToString());
}
}
private void AddData(BFSParameters paramter)
{
if (visualizator == null)
{
return;
}
2024-05-22 20:29:11 +04:00
_manager = new Manager(paramter).StartBFS();
2024-05-16 00:37:58 +04:00
MessageBox.Show("Обход графа в ширину запущен");
UpdateVizualization();
}
private void Draw()
{
2024-05-22 20:29:11 +04:00
if (visualizator == null || _manager == null || _manager.GetCurrentState() == null)
2024-05-16 00:37:58 +04:00
{
return;
}
Bitmap bmp = new(pictureBox1.Width, pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp);
2024-05-22 20:29:11 +04:00
visualizator.Draw(gr, _manager.GetCurrentState());
2024-05-16 00:37:58 +04:00
pictureBox1.Image = bmp;
}
private void SaveToolStripMenuItem1_Click(object sender, EventArgs e)
{
2024-05-22 20:29:11 +04:00
if (_manager == null)
2024-05-16 00:37:58 +04:00
{
MessageBox.Show("Сначала нужно создать граф");
return;
}
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
2024-05-22 20:29:11 +04:00
MessageBox.Show(_manager.SaveStateList(saveFileDialog.FileName)
2024-05-16 00:37:58 +04:00
? $"Список состояний успешно сохранён в {saveFileDialog.FileName}"
: $"Ошибка при сохранении файла {saveFileDialog.FileName}");
}
}
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
2024-05-22 20:29:11 +04:00
_manager ??= new StatesStorage();
2024-05-16 00:37:58 +04:00
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
2024-05-22 20:29:11 +04:00
if (_manager.LoadStateList(filePath))
2024-05-16 00:37:58 +04:00
{
UpdateVizualization();
MessageBox.Show("Файл загружен успешно");
}
else
{
MessageBox.Show("Неверный формат файла или файл поврежден");
}
}
}
private void InfoToolStripMenuItem_Click(object sender, EventArgs e)
{
InfoForm infoForm = new InfoForm();
infoForm.Show();
}
private void UpdateVizualization()
{
2024-05-22 20:29:11 +04:00
startPointLabel.Text = "Отсчёт от: " + _manager.GetStartNode();
progressBar1.Maximum = _manager.Count;
progressBar1.Value = _manager.CurrentStateIndex + 1;
2024-05-16 00:37:58 +04:00
listBox1.Items.Clear();
2024-05-22 20:29:11 +04:00
foreach (var node in _manager.GetCurrentState().queue)
2024-05-16 00:37:58 +04:00
{
listBox1.Items.Add(node.ToString());
}
listBox2.Items.Clear();
2024-05-22 20:29:11 +04:00
foreach (var node in _manager.GetCurrentState().visited)
2024-05-16 00:37:58 +04:00
{
listBox2.Items.Add(node.ToString());
}
Draw();
}
}
}