Potentially Silly Question

Monkey Forums/Monkey Programming/Potentially Silly Question

donicamm(Posted 2013) [#1]
So, I have this code:

Function Main%()

Local mapData:String
Local levelPosition:Int

		mapData = ""
		mapData += "1111111111"
		mapData += "1010000031"
		mapData += "1011011111"
		mapData += "1000000001"
		mapData += "1111101111"
		mapData += "1200101001"
		mapData += "1000101001"
		mapData += "1011101101"
		mapData += "1000000001"
		mapData += "1111111111"
		
		levelPosition = 0;
		
		For Local i:Int = 1 To 10
			
			For Local j:Int = 1 To 10
			
				If String.FromChar(mapData[levelPosition]) = 0
					Print ("EMPTY SPACE")
				Elseif String.FromChar(mapData[levelPosition]) = 1
					Print ("WALL")
				Elseif String.FromChar(mapData[levelPosition]) = 2
					Print ("PLAYER")
				Elseif String.FromChar(mapData[levelPosition]) = 3
					Print ("BANANA")
				End
				'Print(String.FromChar(mapData[levelPosition]))
				levelPosition += 1
			End
			Print("END LINE")
		End
		
End


Right now, when I run the code, it prints everything as I'd expect, except it never prints "EMPTY SPACE".

Here is an example of the output:

WALL
WALL
WALL
WALL
WALL
WALL
WALL
WALL
WALL
WALL
END LINE
WALL
WALL
BANANA
WALL
END LINE
WALL
WALL
WALL
WALL
WALL
WALL
WALL
WALL
END LINE
WALL
WALL
END LINE
WALL
WALL
WALL
WALL
WALL
WALL
WALL
WALL
WALL
END LINE
WALL
PLAYER
WALL
WALL
WALL
END LINE
WALL
WALL
WALL
WALL
END LINE
WALL
WALL
WALL
WALL
WALL
WALL
WALL
END LINE
WALL
WALL
END LINE
WALL
WALL
WALL
WALL
WALL
WALL
WALL
WALL
WALL
WALL
END LINE


When I uncomment, my one commented line, the 0s are printed. So I know 0 is what is being picked up. Why aren't they picked up by my conditional?

WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
END LINE
WALL
1
0
WALL
1
0
0
0
0
0
BANANA
3
WALL
1
END LINE
WALL
1
0
WALL
1
WALL
1
0
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
END LINE
WALL
1
0
0
0
0
0
0
0
0
WALL
1
END LINE
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
0
WALL
1
WALL
1
WALL
1
WALL
1
END LINE
WALL
1
PLAYER
2
0
0
WALL
1
0
WALL
1
0
0
WALL
1
END LINE
WALL
1
0
0
0
WALL
1
0
WALL
1
0
0
WALL
1
END LINE
WALL
1
0
WALL
1
WALL
1
WALL
1
0
WALL
1
WALL
1
0
WALL
1
END LINE
WALL
1
0
0
0
0
0
0
0
0
WALL
1
END LINE
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
WALL
1
END LINE



marksibly(Posted 2013) [#2]
Ummm...you've actually found a bug in Monkey!

To fix for now, try comparing with "0", "1" etc instead of 0, 1 etc.


muddy_shoes(Posted 2013) [#3]
I know 0 is what is being picked up


"0" is on one side of the comparison sure, but it isn't on the other.

If String.FromChar(mapData[levelPosition]) = 0


That line tries to compare a String with an integer literal. It implies a conversion from the integer to a string. For some reason String(0) produces an empty string rather than "0".

That may well be a bug, but for your purposes you're better off comparing like with like and avoiding converting anyway. So either put string values on the other side ("0" rather than 0) or compare the charCode to the ascii value directly (mapData[levelPosition] = 48).


donicamm(Posted 2013) [#4]
Oh, awesome!

The "for now" fix is fine for now, so I'm going to roll with it. Thanks for replying to my post marksibly.


Gerry Quinn(Posted 2013) [#5]
By the way, just as a suggestion you may find something like the following a bit cleaner and also more efficient:



Obviously it doesn't really matter for a small test program, but the string concatenation and the use of 1-based indexes can often be a recipe for confusion...