latest update

This commit is contained in:
asakky 2024-05-29 02:26:53 +04:00
parent 6f18d1f7cc
commit f8ace986b0
5 changed files with 96 additions and 13 deletions

View File

@ -13,7 +13,7 @@ public class KnapsackState
public double CurrentWeight { get; } public double CurrentWeight { get; }
public double CurrentValue { get; } public double CurrentValue { get; }
public int CurrentIndex { 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) public KnapsackState(List<Item> items, double currentWeight, double currentValue, int currentIndex, int capacity)
{ {
@ -21,6 +21,6 @@ public class KnapsackState
CurrentWeight = currentWeight; CurrentWeight = currentWeight;
CurrentValue = currentValue; CurrentValue = currentValue;
CurrentIndex = currentIndex; CurrentIndex = currentIndex;
Capacity = capacity; // Инициализируем вместимость рюкзака Capacity = capacity;
} }
} }

View File

@ -11,9 +11,9 @@ namespace ProjectKnapsack.classes;
public class KnapsackVisualizer 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); Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
@ -29,8 +29,8 @@ public class KnapsackVisualizer
graphics.FillRectangle(Brushes.LightGray, knapsackX, knapsackY, knapsackWidth, knapsackHeight); graphics.FillRectangle(Brushes.LightGray, knapsackX, knapsackY, knapsackWidth, knapsackHeight);
graphics.DrawRectangle(Pens.Black, 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); 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;
} }
} }

View File

@ -43,9 +43,4 @@ public class Storage
var json = File.ReadAllText(filename); var json = File.ReadAllText(filename);
states = JsonSerializer.Deserialize<List<KnapsackState>>(json); states = JsonSerializer.Deserialize<List<KnapsackState>>(json);
} }
public void Reset()
{
currentPosition = 0;
}
} }

View File

@ -36,6 +36,8 @@ namespace ProjectKnapsack.forms
private ToolStripMenuItem loadMenuItem; private ToolStripMenuItem loadMenuItem;
private ToolStripMenuItem helpMenuItem; private ToolStripMenuItem helpMenuItem;
private ToolStripMenuItem aboutMenuItem; private ToolStripMenuItem aboutMenuItem;
private System.Windows.Forms.Button nextStepButton;
private System.Windows.Forms.Button prevStepButton;
private void InitializeComponent() private void InitializeComponent()
{ {
@ -47,6 +49,8 @@ namespace ProjectKnapsack.forms
helpMenuItem = new ToolStripMenuItem(); helpMenuItem = new ToolStripMenuItem();
aboutMenuItem = new ToolStripMenuItem(); aboutMenuItem = new ToolStripMenuItem();
pictureBox1 = new PictureBox(); pictureBox1 = new PictureBox();
nextStepButton = new System.Windows.Forms.Button();
prevStepButton = new System.Windows.Forms.Button();
menuStrip.SuspendLayout(); menuStrip.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
SuspendLayout(); SuspendLayout();
@ -111,9 +115,29 @@ namespace ProjectKnapsack.forms
pictureBox1.TabIndex = 3; pictureBox1.TabIndex = 3;
pictureBox1.TabStop = false; 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 // MainForm
// //
ClientSize = new Size(800, 600); ClientSize = new Size(800, 600);
Controls.Add(nextStepButton);
Controls.Add(prevStepButton);
Controls.Add(pictureBox1); Controls.Add(pictureBox1);
Controls.Add(startButton); Controls.Add(startButton);
Controls.Add(menuStrip); Controls.Add(menuStrip);

View File

@ -8,6 +8,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using static ProjectKnapsack.classes.KnapsackParameters;
namespace ProjectKnapsack.forms; namespace ProjectKnapsack.forms;
@ -17,11 +18,13 @@ public partial class MainForm : Form
private KnapsackManager manager; private KnapsackManager manager;
private Storage storage; private Storage storage;
private int currentStep; private int currentStep;
private List<Item> items; // Добавление списка предметов
public MainForm() public MainForm()
{ {
InitializeComponent(); InitializeComponent();
visualization = new KnapsackVisualizer(); visualization = new KnapsackVisualizer();
items = new List<Item>(); // Инициализация списка предметов
} }
private void startButton_Click(object sender, EventArgs e) private void startButton_Click(object sender, EventArgs e)
@ -36,6 +39,7 @@ public partial class MainForm : Form
manager.Execute(); manager.Execute();
storage = manager.Storage; storage = manager.Storage;
currentStep = 0; currentStep = 0;
items = parameters.Items.ToList(); // Инициализация списка предметов из параметров
DisplayCurrentState(); DisplayCurrentState();
} }
else else
@ -47,12 +51,24 @@ public partial class MainForm : Form
private void DisplayCurrentState() 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) if (storage != null && visualization != null)
{ {
var state = storage.GetState(currentStep); var state = storage.GetState(currentStep);
if (state != null) if (state != null)
{ {
visualization.Visualize(state.Items, state.Capacity, this); visualization.Visualize(state.Items, state.Capacity, state.CurrentWeight, state.CurrentValue, this);
} }
else else
{ {
@ -97,4 +113,52 @@ public partial class MainForm : Form
InformationForm informationForm = new InformationForm(); InformationForm informationForm = new InformationForm();
informationForm.Show(); 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();
}
}
}
} }