Shop OBEX P1 Docs P2 Docs Learn Events
I have a bug I can't solve with an array and a if loop — Parallax Forums

I have a bug I can't solve with an array and a if loop

jonduncanjonduncan Posts: 40
edited 2007-03-18 11:09 in Propeller 1
I have a bug with this code

if ((VWalls[noparse][[/noparse]x] >> y) & 1)

for some reason it is never true in my code
x and y·go from 0 to 15
I filled all values of VWalls with $ffff for debuging purposes
and it is never true?????

I think the problem is with this code, but I am not completely sure is the code above correct code to find if a certain bit is a 1

Comments

  • CJCJ Posts: 470
    edited 2007-03-18 01:00
    looks good as far as checking for a 1, could you post the rest of your code?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Parallax Forums - If you're ready to learn, we're ready to help.
  • jonduncanjonduncan Posts: 40
    edited 2007-03-18 03:32
    ok i will post the rest of the code, it maps a maze, there are no comments so it might be hard to figure out what it is doing, but i will add some comments



    { Micromouse Maze Mapper.
    This program maps a 16 by 16 maze using a flooding technique
    Author: Jonathan Duncan
    Date: March 16, 2007}}

    Con
    · _CLKMODE = XTAL1 + PLL16X
    · _XINFREQ =5_000_000
    · INF = 255
    obj
    ··· SS : "Simple_Serial"
    dat ' sets the distance from the center if there were no walls
    startweight· byte 14, 13, 12, 11, 10,· 9,· 8,· 7,· 7,· 8,· 9, 10, 11, 12, 13, 14, 13, 12, 11, 10,· 9,· 8,· 7,· 6,· 6,· 7,· 8,· 9, 10, 11, 12, 13, 12, 11, 10,· 9,· 8,· 7,· 6,· 5,· 5,· 6,· 7,· 8,· 9, 10, 11, 12, 11, 10,· 9,· 8,· 7,· 6,· 5,· 4,· 4,· 5,· 6,· 7,· 8,· 9, 10, 11, 10,· 9,· 8,· 7,· 6,· 5,· 4,· 3,· 3,· 4,· 5,· 6,· 7,· 8,· 9, 10, 9,· 8,· 7,· 6,· 5,· 4,· 3,· 2,· 2,· 3,· 4,· 5,· 6,· 7,· 8,· 9, 8,· 7,· 6,· 5,· 4,· 3,· 2,· 1,· 1,· 2,· 3,· 4,· 5,· 6,· 7,· 8, 7,· 6,· 5,· 4,· 3,· 2,· 1,· 0,· 0,· 1,· 2,· 3,· 4,· 5,· 6,· 7,· 7,· 6,· 5,· 4,· 3,· 2,· 1,· 0,· 0,· 1,· 2,· 3,· 4,· 5,· 6,· 7, 8,· 7,· 6,· 5,· 4,· 3,· 2,· 1,· 1,· 2,· 3,· 4,· 5,· 6,· 7,· 8,· 9,· 8,· 7,· 6,· 5,· 4,· 3,· 2,· 2,· 3,· 4,· 5,· 6,· 7,· 8,· 9, 10,· 9,· 8,· 7,· 6,· 5,· 4,· 3,· 3,· 4,· 5,· 6,· 7,· 8,· 9, 10, 11, 10,· 9,· 8,· 7,· 6,· 5,· 4,· 4,· 5,· 6,· 7,· 8,· 9, 10, 11, 12, 11, 10,· 9,· 8,· 7,· 6,· 5,· 5,· 6,· 7,· 8,· 9, 10, 11, 12, 13, 12, 11, 10,· 9,· 8,· 7,· 6,· 6,· 7,· 8,· 9, 10, 11, 12, 13, 14, 13, 12, 11, 10,· 9,· 8,· 7,· 7,· 8,· 9, 10, 11, 12, 13, 14

    VAR
    · byte x
    · byte y
    · byte MapWeights[noparse][[/noparse]256]
    · byte· Hwalls[noparse][[/noparse]17]
    · byte· Vwalls[noparse][[/noparse]17]
    · byte· nib0
    · byte· nib1·
    pub main· | i , j
    · SS.start( -1, 30, 19200)
    · repeat i from 0· to 255
    ··· MapWeights[noparse][[/noparse]i] := startweight[noparse][[/noparse]i]
    ··repeat i from 0 to 16·· ' set all walls on for debuging purposes
    ···· hwalls[noparse][[/noparse]i] := $ffff
    ···· vwalls[noparse][[/noparse]i] := $ffff
    · repeat
    ··· SetMapWeight·· ·' set new weight according to the walls
    ··· repeat i from 0 to 15··· ' Sends map over serial for debuging
    ····· repeat j from 0 to 15
    ······· nib0 := MapWeights[noparse][[/noparse]j + i* 16] & $0f
    ······· nib1 := (MapWeights[noparse][[/noparse]j + i* 16] & $f0)>> 4
    ······· if nib0 > 9
    ········· nib0 := nib0 + 55
    ······· else
    ········· nib0 := nib0 + $30
    ······· if nib1 > 9
    ········· nib1 := nib1 + 55
    ······· else
    ········· nib1 := nib1 + $30
    ······· SS.tx(nib1)
    ······· SS.tx(nib0)
    ······· SS.tx($20)
    ····· SS.tx($A)
    ····· SS.tx($D)
    Pub SetMapWeight | t, Weight
    · repeat t from 0 to INF
    ··· repeat x from 0 to 15
    ····· repeat y from 0 to 15
    ······· Weight := MapWeights[noparse][[/noparse]x +(y* 16)]
    ········ if not (Weight == 0)
    ·········· Weight := FindNewWeight
    ·········· if Weight > INF
    ······························· Weight := INF
    ·········· MapWeights[noparse][[/noparse]x +(y* 16)] := Weight


    Pri FindNewWeight : NewWeight | xminus, xplus, yminus, yplus
    · if ((VWalls[noparse][[/noparse]x] >> y) & 1)· 'test this one this is where the problem is <----never does this
    ··· xminus:=INF···
    · else
    ··· xminus:= MapWeights[noparse][[/noparse]x-1 + y*16]
    · if ((VWalls[noparse][[/noparse]x + 1] >> y)| 1)·· ' always true to test if case above
    ··· xplus:=INF···
    · else
    ··· xplus:= MapWeights[noparse][[/noparse]x+1 + y*16]
    · if ((HWalls[noparse][[/noparse]y] >> (15 - x)) | 1 )·· ' always true to test if case above
    ··· yminus:=INF···
    · else
    ··· yminus:= MapWeights[noparse][[/noparse]x + (y-1)*16]
    · if ((HWalls[noparse][[/noparse]y + 1] >> (15 - x)) | 1 )·· ' always true to test if case above
    ··· yplus:=INF···
    · else
    ··· yplus:= MapWeights[noparse][[/noparse]x + (y+1)*16]······

    · NewWeight := xminus <# xplus <# yminus <# yplus
    · NewWeight++

    {
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF 00 00 FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF 00 00 01 02 03 04 05 06 07
    08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17
    18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27
    28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37
    38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47
    48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57
    58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67
    68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77



    ·output it should be

    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF 00 00 FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF 00 00 FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    ·since all walls are on}
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-18 03:43
    HWalls and VWalls are declared as byte arrays (with 8 bits) so they can't hold a value like $FFFF. You'll actually get $00FF and, for any y value greater than 7, the IF statement will fail.
  • jonduncanjonduncan Posts: 40
    edited 2007-03-18 03:44
    ohh thanks haha i hate simple errors like that
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-18 03:57
    Yeah, it's the simple errors that get you because you just can't see them. You've got the same false assumptions that caused you to make the mistake in the first place. For what it's worth, it's part of the real art of debugging to be able to start from scratch when looking at a program and see it as it's written. Not many people can do it well. That's why it's sometimes important to display everything when you've got some bug that you can't see. The displayed data will tell you that something's not the way you expected it to be and lead you to the section to look at.
  • JossJoss Posts: 32
    edited 2007-03-18 11:09
    ..>That's why it's sometimes important to display everything when you've got some bug that you can't see.

    Exactly.

    The answer is always there. With compound statements, you simply deconstruct the statement and examine the outputs.

    Debugging is my favorite task when programming. I can't tell you how many times I've heard programmers blame the compiler, the operating system and even the computer for things.

    I simply tell them: What is, is. Then I start deconstructing. It's not so much an art as an act of patience, in my experience.

    Regards
Sign In or Register to comment.