Getting rid of unnecessary loops - c#

In my game I'm going to have a lot of interactions in which I'll need to see if a player has an item, and if he does and something else is true, then do an action. Described in the following code.
private void SetTinderInPit()
{
MouseState currentMouseState = Mouse.GetState();
if (player.NextToFirePit == true)
{
foreach (Item item in player.PlayerInventory.Items)
{
if (item.ItemName == "tinder")
{
foreach (Item pit in allItemsOnGround)
{
if (pit.ItemName == "firepit" &&
pit.ItemRectangle.Contains(MouseWorldPosition) &&
currentMouseState.LeftButton == ButtonState.Pressed &&
oldMouseState.LeftButton == ButtonState.Released)
{
item.ItemName = "empty";
pit.ItemName = "firepitwithtinder";
pit.Texture = Content.Load<Texture2D>("firepitwithtinder");
}
}
}
}
oldMouseState = currentMouseState;
}
}
As you can see, this is ugly to look at and I think that there would be a better way to do this, but I'm not sure how. Since there will be a lot of these types of methods, I'm wondering what would be the best way to accomplish this?

Seems like you could get rid of (actually hide) the loops altogether by using some LINQ:
private void SetTinderInPit()
{
MouseState currentMouseState = Mouse.GetState();
if (player.NextToFirePit)
{
Item tinder = player.PlayerInventory.Items.FirstOrDefault(i => i.ItemName == "tinder");
if (tinder != null)
{
Item firepit = allItemsOnGround.FirstOrDefault(i => i.ItemName == "firepit" && i.ItemRectangle.Contains(MouseWorldPosition));
if (firepit != null &&
currentMouseState.LeftButton == ButtonState.Pressed &&
oldMouseState.LeftButton == ButtonState.Released)
{
tinder.ItemName = "empty";
firepit.ItemName = "firepitwithtinder";
firepit.Texture = Content.Load<Texture2D>("firepitwithtinder");
}
}
oldMouseState = currentMouseState;
}
}
This has the added advantage of short-circuiting the loop when the item is found. It also makes it easy to check against things other than the name (like an "IsFlammable" or "CanContainFire" property) so you could use multiple items instead of just "tinder" and "firepit".
If you actually intended to remove all firepits and tinder, use:
private void SetTinderInPit()
{
MouseState currentMouseState = Mouse.GetState();
if (player.NextToFirePit)
{
foreach (Item tinder in player.PlayerInventory.Items.Where(i => i.ItemName == "tinder")
{
foreach (Item firepit in allItemsOnGround.Where(i => i.ItemName == "firepit"))
{
if (firepit.ItemRectangle.Contains(MouseWorldPosition) &&
currentMouseState.LeftButton == ButtonState.Pressed &&
oldMouseState.LeftButton == ButtonState.Released)
{
tinder.ItemName = "empty";
firepit.ItemName = "firepitwithtinder";
firepit.Texture = Content.Load<Texture2D>("firepitwithtinder");
}
}
}
oldMouseState = currentMouseState;
}
}
Quick caveat; this code will remove all firepits with the first tinder, leaving the other tinders unscathed. I could unravel the loops to remove everything, but this function matches the provided one; and besides, I'm assuming thats not the intended behavior.
Note you do not need ToList anywhere because you are not modifying the collection during enumeration. You can always modify the items in the collection, proved with the following test:
class IntWrapper
{
public int value;
public IntWrapper(int value)
{
this.value = value;
}
}
class Program
{
static void Main(string[] args)
{
List<IntWrapper> test = new List<IntWrapper>() { new IntWrapper(1), new IntWrapper(2), new IntWrapper(3), new IntWrapper(4), new IntWrapper(5) };
foreach (IntWrapper i in test.Where(i => i.value == 1))
{
i.value = 0;
}
foreach (IntWrapper i in test)
{
Console.WriteLine(i.value);
}
Console.ReadLine();
}
}

The only real change I would make to your existing code would be to move the check for mouse state early on, to avoid checking this multiple times in your loops. In addition I would use Linq to shorten the conditions (by removing the 'if' statements):
MouseState currentMouseState = Mouse.GetState();
// I would get all the conditional checks out of the way up front first
if (player.NextToFirePit &&
currentMouseState.LeftButton == ButtonState.Pressed &&
oldMouseState.LeftButton == ButtonState.Released)
{
foreach (var tinderItem in player.PlayerInventory.Items
.Where(item => item.ItemName == "tinder"))
{
foreach (var firePit in allItemsOnGround
.Where(item => item.ItemName == "firepit" &&
item.ItemRectangle.Contains(MouseWorldPosition)))
{
tinderItem.ItemName = "empty";
firePit.ItemName = "firepitwithtinder";
firePit.Texture = Content.Load<Texture2D>("firepitwithtinder");
}
}
}
oldMouseState = currentMouseState;
An alternate idea, since you were looking for a way to get rid of the 'ugly' code, would be to move some of this functionality to the player object.

I would probably use LINQ more.
Note that this is written in Notepad, not visual studio so is more of a pseudo-code.
private void SetTinderInPit()
{
var currentMouseState = Mouse.GetState();
if (!player.NextToFirePit) return;
player.PlayerInventory.Items.Where(item => item.ItemName == "tinder").ToList().ForEach(item =>
{
allItemsOnGround.Where(x => x.ItemName == "firepit" &&
x.ItemRectangle.Contains(MouseWorldPosition) &&
currentMouseState.LeftButton == ButtonState.Pressed &&
oldMouseState.LeftButton == ButtonState.Released)
.ToList().ForEach(pit =>
{
item.ItemName = "empty";
pit.ItemName = "firepitwithtinder";
pit.Texture = Content.Load<Texture2D>("firepitwithtinder");
});
});
oldMouseState = currentMouseState;
}

Related

Multiple Correct If Statements from a Mouse Click Event

I'm trying to do waste collection program and these are part of codes. My problem is if the picturebox shows image that on second if statements (magazine), there is no problem. But if shows first image that on first if statements (newspaper) and if NewWaste(); gives magazine then there is a problem. Because it adds both of them to listbox but I don't see the second image on picturebox. How can I solve that?
private void NewWaste()
{
Image[] images = new Image[] { newspaper.Image, magazine.Image, glass.Image };
int wastes = rnd.Next(images.Length);
wastePictureBox.Image = images[wastes];
}
//(part of class)
public bool Add(Waste waste)
{
if (FilledVolume + waste.Volume <= Capacity)
return true;
else
return false;
}
private void addPaperWasteBtn_Click(object sender, EventArgs e)
{
if (paperWasteBox.Add(newspaper) == true && wastePictureBox.Image == newspaper.Image)
{
paperWasteListBox.Items.Add("Newspaper");
NewWasteImage();
}
if (paperWasteBox.Add(magazine) == true && wastePictureBox.Image == magazine.Image)
{
paperAtikListBox.Items.Add("Magazine");
NewWasteImage();
}
}
If you only want the second if statement to run if the first one didn't, then you want an else if statement before the second conditional check.
Change:
if (paperWasteBox.Add(newspaper) == true && wastePictureBox.Image == newspaper.Image)
{
paperWasteListBox.Items.Add("Newspaper");
NewWasteImage();
}
if (paperWasteBox.Add(magazine) == true && wastePictureBox.Image == magazine.Image)
{
paperAtikListBox.Items.Add("Magazine");
NewWasteImage();
}
To:
if (paperWasteBox.Add(newspaper) == true && wastePictureBox.Image == newspaper.Image)
{
paperWasteListBox.Items.Add("Newspaper");
NewWasteImage();
}
else if (paperWasteBox.Add(magazine) == true && wastePictureBox.Image == magazine.Image)
{
paperAtikListBox.Items.Add("Magazine");
NewWasteImage();
}
Notice the difference in the SIXTH line!

Multithread, Linq to Sql, ConcurrentDictonary Fails Remove

I have a ConcurrentDictionary of Attributes for products. These attributes have the product ID and values for the names of the attribute and any options the attribute has. I have this ConcurrentDictionary because I have threads that are created to handle each attribute in the dictionary by attribute name.
if (knownAttribute.AttributeType.Value.Equals("Product Specification"))
{
Console.WriteLine("Started a thread for: " + knownAttribute.AttributeTypeId + ", " + knownAttribute.Value);
while (true)
{
/* if (AS400SpecificationAttributes.IsEmpty && knownSpecificationBag.IsEmpty && gatherRowsTasks.All(x => x.IsCompleted))
break;*/
AS400SpecificationAttribute AS400SpecificationAttributeWork = null;
AS400SpecificationAttributeWork = knownSpecificationBag.Keys.FirstOrDefault(x => x.AttributeName == knownAttribute.Value);
if (AS400SpecificationAttributeWork != null)
{
var product = ctx.Products.FirstOrDefault(x => x.ProductNumber == AS400SpecificationAttributeWork.ProductNumber);
if (product == null)
continue;
var productAttribute = new ProductAttribute();
productAttribute.Attribute = knownAttribute;
if (AS400SpecificationAttributeWork.AttributeValue != null)
{
var knownAttributeOption = ctx.AttributeOptions.FirstOrDefault(x => x.Attribute.Equals(knownAttribute) && x.Value.Equals(AS400SpecificationAttributeWork.AttributeValue));
if (knownAttributeOption == null)
{
knownAttributeOption = new AttributeOption();
knownAttributeOption.Value = AS400SpecificationAttributeWork.AttributeValue;
knownAttributeOption.Attribute = knownAttribute;
ctx.AttributeOptions.InsertOnSubmit(knownAttributeOption);
ctx.SubmitChanges();
}
productAttribute.AttributeOption = knownAttributeOption;
productAttribute.AttributeOptionId = knownAttributeOption.Id;
}
product.ProductAttributes.Add(productAttribute);
ctx.SubmitChanges();
string tmpstr = null;
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr))
Thread.Sleep(50);
}
else
{
if (tryCounter < 5)
{
tryCounter++;
Thread.Sleep(1000);
Console.WriteLine("Thread waiting for work: Product Specification:" + knownAttribute.Value);
continue;
}
else
{
int outVal;
threadTracker.TryRemove("Product Specification:" + knownAttribute.Value, out outVal);
Console.WriteLine("Closing Thread: Product Specification:" + knownAttribute.Value);
break;
}
}
Thread.Sleep(50);
}
It seems like the following Attribute element refuses to be removed.
I don't understand why. If i put it in a while(!dic.tryRemove(ele)) it will forever be stuck and never move from there.
There may be an error somewhere within the thread but I have no idea why.
This statement
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr))
will always return true or false. It won't block. That's the behavior of ConcurrentDictionary. It will return false if the key is not in the dictionary.
If you're looping while that method returns false and it's stuck, that means that the item isn't in the dictionary when the loop begins. Either it either was never in the dictionary or that another thread already removed it.
Is your intention to loop until the item is not in the dictionary?
You could try this:
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr)
&& !knownSpecificationBag.ContainsKey(AS400SpecificationAttributeWork))
Implement proper equals and gethashcode when using TryRemove
public override int GetHashCode()
{
return new { this.name, this.value, this.group, this.productNumber }.GetHashCode();
}
public bool Equals(AS400SpecificationAttribute other)
{
if (other == null)
return false;
return (this.ProductNumber.Equals(other.productNumber) && ((this.group != null && this.group.Equals(other.AttributeGroup)) || (this.group == null && other.AttributeGroup == null)) && ((this.name!= null && this.name.Equals(other.AttributeName)) || (this.name == null && other.AttributeName == null)) && ((this.value != null && this.value.ToUpper().Equals(other.AttributeValue.ToUpper())) || (this.value == null && other.AttributeValue == null)));
}

TreeListView and hierarchical checkboxes

I am using TreeListView with:
this.tlv.CheckBoxes = true;
this.tlv.TriStateCheckBoxes = true;
this.tlv.HierarchicalCheckboxes = true;
Hierarchical with tristate works well, except one: the user can set the CheckState.Indeterminate by clicking the mouse, and I don't need it. For this I use 2 delegate that are not working correctly. How to make that work?
this.tvl.CheckStateGetter = delegate(object rowObject)
{
if (((ModelData)rowObject).IsChecked == true)
{
return CheckState.Checked;
}
else
{
if (((ModelData)rowObject).IsChecked == false)
{
return CheckState.Unchecked;
}
else
{
return CheckState.Indeterminate;
}
}
};
this.tvl.CheckStatePutter = delegate(object rowObject, CheckState newValue)
{
if (((ModelData)rowObject).Child.Count > 0)
{
if ((((ModelData)rowObject).Child.Where(x => x.IsChecked != null).Any(x => (bool)x.IsChecked) &&
((ModelData)rowObject).Child.Where(x => x.IsChecked != null).Any(x => !(bool)x.IsChecked)) ||
(((ModelData)rowObject).Child.Any(x => x.IsChecked == null)))
{
((ModelData)rowObject).IsChecked = null;
return CheckState.Indeterminate;
}
else
{
if (((ModelData)rowObject).Child.Where(x => x.IsChecked != null).All(x => (bool)x.IsChecked))
{
((ModelData)rowObject).IsChecked = true;
return CheckState.Checked;
}
else
{
((ModelData)rowObject).IsChecked = false;
return CheckState.Unchecked;
}
}
}
else
{
((ModelData)rowObject).IsChecked = (newValue == CheckState.Checked) ? true : false;
return newValue;
}
};
According to the documentation, "CheckStateGetters" are not allowed in the treelistview.
From the webpage:
One major problem is that we don’t know the checkedness of all the
subitems. When an ObjectListView has a CheckStateGetter installed, the
only way we can know if an item is checked is by calling the
CheckStateGetter on that item. We can’t reason about what is checked
or unchecked – we always have to ask. In our disk browser example, we
would have to ask all 700,000 items if it was checked. That’s never
going to work, so with hierarchical checkboxes, we don’t allow
CheckStateGetters to be installed.
http://objectlistview.sourceforge.net/cs/blog7.html

how to iterate collection and change value

I know this is a dumb question because you cannot modify the loop collection while in a loop, but I do need to change it. I know I must not change the referenced objects, but I have no idea how to do this.
var orders = _orderService.GetOrders(o => !o.Deleted &&
o.OrderStatus != OrderStatus.Cancelled &&
o.OrderStatus != OrderStatus.Complete);
foreach (var order in orders)
{
if (order.PaymentStatus == PaymentStatus.Paid)
{
if (order.ShippingStatus == ShippingStatus.ShippingNotRequired || order.ShippingStatus == ShippingStatus.Delivered)
{
var tempOrder = _orderService.GetOrderById(order.Id);
SetOrderStatus(tempOrder , OrderStatus.Complete, true);
}
}
}
I always get an error.
UPDATED: I changed to this
var orders = _orderService.GetOrders(o => !o.Deleted &&
o.OrderStatus != OrderStatus.Cancelled && o.OrderStatus != OrderStatus.CompletE);
List<int> orderIndex = new List<int>();
orders.ToList().ForEach(x => orderIndex.Add(x.Id));
foreach(var index in orderIndex)
{
var order = _orderService.GetOrderById(index);
if (order.PaymentStatus == PaymentStatus.Paid)
{
if (order.ShippingStatus == ShippingStatus.ShippingNotRequired || order.ShippingStatus == ShippingStatus.Delivered)
{
SetOrderStatus(order, OrderStatus.Complete, true);
}
}
}
try
int count = orders.Count; // the length of the collect : may need a different method for different collection types.
for(int i = 0; i < count; i++)
{
var current = orders[i];
// do stuff with current.
}
use for loop instead of foreach loop
for(int i=0; i<orders.Count; i++)
{
if (orders[i].PaymentStatus == PaymentStatus.Paid)
{
if (orders[i].ShippingStatus == ShippingStatus.ShippingNotRequired || orders[i].ShippingStatus == ShippingStatus.Delivered)
{
var tempOrder = _orderService.GetOrderById(orders[i].Id);
SetOrderStatus(tempOrder , OrderStatus.Complete, true);
}
}
}

convert recursive code to LINQ

I want to convert this recursive call to LINQ but I am not sure how to do the last two conditions. Please advise on how to add these last two conditions.
private void findGoogleOrganic(HtmlNode node)
{
if (node.Attributes["class"] != null)
{
if (node.Attributes["class"].Value.ToString().Contains("r ld"))
{
String tmp;
tmp = node.ParentNode.InnerHtml.ToString();
bool condition1 = false;
bool condition2 = false;
if (tmp != null)
{
**condition1 = tmp.Contains("qcp47e");
condition2 = tmp.Contains("r ld");**
}
**if (condition1 == false && condition2 == true)**
{
GoogleOrganicResults.Add(new Result(URLGoogleOrganic, Listing, node, SearchEngine.Google, SearchType.Organic, ResultType.Website));
}
}
}
if (node.HasChildNodes)
{
foreach (HtmlNode children in node.ChildNodes)
{
findGoogleOrganic(children);
}
}
}
Here is My first attempt without the last two conditions:
private void findGoogleOrganicLINQ(HtmlNode node)
{
var results = node.Descendants()
.Where(x => x.Attributes["class"] != null &&
x.Attributes["class"].Value.Contains("r ld"))
.Select(x => new Result(URLGoogleLocal, Listing, x, SearchEngine.Google, SearchType.Local, ResultType.GooglePlaces));
foreach (Result x in results)
{
GoogleOrganicResults.Add(x);
}
}
if(node.HasChildNodes)
{
node.ChildNodes.ForEach(findGoogleOrganic);
}

Categories

Resources