微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何检查3个列表之间的内容?

如何解决如何检查3个列表之间的内容?

在这里,我需要一些游戏代码方面的帮助。这是一个关于制作菜肴和配送的游戏。您需要捕获一些产在地下的成分,当您捕获它们时,它们就会进入库存。

有3种食谱,分别具有1、2和3种成分。同样,不需要在您的库存中添加任何成分即可完成配方,它只应验证您是否具有合适的Iten。如果您有3种成分,则应在包含3种成分的食谱列表中进行验证,以确认这些成分是否存在。

这些成分具有名称和ID。

public abstract class IIngredient : IItem {
    public string ingredientName;
    public int ingredientId;
}

食谱是IIngredient的列表,并且食谱有一个名称

public abstract class IRecipe {
    public List<IIngredient> ingredients;
    public string recipe_name;
}

还有库存

public abstract class IInventory
{
    public List<IIngredient> ingredient_inventory; 
    public abstract void AddIngredient(IIngredient ingredient);
}

玩家清单:

public class PlayerInventory : IInventory
{

    public PlayerInventory()
    {
        this.ingredient_inventory = new List<IIngredient>();
    }

    public override void AddIngredient(IIngredient ingredient)
    {
        ingredient_inventory.Add(ingredient);
    }
}

我交付物品时,要检查库存中有多少Iten,知道应该比较哪些食谱,然后,尝试将Itens与所有成分相同的食谱匹配。如果有食谱匹配,我想知道是谁。

我正在使用此代码,但是它不起作用:

player_inventory_ingredients_id = new List<int>();
player_inventory_ingredients_name = new List<string>();

   public override void CheckRecipe(PlayerInventory playerInventory) 
    {

        foreach (IIngredient item_ingredient in playerInventory.ingredient_inventory)
        {
            player_inventory_ingredients_name.Add(item_ingredient.ingredientName);
            Debug.Log("You have " + item_ingredient.ingredientName); 
        }

        List<IRecipe> recipes_item_amount = recipes.FindAll
                      (i => i.ingredients.Count == playerInventory.ingredient_inventory.Count);



        foreach (IRecipe recipe_name in recipes_item_amount)
        {
            Debug.Log("Possible recipes are " + recipe_name.recipe_name);

            List<string> recipe_temp = new List<string>();
            IEnumerable<string> test_the_names = player_inventory_ingredients_name.Except(recipe_temp);

            foreach (IIngredient ingredients in recipe_name.ingredients)
            {
                Debug.Log(recipe_name + " ingredient is: " + ingredients);

                recipe_temp.Add(ingredients.ingredientName);
            }
        }    

}

我尝试验证并检查是否有itens的最后一个代码块没有显示任何结果。我什至不知道我写的代码是否有意义

解决方法

也许是这样吗?

for (int i = 0; i < recipes_item_amount.Count; i++)
{
    int item_ok=0;

    for (int f = 0; f < recipes_item_amount[i].ingredients.Count; f++)
    {
        if (item_ok == recipes_item_amount[i].ingredients.Count)
        {
            Debug.Log(recipes_item_amount[i].recipe_name);
        }

        for (int v = 0; v < playerInventory.ingredient_inventory.Count; v++)
        {
            if (playerInventory.ingredient_inventory[v].ingredientName == recipes_item_amount[i].ingredients[f].ingredientName)
            {
                Debug.Log("This item matchs");
                item_ok++;
                break;
            }
            else 
            {
                Debug.Log("This item doesn't match");
            }
        }
    }

但是我没有配方名称,调试仅显示:

Debug.log

,

这有意义吗?它没有显示正确的答案:(

bool found = false;

for (int i = 0; i < recipes_item_amount.Count; i++)
{           

    if (!found)
    {
        Debug.Log(recipes_item_amount[i].recipe_name);
    }

    for (int f = 0; f < recipes_item_amount[i].ingredients.Count; f++)
    {          
        for (int v = 0; v < playerInventory.ingredient_inventory.Count; v++)
        {
            if (playerInventory.ingredient_inventory[v].ingredientName == recipes_item_amount[i].ingredients[f].ingredientName)
            {
                Debug.Log("This item matchs : " + recipes_item_amount[i].ingredients[f].ingredientName);
                found = true;
                break;
            }
            else 
            {
                Debug.Log("This item doesn't match: " + recipes_item_amount[i].ingredients[f].ingredientName);
            }
        }
    }
}

我移动了代码。

for (int i = 0; i < recipes_item_amount.Count; i++)
{

    for (int f = 0; f < recipes_item_amount[i].ingredients.Count; f++)
    {
        bool found = false;
        for (int v = 0; v < playerInventory.ingredient_inventory.Count; v++)
        {
            if (playerInventory.ingredient_inventory[v].ingredientName == recipes_item_amount[i].ingredients[f].ingredientName)
            {
                Debug.Log("This item matchs : " + recipes_item_amount[i].ingredients[f].ingredientName);
                found = true;
                break;
            }
            else
            {
                Debug.Log("This item doesn't match: " + recipes_item_amount[i].ingredients[f].ingredientName);
            }
        }
        if (!found)
        {
            Debug.Log(recipes_item_amount[i].recipe_name);
        }
    }
}
,

我终于做到了,非常感谢@jdweng的帮助!

for (int i = 0; i < recipes_item_amount.Count; i++)
{
    int have_item = 0;

    for (int f = 0; f < recipes_item_amount[i].ingredients.Count; f++)
    {
        for (int v = 0; v < playerInventory.ingredient_inventory.Count; v++)
        {
            if (playerInventory.ingredient_inventory[v].ingredientName == recipes_item_amount[i].ingredients[f].ingredientName)
            {
                Debug.Log("This item matchs : " + recipes_item_amount[i].ingredients[f].ingredientName);
                have_item++;
                Debug.Log(have_item);
                break;
            }
            else
            {
                Debug.Log("This item doesn't match: " + recipes_item_amount[i].ingredients[f].ingredientName);
            }
        }
        if (have_item == playerInventory.ingredient_inventory.Count)
        {
            Debug.Log(recipes_item_amount[i].recipe_name);
        }
    }
,

如何?

        var ingredientIds = playerInventory.ingredient_inventory.Select(i => i.ingredientId).ToList();

        var recipiesMatchingAllTheIngredients = recipes.Where(r => r.ingredients.Count == ingredientIds.Count &&
            r.ingredients.All(i => ingredientIds.Contains(i.ingredientId)))
            .ToList();

Linq很棒:)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。