Struggling with Inventory system on C++ Game [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
So I am having problems when adding a quantity to an item. For example, the item Fist has a quantity of 1 and id 1. In the void addQuantity(int id, int quantity) function, i "did it" so when you type addQuantity(1, 1) for example, the item fist (which has rarity 1) will get quantity 2 but it just doesn't do anything. Can you guys help me fix it? I just need a working addQuantity, setQuantity and removeQuantity function.
Code: https://hastebin.com/itacijacur.cpp

The code that addQuantity executes is:
this->inventoryVec[id].quantity += quantity;
Here, you are using id as the index within inventoryVec. That's not the same as the id of the Item. In the scenario you have set up in your code, the index of the "fist" Item is actually 0, since it is the one and only Item added to the vector.
If you really want the addQuantity method to access items by their ID numbers, it would need to loop through the inventoryVec vector to search for an item whose ID number matches the id parameter of the method.

You are accessing the index of the vector as index 1, you are wanting to scan the vector and try and find the item that has the correct id, if found then you want to make the change to the item.
Change your addQuantity function to
void Inventory::addQuantity(int id, int quantity)
{
for (int i = 0; i < inventoryVec.size(); i++)
{
if (inventoryVec.at(i).getID() == id)
{
inventoryVec.at(i).quantity += quantity;
}
}
}
This will cycle through the vector and find the correct item, you were assuming the items index was equal to its ID.
Hope this helps, any questions just ask.

Related

How to return all value out the for loop in golang [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
In python I can return how value what I append in the array
valuesinPy= []
for i range(len(value)) :
valuesinPy.append(value[i])
return valuesinPy
It's able to return all value I want to append in golang
valueappend =make(map[strig]string)
for i :=range value{
valueappend['abs']=value[i]
}
return valueappend
it's all same
But the value return same I got confused
Problem fix
I use the map [string]interface{}{} I less one struct of slice
valueappend:=map[string]interface{}{}
for i :=range value{
valueappend= append(valueappend, map[string]interface{}{
"valueIwant append" value[i] :
}
}
return valueappend
One issue I can see is that the key you are using is same in all iterations of the loop
valueappend['abs']=value[i]
This will overwrite all previous values and you will only have the last one in the loop be saved. In the end you will get a map with just one value. I'm guessing that's not what you want here. Use can use a unique key for each iteration as follows, or something else along the same line
for i := range value {
valueappend[i]=value[i]
}
#范紀予 please add more info and clarify what you are trying to achieve here.
You'll probably need to change the value of the map each time you go through the loop, or else every value from value[i] will keep writing to that same field in the map, leaving with the last value of i as your only value in the map

Stepfunction map with condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is there is any possibility to run the was step function map Iterator based on the condition Am going to invoke the lambda and based on the result I have to choose the input item of the iteration meaning if there is an array of 0-3 items once the first item executes with lambda then based on the result it should execute 1st index or 2nd index item.
The other option is to create a while loop like this:
Set a variable to track your index (pos = 0)
Do whatever you want to do inside your iteration also you could use some condition, if you want to process each item conditionally
increase your index ( pos++)
If your index is less that your items count go to step1.
This way you have access to the result of previous iteration, so you can fine tune your condition.

Changing one value of row to column [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am working on a Power BI report. There are two dimensions DimWorkedClass and DimWorkedService. (The above snippet is obtained by exporting matrix values to csv.)
The requirement is to transform only the Worked Service Text5 into the Worked Class of Text5 as opposed to A (which is the current value).
It can be transformed at the backend, but is there any way to do it in Power BI?
This is trickier than it might appear, but it looks like this question has already been answered here:
Power Query Transform a Column based on Another Column
In your case, the M code would look something like this:
= Table.FromRecords(Table.TransformRows(#"[Source or Previous Step Here]",
(here) => Record.TransformFields(here, {"Worked Class",
each if here[Worked Service] = "text5" then "text5" else here[Worked Class]})))
(In the above, here represents the current row.)
Another answer points out a slightly cleaner way of doing this:
= Table.ReplaceValue(#"[Source or Previous Step Here]",
each [Worked Class],
each if [Worked Service] = "text5" then "text5" else [Worked Class],
Replacer.ReplaceText,{"Worked Class"})

Groovy sort list based off another list [duplicate]

This question already has an answer here:
Custom List sorting
(1 answer)
Closed 7 years ago.
I have an array of objects that I want to sort based off the following list:
days = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday']
def i = 0
schedule.shifts.each {
println it.dayOfWeek
println days[i]
if (it.dayOfWeek == days[i]) {
shifts.add(it)
}
}
I have tried wrapping this in a while loop and increment i in different spots, but I guess I cant seem to think about how it should be. (keep getting stuck in infinite loops). Can someone help me get this right? Ive been stuck for awhile
The sort method allows you to specify the property you want to sort on. In this case, it would be the index of the day in the days list. Try something like this:
schedule.shifts.sort { shift -> days.indexOf(shift.dayOfWeek) }

Recursive backtracking, showing the best solution

For school I am supposed to use recursive backtracking to solve a Boat puzzle. The user inputs a maximum weight for the boat, the amount of item types, and a weight and value for each item type. More than one of each item type can be placed on the boat.
Our assignment states "The program should find a solution that fills the boat with selected valuable items such that the total value of the items in the boat is maximized while the total weight of the items stays within the weight capacity of the boat."
It also has pretty specific template for the recursive backtracking algorithm.
Currently I am using contiguous lists of items to store the possible items and the items on the boat. The item struct includes int members for weight, value, count (of how many times it is used) and a unique code for printing purposes. I then have a Boat class which contains data members max_weight, current_weight, value_sum, and members for each of the contiguous lists, and then member functions needed to solve the puzzle. All of my class functions seem to be working perfectly and my recursion is indeed displaying the correct answer given the example input.
The thing I can't figure out is the condition for extra credit, which is, "Modify your program so that it displays the best solution, which has the lowest total weight. If there are two solutions with the same total weight, break the tie by selecting the solution with the least items in it." I've looked at it for awhile, but I'm just not sure how I can change it make sure the weight is minimized while also maximizing the value. Here is the code for my solution:
bool solve(Boat &boat) {
if (boat.no_more()) {
boat.print();
return true;
}
else {
int pos;
for (int i = 0; i < boat.size(); i++){
if (boat.can_place(i)) {
pos = boat.add_item(i);
bool solved = solve(boat);
boat.remove_item(pos);
if (solved) return true;
}
}
return false;
}
}
All functions do pretty much exactly what their name says. No more returns true if none of the possible items will fit on the boat. Size returns the size of the list of possible items. Adding and removing items change the item count data and also the Boat current_weight and value_sum members accordingly. Also the add_item, remove_item and can_place parameter is the index of the possible item that is being used. In order to make sure maximized value is found, the list of possible items is sorted in descending order by value in the Boat's constructor, which takes a list of possible items as a parameter.
Also here is an example of what input and output look like:
Any insight is greatly appreciated!
It turned out that the above solution was correct. The only reason I was getting an incorrect answer was because of my implementation of the nomore() function. In the function I was checking if any item in the possible items list was less than the weight left on the boat. I should have been checking if they were less than or equal to the weight on the boat. A simple mistake.
The wikipedia entry was indeed of use and I enjoyed the comic :)