# HP Prime for All

English  Русский
 Name Towers of Hanoi Description Simple implementation of the Towers of Hanoi game. Author Vlad A.B. (Vlad_AB)

EXPORT PIR_3() `BEGIN ```` LOCAL a, b, t, taut; LOCAL k1, k2, aut; LOCAL j, m, n; LOCAL vstp, hstp; LOCAL cl, c, d; LOCAL v, f; LOCAL k, t1, t2; LOCAL lst, stp; t := 0.3; f := 0; v := 0; k := 0; stp := 0; aut := 0; {{}, {}, {}}▶lst; {{}, {}, {}}▶cl; REPEAT INPUT(n, "Введите количество дисков", "n = ", "Натуральное число от 3 до 9", 3); UNTIL n ≥ 3 AND n ≤ 9 AND FP(n) == 0; REPEAT INPUT(taut, "Автоматическое решение", "Time(step) = ", "Длительность одного хода от 0 до 2 сек."); UNTIL taut ≥ 0 AND taut ≤ 2; CASE IF n == 3 THEN hstp := 15; vstp := 40; END; IF n == 4 THEN hstp := 12; vstp := 30; END; IF n == 5 THEN hstp := 10; vstp := 25; END; IF n == 6 THEN hstp := 8; vstp := 20; END; IF n == 7 THEN hstp := 7; vstp := 20; END; IF n == 8 THEN hstp := 6; vstp := 20; END; IF n == 9 THEN hstp := 5; vstp := 20; END; END; lst(1) := MAKELIST(X, X, 1, n, 1); RECT_P(); RECT_P(0, 0, 319, 239, #FF0000h, #FFFFFFh); FOR j FROM 0 TO n-1 DO IF even(j) THEN RECT_P(7+hstp*j, 239-vstp * (j+1), 104-hstp*j, 237-vstp*j, #000000h, #00FF00h); ELSE RECT_P(7+hstp*j, 239-vstp * (j+1), 104-hstp*j, 237-vstp*j, #000000h, #FF0000h); END; END; TEXTOUT_P("Количество шагов = "+STRING(stp), 2, 2, 7, #000000h); cl(1) := MAKELIST({4, 238-vstp * (X+1), 107, 238-vstp*X}, X, 0, n-1, 1); cl(2) := MAKELIST({108, 238-vstp * (X+1), 211, 238-vstp*X}, X, 0, n-1, 1); cl(3) := MAKELIST({212, 238-vstp * (X+1), 315, 238-vstp*X}, X, 0, n-1, 1); t1 := TICKS; REPEAT WAIT(t); CASE IF aut == 0 THEN REPEAT IF stp == 0 THEN f := ISKEYDOWN(30); END; IF f ≠ 0 THEN aut := 1; k1 := 1; k2 := 2; t := taut; ELSE 0▶k1; MOUSE(0)▶a; CASE IF a ≥ 4 AND a ≤ 107 THEN 1▶k1; END; IF a ≥ 108 AND a ≤ 211 THEN 2▶k1; END; IF a ≥ 212 AND a ≤ 315 THEN 3▶k1; END; END; END; UNTIL k1 ≠ 0 OR f ≠ 0; REPEAT IF f == 0 THEN 0▶k2; MOUSE(0)▶b; CASE IF b ≥ 4 AND b ≤ 107 THEN 1▶k2; END; IF b ≥ 108 AND b ≤ 211 THEN 2▶k2; END; IF b ≥ 212 AND b ≤ 315 THEN 3▶k2; END; END; END; UNTIL (k2 ≠ 0 AND k1 ≠ k2) OR f ≠ 0; END; IF aut ≠ 0 THEN IF v == 1 THEN v := 0; FOR j FROM 1 TO 3 DO IF SIZE(lst(j)) ≠ 0 THEN IF head(lst(j)) == 1 THEN k1 := j; END; END; END; FOR j FROM 1 TO 3 DO IF SIZE(lst(j)) == 0 THEN k2 := j; END; END; FOR j FROM 1 TO 3 DO IF SIZE(lst(j)) ≠ 0 THEN IF even(head(lst(j))) THEN k2 := j; END; END; END; ELSE v := 1; FOR j FROM 1 TO 3 DO IF SIZE(lst(j)) ≠ 0 THEN IF head(lst(j)) == 1 THEN m := remove(j, [1, 2, 3]); k1 := m(1); k2 := m(2); END; END; END; IF SIZE(lst(k1)) == 0 THEN k := k2; k2 := k1; k1 := k; END; IF SIZE(lst(k1)) ≠ 0 AND SIZE(lst(k2)) ≠ 0 THEN IF head(lst(k1)) > head(lst(k2)) THEN k := k2; k2 := k1; k1 := k; END; END; END; END; END; CASE IF SIZE(lst(k1)) ≠ 0 AND SIZE(lst(k2)) ≠ 0 THEN IF head(lst(k1)) < head(lst(k2)) THEN SIZE(lst(k1))▶a; SIZE(lst(k2)) + 1▶b; cl(k1, a)▶c; cl(k2, b)▶d; SUBGROB_P(G0, d(1), d(2), d(3), d(4), G1); BLIT_P(G0, d(1), d(2), d(3), d(4), G0, c(1), c(2), c(3), c(4)); BLIT_P(G0, c(1), c(2), c(3), c(4), G1); CONCAT(head(lst(k1)), lst(k2))▶lst(k2); tail(lst(k1))▶lst(k1); stp := stp+1; TEXTOUT_P(" ", 217, 2, 7, #FFFFFFh, 100, #FFFFFFh); TEXTOUT_P(STRING(stp), 217, 2, 7, #000000h, 100, #FFFFFFh); END; END; IF SIZE(lst(k1)) ≠ 0 AND SIZE(lst(k2)) == 0 THEN SIZE(lst(k1))▶a; 1▶b; cl(k1, a)▶c; cl(k2, b)▶d; SUBGROB_P(G0, d(1), d(2), d(3), d(4), G1); BLIT_P(G0, d(1), d(2), d(3), d(4), G0, c(1), c(2), c(3), c(4)); BLIT_P(G0, c(1), c(2), c(3), c(4), G1); CONCAT(head(lst(k1)), lst(k2))▶lst(k2); tail(lst(k1))▶lst(k1); stp := stp+1; TEXTOUT_P(" ", 217, 2, 7, #FFFFFFh, 100, #FFFFFFh); TEXTOUT_P(STRING(stp), 217, 2, 7, #000000h, 100, #FFFFFFh); END; END; UNTIL SIZE(lst(2)) == n OR SIZE(lst(3)) == n; t2 := TICKS; TEXTOUT_P("Решено за "+STRING(IP((t2-t1) / 1000)) + " сек.", 2, 30, 7, #000000h); IF stp == 2^n-1 THEN TEXTOUT_P("ПОЛНАЯ ПОБЕДА !", 70, 90, 7, #000000h); ELSE TEXTOUT_P("РЕШЕНО !", 110, 90, 7, #000000h); END; WAIT(5); "OK"; ```END;