如何检查 3 个列表之间的内容?
Posted
技术标签:
【中文标题】如何检查 3 个列表之间的内容?【英文标题】:How can I check things between 3 lists? 【发布时间】:2021-02-24 14:55:17 【问题描述】:我需要一些关于我的游戏代码的帮助。这是一个关于制作菜肴和送货的游戏。您需要捕获一些在地下生成的成分,当您捕获它们时,它们会进入您的库存。
有 3 种类型的食谱,有 1、2 和 3 种成分。此外,不需要在您的库存中按顺序排列成分即可完成配方,它应该只是验证您是否拥有正确的物品。如果您有 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);
当我交付物品时,我想检查我的库存中有多少物品,以了解我应该比较哪些食谱,然后尝试将我的物品与所有成分相同的食谱进行匹配.如果有任何食谱匹配,我想知道它是谁。
我正在使用此代码,但它不起作用:
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,但没有显示任何结果。我什至不知道我写的代码是否有意义嘿嘿
【问题讨论】:
最后一个代码块,我尝试验证并检查是否有 itens,但没有显示任何结果。我什至不知道我写的代码是否有意义嘿嘿 那么你需要三个嵌套的for循环1)用户2)配方名称3)成分 @GertArnold 成功了!谢谢! @jdweng 你介意写吗? 球员名单在哪里。我看不出你是从哪里得到玩家名字的? 【参考方案1】:也许是这样的?
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
【讨论】:
是的。但是您只需要找到一个匹配项。所以在你进入最后一个 for 循环之前,你有一个变量:Boolean found = false。然后,当您获得匹配项时,您设置 true 并退出最后一个 for 循环。仅当 found == false 时才会出现“不匹配”。 发布另一个答案>>>>【参考方案2】:这有意义吗?它没有显示正确的答案:(
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);
【讨论】:
非常感谢!!!当我在食谱中有一个项目时它可以工作,但是有 2 或 3 个项目的食谱它不起作用 可能是你缺少成分!【参考方案3】:我终于成功了,非常感谢@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);
【讨论】:
【参考方案4】:这个怎么样:
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 很棒 :)
【讨论】:
以上是关于如何检查 3 个列表之间的内容?的主要内容,如果未能解决你的问题,请参考以下文章
如果数据库表格单元格的内容是一个列表,我如何检查该列表是不是包含 T-SQL 中的特定值?