I'm currently developing a "Sokoban" game at a very basic level. I'm trying to make this for loop but cannot seem to complete it. My aim is to carry out an example of the movement code below, I feel my syntax is wrong in regards to adding another condition where it says "(diamondlist.Count) & (Playerlist.Count). The error I am getting is "Operator '&' cannot be applied to operands of type 'bool' and 'int'. I have tried to add double "&&" and this does not solve it eitheir, any help is appreciated - thanks :D
protected override void Update(GameTime gameTime)
{
for (int i = 0; i < (diamondlist.Count) & (Playerlist.Count); i++)
{
if ((Playerlist[i].Position == diamondlist[i].Position) && kb_old.IsKeyDown(Keys.W))
{
if (currentMap.isWalkable(new Point(diamondlist[i].m_position.X, diamondlist[i].m_position.Y - 1)))
diamondlist[i].m_position.Y--;
}
What about simplifying your test condition ?
int maxValue = System.Math.Min(diamondlist.Count,Playerlist.Count);
for (int i = 0; i < maxValue; i++)
There are a few problems.
The expression i < (diamondlist.Count) & (Playerlist.Count)
is evaluated as:
(i < diamondlist.Count) & Playerlist.Count
The first part of the expression (i < diamondlist.Count
) is a bool
, but the second part (Playerlist.Count
) is an int
. It's complaining because the &
doesn't accept two different types like that.
You are then trying to apply &
which (for integers) is a bitwise AND operator. This would work if both types were the same, but it can lead to spend unnecessary CPU cycles and is not typically used in Boolean expressions. You probably want to use &&
-- the logical or conditional and operator.
Try this instead:
for (int i = 0; i < diamondlist.Count && i < Playerlist.Count; i++)
{
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With