Loading ...
Sorry, an error occurred while loading the content.

A strange little bug

Expand Messages
  • René
    Here is a subroutine belonging to a Sudoku solver program: sub solution(ordre$,test,suivre,bb$,choix$,tmp) ordre$ =
    Message 1 of 2 , Dec 8, 2010
    • 0 Attachment
      Here is a subroutine belonging to a Sudoku solver program:

      sub solution(ordre$,test,suivre,bb$,choix$,tmp)
      ordre$ = "ns_hs_in_n2_h2_n3_h3_n4_h4_f2_f3_f4_fx_co_sk_xw_xc_xy_rp_er_"
      test = false : suivre = true
      if debug then
      print "test : "; test
      pause()
      endif
      while suivre
      bb$ = ordre$
      while
      choix$ = left$(bb$,2)
      select case choix$
      case "ns" : tmp = naked_single()
      case "hs" : tmp = hidden_single()
      case "in" : tmp = intersection()
      case "n2" : tmp = naked_set(2)
      case "h2" : tmp = hidden_set(2)
      case "n3" : tmp = naked_set(3)
      case "h3" : tmp = hidden_set(3)
      case "n4" : tmp = naked_set(4)
      case "h4" : tmp = hidden_set(4)
      case "f2" : tmp = fish(2)
      case "f3" : tmp = fish(3)
      case "f4" : tmp = fish(4)
      case "fx" : tmp = fin_xwing()
      case "co" : tmp = coloriage()
      case "sk" : tmp = skyscraper()
      case "xw" : tmp = xy_wing()
      case "xc" : tmp = xy_chain()
      case "xy" : tmp = xyz_wing()
      case "rp" : tmp = remote_pair()
      case "er" : tmp = rectangle()
      end select
      if tmp then
      exit while
      else
      if tchao then
      exit sub
      endif
      endif
      bb$ = right$(bb$,len(bb$)-3)
      wend bb$ = ""
      if bb$ = "" then
      suivre = false
      else
      if compte = 81 then
      test = true
      exit while
      endif
      endif
      wend
      if debug then
      print "compte : "; compte, "test : "; test
      pause()
      endif
      solution = test
      end sub

      Basically, this tries in a loop a suite of functions that hopefully bring some progress. If successful, the function returns temp = true. If sufficient progress is achieved to reach a count of 81, then the problem is solved and solution() returns true; if not, it returns false.

      This had been working perfectly, until some undetected change happened. As a result, the value of 'test' is altered in the last case (failure): initially 0, and although no assignation is made, it turns out to be 4 at the end. This of course causes solution() to
      falsely return true, and the program ends as though the problem were solved, which is not the case.

      Now comes the really strange point: if instead of 'test' I use another name, say 'tpo', then everything is all right again. I fail to understand what happens, as 1) 'test' is a local variable, and 2) it is not a keyword: by what mystery of parsing can it be read as '4'? And what sort of change may have caused such behaviour?

      Any ideas, anybody?

      Cheers.

      Charles
    • jeffonymail
      Hi Charles. I just had a quick look. ... ....Jeff
      Message 2 of 2 , Dec 9, 2010
      • 0 Attachment
        Hi Charles.

        I just had a quick look.

        > if tchao then
        > exit sub
        > endif

        I notice that this routine exits the sub without setting a value as in:

        > solution = test

        ....Jeff
      Your message has been successfully submitted and would be delivered to recipients shortly.