latest update
This commit is contained in:
parent
6f18d1f7cc
commit
f8ace986b0
@ -13,7 +13,7 @@ public class KnapsackState
|
||||
public double CurrentWeight { get; }
|
||||
public double CurrentValue { get; }
|
||||
public int CurrentIndex { get; }
|
||||
public int Capacity { get; set; } // Добавляем свойство для хранения вместимости рюкзака
|
||||
public int Capacity { get; set; }
|
||||
|
||||
public KnapsackState(List<Item> items, double currentWeight, double currentValue, int currentIndex, int capacity)
|
||||
{
|
||||
@ -21,6 +21,6 @@ public class KnapsackState
|
||||
CurrentWeight = currentWeight;
|
||||
CurrentValue = currentValue;
|
||||
CurrentIndex = currentIndex;
|
||||
Capacity = capacity; // Инициализируем вместимость рюкзака
|
||||
Capacity = capacity;
|
||||
}
|
||||
}
|
@ -11,9 +11,9 @@ namespace ProjectKnapsack.classes;
|
||||
|
||||
public class KnapsackVisualizer
|
||||
{
|
||||
public void Visualize(List<Item> items, int capacity, MainForm form)
|
||||
public void Visualize(List<Item> items, int capacity, double currentWeight, double currentValue, MainForm form)
|
||||
{
|
||||
PictureBox? pictureBox = form.Controls["pictureBox1"] as PictureBox;// получаем ссылку на PictureBox на форме
|
||||
PictureBox? pictureBox = form.Controls["pictureBox1"] as PictureBox;
|
||||
|
||||
Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
|
||||
|
||||
@ -29,8 +29,8 @@ public class KnapsackVisualizer
|
||||
graphics.FillRectangle(Brushes.LightGray, knapsackX, knapsackY, knapsackWidth, knapsackHeight);
|
||||
graphics.DrawRectangle(Pens.Black, knapsackX, knapsackY, knapsackWidth, knapsackHeight);
|
||||
|
||||
// Добавление текста "Капацитет" рядом с рюкзаком
|
||||
string capacityText = "Capacity: " + capacity;
|
||||
// Отображение вместимости рюкзака и текущего заполнения
|
||||
string capacityText = $"Capacity: {capacity}, Current Weight: {currentWeight}/{capacity}, Current Value: {currentValue}";
|
||||
graphics.DrawString(capacityText, SystemFonts.DefaultFont, Brushes.Black, knapsackX + knapsackWidth + 10, knapsackY);
|
||||
|
||||
// Отрисовка выбранных предметов
|
||||
@ -55,7 +55,7 @@ public class KnapsackVisualizer
|
||||
}
|
||||
}
|
||||
|
||||
pictureBox.Image = bitmap; // Отображение изображения на PictureBox
|
||||
pictureBox.Image = bitmap;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -43,9 +43,4 @@ public class Storage
|
||||
var json = File.ReadAllText(filename);
|
||||
states = JsonSerializer.Deserialize<List<KnapsackState>>(json);
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
currentPosition = 0;
|
||||
}
|
||||
}
|
||||
|
24
forms/MainForm.Designer.cs
generated
24
forms/MainForm.Designer.cs
generated
@ -36,6 +36,8 @@ namespace ProjectKnapsack.forms
|
||||
private ToolStripMenuItem loadMenuItem;
|
||||
private ToolStripMenuItem helpMenuItem;
|
||||
private ToolStripMenuItem aboutMenuItem;
|
||||
private System.Windows.Forms.Button nextStepButton;
|
||||
private System.Windows.Forms.Button prevStepButton;
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
@ -47,6 +49,8 @@ namespace ProjectKnapsack.forms
|
||||
helpMenuItem = new ToolStripMenuItem();
|
||||
aboutMenuItem = new ToolStripMenuItem();
|
||||
pictureBox1 = new PictureBox();
|
||||
nextStepButton = new System.Windows.Forms.Button();
|
||||
prevStepButton = new System.Windows.Forms.Button();
|
||||
menuStrip.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
|
||||
SuspendLayout();
|
||||
@ -111,9 +115,29 @@ namespace ProjectKnapsack.forms
|
||||
pictureBox1.TabIndex = 3;
|
||||
pictureBox1.TabStop = false;
|
||||
//
|
||||
// nextStepButton
|
||||
//
|
||||
nextStepButton.Location = new Point(318, 500);
|
||||
nextStepButton.Name = "nextStepButton";
|
||||
nextStepButton.Size = new Size(120, 40);
|
||||
nextStepButton.TabIndex = 0;
|
||||
nextStepButton.Text = "Next Step";
|
||||
nextStepButton.Click += nextStepButton_Click;
|
||||
//
|
||||
// prevStepButton
|
||||
//
|
||||
prevStepButton.Location = new Point(178, 500);
|
||||
prevStepButton.Name = "prevStepButton";
|
||||
prevStepButton.Size = new Size(120, 40);
|
||||
prevStepButton.TabIndex = 1;
|
||||
prevStepButton.Text = "Previous Step";
|
||||
prevStepButton.Click += prevStepButton_Click;
|
||||
//
|
||||
// MainForm
|
||||
//
|
||||
ClientSize = new Size(800, 600);
|
||||
Controls.Add(nextStepButton);
|
||||
Controls.Add(prevStepButton);
|
||||
Controls.Add(pictureBox1);
|
||||
Controls.Add(startButton);
|
||||
Controls.Add(menuStrip);
|
||||
|
@ -8,6 +8,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using static ProjectKnapsack.classes.KnapsackParameters;
|
||||
|
||||
namespace ProjectKnapsack.forms;
|
||||
|
||||
@ -17,11 +18,13 @@ public partial class MainForm : Form
|
||||
private KnapsackManager manager;
|
||||
private Storage storage;
|
||||
private int currentStep;
|
||||
private List<Item> items; // Добавление списка предметов
|
||||
|
||||
public MainForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
visualization = new KnapsackVisualizer();
|
||||
items = new List<Item>(); // Инициализация списка предметов
|
||||
}
|
||||
|
||||
private void startButton_Click(object sender, EventArgs e)
|
||||
@ -36,6 +39,7 @@ public partial class MainForm : Form
|
||||
manager.Execute();
|
||||
storage = manager.Storage;
|
||||
currentStep = 0;
|
||||
items = parameters.Items.ToList(); // Инициализация списка предметов из параметров
|
||||
DisplayCurrentState();
|
||||
}
|
||||
else
|
||||
@ -47,12 +51,24 @@ public partial class MainForm : Form
|
||||
|
||||
private void DisplayCurrentState()
|
||||
{
|
||||
//if (storage != null && visualization != null)
|
||||
//{
|
||||
// var state = storage.GetState(currentStep);
|
||||
// if (state != null)
|
||||
// {
|
||||
// visualization.Visualize(state.Items, state.Capacity, this);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// MessageBox.Show("State is null.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
// }
|
||||
//}
|
||||
if (storage != null && visualization != null)
|
||||
{
|
||||
var state = storage.GetState(currentStep);
|
||||
if (state != null)
|
||||
{
|
||||
visualization.Visualize(state.Items, state.Capacity, this);
|
||||
visualization.Visualize(state.Items, state.Capacity, state.CurrentWeight, state.CurrentValue, this);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -97,4 +113,52 @@ public partial class MainForm : Form
|
||||
InformationForm informationForm = new InformationForm();
|
||||
informationForm.Show();
|
||||
}
|
||||
|
||||
private void nextStepButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
//if (currentStep < storage.StateCount - 1)
|
||||
//{
|
||||
// currentStep++;
|
||||
// DisplayCurrentState();
|
||||
//}
|
||||
if (currentStep < storage.StateCount - 1)
|
||||
{
|
||||
currentStep++;
|
||||
var state = storage.GetState(currentStep);
|
||||
if (state != null)
|
||||
{
|
||||
if (state.Items.Count > currentStep)
|
||||
{
|
||||
|
||||
var addedItem = state.Items[currentStep];
|
||||
items.Remove(addedItem); // Удаление добавленного предмета из списка предметов
|
||||
DisplayCurrentState();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Следующего шага нет");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private void prevStepButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
//if (currentStep > 0)
|
||||
//{
|
||||
// currentStep--;
|
||||
// DisplayCurrentState();
|
||||
//}
|
||||
if (currentStep > 0)
|
||||
{
|
||||
currentStep--;
|
||||
var state = storage.GetState(currentStep);
|
||||
if (state != null)
|
||||
{
|
||||
var addedItem = state.Items[currentStep];
|
||||
items.Add(addedItem); // Добавление предмета обратно в список предметов
|
||||
DisplayCurrentState();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user