From f8ace986b0df63c8b2559e36fcab0ceb57b9a999 Mon Sep 17 00:00:00 2001 From: asakky Date: Wed, 29 May 2024 02:26:53 +0400 Subject: [PATCH] latest update --- .../{KnapsackStatus.cs => KnapsackState.cs} | 4 +- classes/KnapsackVisualizer.cs | 10 +-- classes/Storage.cs | 5 -- forms/MainForm.Designer.cs | 24 +++++++ forms/MainForm.cs | 66 ++++++++++++++++++- 5 files changed, 96 insertions(+), 13 deletions(-) rename classes/{KnapsackStatus.cs => KnapsackState.cs} (74%) diff --git a/classes/KnapsackStatus.cs b/classes/KnapsackState.cs similarity index 74% rename from classes/KnapsackStatus.cs rename to classes/KnapsackState.cs index 8b3418c..403a304 100644 --- a/classes/KnapsackStatus.cs +++ b/classes/KnapsackState.cs @@ -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 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; } } diff --git a/classes/KnapsackVisualizer.cs b/classes/KnapsackVisualizer.cs index ad3f87d..54dd2a6 100644 --- a/classes/KnapsackVisualizer.cs +++ b/classes/KnapsackVisualizer.cs @@ -11,9 +11,9 @@ namespace ProjectKnapsack.classes; public class KnapsackVisualizer { - public void Visualize(List items, int capacity, MainForm form) + public void Visualize(List 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; } } diff --git a/classes/Storage.cs b/classes/Storage.cs index b4d7ce2..b622168 100644 --- a/classes/Storage.cs +++ b/classes/Storage.cs @@ -43,9 +43,4 @@ public class Storage var json = File.ReadAllText(filename); states = JsonSerializer.Deserialize>(json); } - - public void Reset() - { - currentPosition = 0; - } } diff --git a/forms/MainForm.Designer.cs b/forms/MainForm.Designer.cs index 7942360..9cc9fc8 100644 --- a/forms/MainForm.Designer.cs +++ b/forms/MainForm.Designer.cs @@ -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); diff --git a/forms/MainForm.cs b/forms/MainForm.cs index aacf969..2d97680 100644 --- a/forms/MainForm.cs +++ b/forms/MainForm.cs @@ -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 items; // Добавление списка предметов public MainForm() { InitializeComponent(); visualization = new KnapsackVisualizer(); + items = new List(); // Инициализация списка предметов } 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(); + } + } + } }