Reputation: 45
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--;
}
Upvotes: 4
Views: 28525
Reputation: 149058
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++)
{
...
}
Upvotes: 13
Reputation: 25370
Think about this like a computer. It needs a something that evaluates to true or false there. In english, you have:
IF (i is less than this) AND (this)
It can't evaulate the second expression because there's no comparison going on (though to a human there seems to be). So you have to make it a bit more broad, and do:
IF (i is less than this) AND (i is less than this)
Upvotes: 0
Reputation: 1756
If you intend to check both conditions, then it would look like this:
for (int i = 0; i < diamondlist.Count && i < Playerlist.Count; i++) ...
&& is the logical AND operator. & is the bitwise AND operator.
Upvotes: 0
Reputation: 2823
Can you try: for (int i = 0; i < (diamondlist.Count) & i < (Playerlist.COunt); i++)
.
It seems that the error you are getting is indicating that i < (diamondlist.Count) & (Playerlist.Count)
is being executed as doing the comparison i < (diamondlist.Count)
and doing an &
with (Playlist.Count)
, which is an int.
Upvotes: 0
Reputation: 11
I'm not sure what you try to achieve exactly, but I think this is what you want:
for (int i = 0; i < (diamondlist.Count && i < Playerlist.Count; i++)
Upvotes: 0
Reputation: 216353
What about simplifying your test condition ?
int maxValue = System.Math.Min(diamondlist.Count,Playerlist.Count);
for (int i = 0; i < maxValue; i++)
Upvotes: 5
Reputation: 50235
Each check is independent of each other. You have to be explicit about what you're checking. When you say "i less than diamondlist.Count and playerList.Count" you really mean "i less than diamondlist.Count and i less than playerlist.Count".
for (int i = 0; (i < diamondlist.Count) && (i < Playerlist.Count); i++)
....
Upvotes: 2
Reputation: 742
for (int i = 0; i < (diamondlist.Count) && i < (Playerlist.Count); i++)
Upvotes: 0