Beispiel #1
0
        public void Visualize(int capacity, int[] weights, int[] values, int itemsCount)
        {
            if (this.currentBagSlot != null)
            {
                this.currentBagSlot.BackColor = SystemColors.Control;
            }
            if (this.firstComparedSlot != null)
            {
                this.firstComparedSlot.BackColor = SystemColors.Control;
            }
            if (this.secondComparedSlot != null)
            {
                this.secondComparedSlot.BackColor = SystemColors.Control;
            }
            if (this.currentRow == 0 || this.currentCol == 0)
            {
                this.currentBagSlot           = ControlsHelper.GetBagSlot(this.currentRow, this.currentCol, this.Controls);
                this.currentBagSlot.BackColor = Color.Red;
                this.currentBagSlot           = ControlsHelper.GetBagSlot(this.currentRow, this.currentCol, this.Controls);
                this.currentBagSlot.Text      = 0.ToString();
                this.tooltip.Text             = "When either the row or the column is 0, then the value of the items is also 0";
            }
            else if (weights[this.currentRow - 1] <= this.currentCol)
            {
                this.currentItemSlot             = ControlsHelper.GetItemSlot(this.currentRow - 1, this.Controls);
                this.currentItemSlot.BackColor   = Color.Blue;
                this.currentWeightSlot           = ControlsHelper.GetWeightSlot(this.currentRow - 1, this.Controls);
                this.currentWeightSlot.BackColor = Color.Blue;

                //TODO: Implement tooltips.
                this.firstComparedSlot            = ControlsHelper.GetBagSlot(this.currentRow - 1, this.currentCol - weights[this.currentRow - 1], this.Controls);
                this.secondComparedSlot           = ControlsHelper.GetBagSlot(this.currentRow - 1, this.currentCol, this.Controls);
                this.firstComparedSlot.BackColor  = Color.Blue;
                this.secondComparedSlot.BackColor = Color.Blue;

                int A   = values[this.currentRow - 1] + int.Parse(this.firstComparedSlot.Text);
                int B   = int.Parse(this.secondComparedSlot.Text);
                int max = Math.Max(A, B);

                this.currentBagSlot           = ControlsHelper.GetBagSlot(this.currentRow, this.currentCol, this.Controls);
                this.currentBagSlot.BackColor = Color.Red;
                this.currentBagSlot.Text      = Math.Max(A, B).ToString();
                this.tooltip.Text             = $"In this case the weight of the current item is less than or equal to the capacity of the bag. We have to now figure out what is the biggest value of items can we achieve.\r\n" +
                                                $"We take the value of the current item and compare it with itself plus the added most valuable item using the remaining capacity of the bag. \r\n" +
                                                $"In our case: the current value of the item is {this.currentItemSlot.Text}\r\n" +
                                                $"However the combined value of the current item and the one that can fit in the remaining capacity of {this.currentCol - weights[this.currentRow - 1]} is {this.firstComparedSlot.Text}.\r\n" +
                                                $"So we put {max} in the table.";
            }
            else
            {
                this.currentBagSlot           = ControlsHelper.GetBagSlot(this.currentRow, this.currentCol, this.Controls);
                this.currentBagSlot.BackColor = Color.Red;
                Control bagSlot = ControlsHelper.GetBagSlot(this.currentRow - 1, this.currentCol, this.Controls);
                this.currentBagSlot.Text = bagSlot.Text;
                this.tooltip.Text        = $"The current weight of the item is bigger than the capacity of the bag. That's why we have no other choice than to take the value from the previous row ({bagSlot.Text}).";
            }

            if (this.currentCol < capacity)
            {
                this.currentCol++;
            }
            else if (this.currentCol == capacity && this.currentRow < itemsCount)
            {
                if (this.currentItemSlot != null)
                {
                    this.currentItemSlot.BackColor = SystemColors.Control;
                }
                if (this.currentWeightSlot != null)
                {
                    this.currentWeightSlot.BackColor = SystemColors.Control;
                }
                this.currentRow++;
                this.currentCol = 0;
            }
            else if (this.currentCol == capacity && this.currentRow == itemsCount)
            {
                this.currentItemSlot           = ControlsHelper.GetBagSlot(itemsCount, capacity, this.Controls);
                this.currentItemSlot.BackColor = Color.Green;
                this.tooltip.Text = $"After iterating over all the possible combinations, the best possible scenario for us is located on the row that matches the number of items the bag can carry ({itemsCount}) and for the column we use the capacity of the bag({capacity}). This means that the best value we can get from the scenario is {currentItemSlot.Text}";
            }
        }