Jump to content

Recommended Posts

Posted

Hey.

 

The title says it all, I need to compare 8 slots in my custom container and see if they have the same items in them. (empty slots are fine) The only way I can think to do it is with a ton of if statements, is there a better way to do this?

 

Thanks,

 

TheRealMcrafter

Posted

1. Get a list of items for the two containers that you want to compare.

2. Sort the lists of items with a well-defined ordering (lexigraphically by name, etc).

3. Compare the lists (using a loop of some sort).

 

There are more efficient ways, but this way is straightforward and easy to code.

 

[spoiler=An alternative]

1. Make a copy of container B.

2. For every item in container A, remove it from the copy of B.

3. If you try to remove an item and it's not there, then A and B aren't equal.

4. At the end, if there are still items remaining in the copy of B, then A and B aren't equal.

 

 

Posted

You wouldn't have to. The only information you need in order to determine if two containers have the same items is

1. Do they contain the same items?

2. If they both contain an item, do they contain the same amount of that item?

 

The algorithms I gave effectively check for both of these. You don't actually need to know anything about empty slots. (Unless you specifically want to compare them, in which case I misunderstood the question.)

Posted

Basically, I have 2 types of fuel rods (items) that go into the slots (reactor). If there is only "Uranium" in the container (reactor), then return true. If there is only "Plutonium" in the container (reactor), then return true. If there is a mix of "Uranium" and "Plutonium" in the container (reactor), then return false.

Posted

Oh, I thought you had two containers and you wanted to see if they had the same items.

 

It turns out that you have one container, and you want to see if it contains one type of item or not.

 

[spoiler=If you only need to consider uranium and plutonium]

1. Create two booleans,

hasUranium

and

hasPlutonium

.

2. Loop through the inventory contents, if you see uranium, set

hasUranium

to true. Do the same for plutonium.

3. At the end, return

hasUranium != hasPlutonium

. (You can also use "^," the xor symbol in Java, which does the same thing.)

 

Note that this will return true if the container is empty. You can easily modify it so that it returns false in this case.

 

 

 

[spoiler=If you need to consider many item types]

1. Get the first item in your container, store it in a variable,

firstItem

.

2. Loop through the inventory contents, see if every other item has the same item type as first item.

3. If any result from 2 yields false, then there are multiple types of items in your container.

 

Again, it's pretty easy to add in a case for an empty container.

 

 

 

Sorry for the confusion.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.