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