home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 3: The Continuation / 17-Bit_The_Continuation_Disc.iso / amigan / amigan 21 / recursion / compil_bas / tour4.bas < prev   
Encoding:
BASIC Source File  |  1994-01-27  |  5.2 KB  |  143 lines

  1. ' Tour4. A knight's tour of an 4 x 4 board with option of screen printout.
  2. ' Originally written in True Basic; revised by Terry Peterson with TB materal
  3. ' commented out by apostrophes and A/C routines substituted as required.
  4. ' Both straight and recursive moves are graphically visible. This program has
  5. ' been compiled into the A/C Basic runtime package. RUN tour4.bas.run at any
  6. ' CLI prompt. You do NOT need to load Amiga Basic.
  7.  
  8. option base 1: defint a-z
  9. ' ask color user
  10. ' set mode "high4"     ' So we can Amiga N and M in and out & multitask
  11. cls                    ' clear
  12. print "A quick-and-dirty Knight's Tour from recursion"
  13. let size = 4
  14. let sizesq = size*size
  15. dim board(size,size), best(size,size)
  16.  
  17. ' Define possible moves in sets: a for x axis, b for the y.
  18. dim a(8), b(8)
  19. data 2, 1, -1, -2, -2, -1, 1, 2
  20. data 1, 2, 2, 1, -1, -2, -2, -1
  21. MatRead a()
  22. MatRead b()
  23.  
  24. color 3                 ' set color "red"
  25. locate 3,1              ' set cursor 3,1
  26. print "Print all moves? Enter 'y' for yes or 'n' for no. ";
  27. while (visible<>asc("y"))and(visible<>asc("n")) ' do
  28.   visible=asc(inkey$+chr$(0))                           ' get key visible
  29. wend                ' loop until visible = asc("y") or visible = asc("n")
  30. let visible = (visible = asc("y"))
  31. color 1                                  ' set color user
  32. print
  33. cls               ' clear
  34.                   ' set cursor 1,1
  35. Print "Pass:"
  36. ' open #4: printer             ' Uncomment this to print all squares
  37.                                ' of a given size in subroutine.
  38. let board(1,1) = 1             ' We start on a corner square
  39. call try((2), (1), (1))        ' Second move, starting at x = 1, y = 1
  40. if ok = 1 then
  41.   print "SUCCESS!"
  42.   MatPrint board()             ' We did it!
  43. else
  44.   color 3           ' set color "red"  ' Sorry, Sam--no way from here to there.
  45.   print
  46.   print "No solution, Sam. Here's the best board we got:"
  47.   MatPrint best()
  48. end if
  49. print "We generated"; totalmoves; "legal moves to find this out--"
  50. print "And of those,"; legalmoves; "were to open squares."
  51. print "We made a total of"; pass; "computer passes through the code."
  52. waitkey
  53. end
  54.  
  55. sub waitkey
  56.         x%=pos(x%):y%=csrlin
  57.         locate 23,27
  58.         color 3: print "Press any key to continue.";: color 1
  59.         while inkey$="" :wend
  60.         locate 23,27:print "                          ";
  61.         locate y%,x%:print
  62. end sub
  63.  
  64. sub try(movenbr, x, y)
  65.  
  66.   shared ok, size, sizesq, pass, visible    'Global vars from main routine
  67.   shared bestboard, totalmoves, legalmoves  ' (all other vars local)
  68.   shared a(), b(), board(), best()
  69.  
  70.   let oldx=x : let oldy=y               ' Save initial pos. at this level
  71.   let trynbr = 0                        ' Start trying at the beginning
  72.   let ok = 0                            ' OK later says a move succeeded.
  73.   while (ok<1)and(trynbr<8)
  74.     if not visible then
  75.       locate 1,6                 ' set cursor 1,6
  76.       print pass
  77.     end if
  78.     let trynbr = trynbr + 1          ' The eight possible moves of a Knight.
  79.     let pass = pass + 1              ' Total passes made by program
  80.     let nextx = x + a(trynbr)
  81.     let nexty = y + b(trynbr)
  82.     if nextx => 1 and nextx <= size and nexty >= 1 and nexty <= size then
  83.       let totalmoves = totalmoves + 1
  84.       if board(nextx,nexty) = 0 then                    'Not been here before
  85.         let board(nextx,nexty) = movenbr                'Ah, sweet success.
  86.         let ok = 1
  87.         let legalmoves = legalmoves + 1
  88.         if visible then
  89.           print "Pass:"; pass; " Move #:"; movenbr
  90.           MatPrint board()
  91.           waitkey
  92.         end if
  93.         if movenbr > bestboard then    ' Store our best board or recursion
  94.           let bestboard = movenbr      ' will destroy the last moves.
  95.           ' let Mat best = board                Sorry....
  96.               for i%=lbound(best,1) to ubound(best,1)
  97.                 for j%=lbound(best,2) to ubound(best,2)
  98.                   best(i%,j%)=board(i%,j%)
  99.                 next j%
  100.               next i%
  101.         end if
  102.        ' if movenbr => 15 then         ' Uncomment this to print
  103.        '  print #4: "Pass no:"; pass   'all squares of given size to check
  104.        '    mat print #4: board;       ' for duplicates. None found.
  105.        ' end if
  106.         if movenbr < sizesq then   ' Parens around parms in recursive call force
  107.           let x = nextx            ' call by value, not by reference
  108.           let y = nexty
  109.           call try(movenbr + 1, (x), (y))  'Recursive try for next move.
  110.           if ok = 0 then                   'Nope, didn't work. Back up!
  111.             let board(x,y) = 0
  112.             let x = oldx
  113.             let y = oldy
  114.           end if
  115.           if visible then
  116.             color 3                        ' set color "red"
  117.             print "Uncoil from recursion.";
  118.             print " Move No."; movenbr
  119.             MatPrint board()
  120.             waitkey
  121.           end if
  122.         else
  123.           let ok = 1                    'By George, we've got it!
  124.         end if
  125.       end if
  126.     end if
  127.   wend                             ' loop until ok = 1 or trynbr = 8
  128. end sub
  129.  
  130. sub MatRead (x())
  131.    for i%=lbound(x) to ubound(x)
  132.      read x(i%)
  133.    next i%
  134. end sub
  135.  
  136. sub MatPrint (x(2))
  137.   for i%=lbound(x,1) to ubound(x,1)
  138.     for j%=lbound(x,2) to ubound(x,2)
  139.       print using "###"; x(i%,j%);
  140.     next j%: print
  141.   next i%
  142. end sub
  143.