Advertisement
ForrestFox

Hanoi Tower Solver

Mar 23rd, 2021
242
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SCREEN 13
  2.  
  3. CONST CNT = 3
  4.  
  5. ' Stack for current situation
  6. DIM SHARED W(0 TO 15, 0 TO 2) AS INTEGER
  7.  
  8. ' Draw plates & column
  9. FOR i = 0 TO 2: LINE (50 + i * 100 - 1, 25)-(50 + i * 100 + 1, 184), 6, BF: NEXT
  10. FOR i = 0 TO 15: W(i, 0) = -1: W(i, 1) = -1: W(i, 2) = -1: NEXT
  11. FOR i = 0 TO CNT - 1: DrawPlate 0, i, i: W(i, 0) = i: NEXT
  12.  
  13. ' Solve task
  14. Hanoi 0, 1, 2, CNT
  15.  
  16. SLEEP 0
  17.  
  18. SUB Delay (N)
  19.   FOR i = 0 TO N * 100: NEXT
  20.   N = N * .99
  21. END SUB
  22.  
  23. SUB DrawPlate (col, row, isize)
  24.  
  25.   x = 50 + 100 * col
  26.   y = 170 - row * 11
  27.   cl = 1 + isize
  28.   size = 45 - isize * 5
  29.  
  30.   LINE (x - size, y)-(x + size, y + 10), cl, BF
  31.   LINE (x - size, y)-(x + size, y + 10), 15, B
  32.   LINE (x - size, y + 10)-(x + size, y + 10), 8
  33.   LINE (x + size, y + 10)-(x + size, y), 8
  34.  
  35. END SUB
  36.  
  37. SUB Hanoi (A, B, C, N)
  38.  
  39.   ' Front
  40.   IF N > 1 THEN Hanoi A, C, B, N - 1
  41.  
  42.   ' Get from A and place to B
  43.   FOR i = 0 TO CNT
  44.     IF W(i, A) = -1 THEN irow = i: cl = W(i - 1, A): EXIT FOR
  45.   NEXT
  46.  
  47.   FOR i = 0 TO CNT
  48.     IF W(i, B) = -1 THEN orow = i: EXIT FOR
  49.   NEXT
  50.  
  51.   ' Move plate
  52.   W(orow, B) = W(irow - 1, A)
  53.   W(irow - 1, A) = -1
  54.  
  55.   ' Animate
  56.   Move A, irow - 1, cl, B, orow
  57.  
  58.   ' Back
  59.   IF N > 1 THEN Hanoi C, B, A, N - 1
  60.  
  61. END SUB
  62.  
  63. ' From column, row, size (color) -> column, row
  64. SUB Move (icol, irow, isize, ocol, orow)
  65.  
  66.   DL = 25
  67.   HG = 170
  68.  
  69.   ' Direction for move
  70.   IF icol < ocol THEN dir = 1 ELSE dir = -1
  71.  
  72.   x = 50 + 100 * icol
  73.   y = HG - irow * 11
  74.   cl = 1 + isize
  75.   size = 45 - isize * 5
  76.  
  77.   ' Count increments
  78.   icnt = y - 15
  79.   ocnt = HG - orow * 11 - 15
  80.  
  81.   ' Incremental UP
  82.   FOR i = 0 TO icnt
  83.  
  84.     ' Clear one line
  85.     LINE (x - size, y + 10)-(x - 2, y + 10), 0, BF
  86.     LINE (x - 1, y + 10)-(x + 1, y + 10), 6, BF
  87.     LINE (x + 2, y + 10)-(x + size, y + 10), 0, BF
  88.  
  89.     ' Draw underline
  90.     LINE (x - size, y + 9)-(x + size, y + 9), 8
  91.     LINE (x - size + 1, y)-(x + size - 1, y), cl
  92.     LINE (x - size, y - 1)-(x + size, y - 1), 15
  93.     PSET (x + size, y), 8
  94.  
  95.     y = y - 1
  96.     Delay DL
  97.  
  98.   NEXT
  99.  
  100.   ' Move right/left
  101.   xl = x - size
  102.   xr = x + size
  103.   FOR i = 0 TO 100 * ABS(icol - ocol) - 1
  104.  
  105.     ' Move RIGHT
  106.     IF dir > 0 THEN
  107.  
  108.       LINE (xl, y)-(xl, y + 10), 0
  109.       LINE (xl + 1, y)-(xl + 1, y + 10), 15
  110.       LINE (xr, y)-(xr, y + 10), cl
  111.       PSET (xr, y), 15
  112.       PSET (xr, y + 10), 8
  113.       LINE (xr + 1, y)-(xr + 1, y + 10), 8
  114.       xl = xl + 1
  115.       xr = xr + 1
  116.       Delay DL
  117.  
  118.     ELSE
  119.  
  120.       LINE (xr, y)-(xr, y + 10), 0
  121.       LINE (xr - 1, y)-(xr - 1, y + 10), 15
  122.       LINE (xl, y)-(xl, y + 10), cl
  123.       PSET (xl, y), 15
  124.       PSET (xl, y + 10), 8
  125.       LINE (xl - 1, y)-(xl - 1, y + 10), 8
  126.       xl = xl - 1
  127.       xr = xr - 1
  128.       Delay DL
  129.  
  130.     END IF
  131.  
  132.   NEXT
  133.  
  134.   ' Incremental DOWN
  135.   FOR i = 0 TO ocnt
  136.  
  137.     LINE (xl, y)-(xr, y), 0
  138.     LINE (xl + 1, y + 10)-(xr - 1, y + 10), cl
  139.     LINE (xl, y + 1)-(xr, y + 1), 15
  140.     PSET (xl, y + 10), 15
  141.     LINE (xl, y + 11)-(xr, y + 11), 8
  142.  
  143.     ' Draw if real column
  144.     IF i > 10 THEN LINE (xl + size - 1, y)-(xr - size + 1, y), 6
  145.  
  146.     y = y + 1
  147.     Delay DL
  148.  
  149.   NEXT
  150.  
  151. END SUB
Advertisement
RAW Paste Data Copied
Advertisement