Guest User

Untitled

a guest
Apr 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 7.94 KB | None | 0 0
  1. constants
  2.     max = 1000
  3.    
  4. domains
  5.     board = step*
  6.     x, y, sign = byte
  7.     tries = integer
  8.     step = symbol
  9.    
  10. facts
  11.     spot(board, x, y, sign)
  12.     route(board, integer, integer)
  13.     moves(board, board, board, integer)
  14.     bestmove(board, board, board, integer)
  15.     lastmoves(board, board, board, integer)
  16.  
  17. predicates
  18.     mygoal
  19.     routeprint
  20.     move(board, board)
  21.     equal(board, board)
  22.     solve(board, board, tries)
  23.     equalWO2(board, board, sign)
  24.     spotWOS(board, x, y, sign, sign)
  25.     difference(board, board, integer)
  26.     y_powerS(board, integer, sign)
  27.     setBM(board, board, board)
  28.     y_power(board, integer)
  29.     passed(board)
  30.  
  31. clauses
  32.     mygoal :-
  33.         assert(spot([last], 1, 1, 1)),
  34.         assert(spot([last], 2, 1, 2)),
  35.         assert(spot([last], 3, 1, 3)),
  36.         assert(spot([last], 1, 2, 8)),
  37.         assert(spot([last], 2, 2, 0)),
  38.         assert(spot([last], 3, 2, 4)),
  39.         assert(spot([last], 1, 3, 7)),
  40.         assert(spot([last], 2, 3, 6)),
  41.         assert(spot([last], 3, 3, 5)),
  42. /*
  43.         assert(spot([first], 1, 1, 2)),
  44.         assert(spot([first], 2, 1, 0)),
  45.         assert(spot([first], 3, 1, 3)),
  46.         assert(spot([first], 1, 2, 1)),
  47.         assert(spot([first], 2, 2, 8)),
  48.         assert(spot([first], 3, 2, 5)),
  49.         assert(spot([first], 1, 3, 7)),
  50.         assert(spot([first], 2, 3, 4)),
  51.         assert(spot([first], 3, 3, 6)),
  52.  
  53.         assert(spot([first], 1, 1, 0)),
  54.         assert(spot([first], 2, 1, 8)),
  55.         assert(spot([first], 3, 1, 3)),
  56.         assert(spot([first], 1, 2, 2)),
  57.         assert(spot([first], 2, 2, 1)),
  58.         assert(spot([first], 3, 2, 5)),
  59.         assert(spot([first], 1, 3, 7)),
  60.         assert(spot([first], 2, 3, 4)),
  61.         assert(spot([first], 3, 3, 6)),
  62. */
  63.         assert(spot([first], 1, 1, 1)),
  64.         assert(spot([first], 2, 1, 2)),
  65.         assert(spot([first], 3, 1, 3)),
  66.         assert(spot([first], 1, 2, 8)),
  67.         assert(spot([first], 2, 2, 5)),
  68.         assert(spot([first], 3, 2, 7)),
  69.         assert(spot([first], 1, 3, 4)),
  70.         assert(spot([first], 2, 3, 6)),
  71.         assert(spot([first], 3, 3, 0)),
  72.  
  73.         assert(moves([last], [], [first], 0)),
  74.         solve([last], [first], max),
  75.         routeprint./*;
  76.         nl,fail;
  77.         bestmove(A,B,C,D),write(A," ",B," ",C," ",D),nl,fail;
  78.         nl,fail;
  79.         lastmoves(A,B,C,D),write(A," ",B," ",C," ",D),nl,fail;
  80.         nl,fail;
  81.         moves(A,B,C,D),write(A," ",B," ",C," ",D),nl,fail;
  82.         nl,fail;
  83.         spot(A,B,C,D),write(A," ",B," ",C," ",D),nl,fail.*/
  84.  
  85.     difference(A, B, C) :-
  86.         spot(A, 1, 1, S11),
  87.         spot(A, 2, 1, S21),
  88.         spot(A, 3, 1, S31),
  89.         spot(A, 1, 2, S12),
  90.         spot(A, 2, 2, S22),
  91.         spot(A, 3, 2, S32),
  92.         spot(A, 1, 3, S13),
  93.         spot(A, 2, 3, S23),
  94.         spot(A, 3, 3, S33),
  95.         spot(A, X0a, Y0a, 0),
  96.         spot(B, X11, Y11, S11),
  97.         spot(B, X21, Y21, S21),
  98.         spot(B, X31, Y31, S31),
  99.         spot(B, X12, Y12, S12),
  100.         spot(B, X22, Y22, S22),
  101.         spot(B, X32, Y32, S32),
  102.         spot(B, X13, Y13, S13),
  103.         spot(B, X23, Y23, S23),
  104.         spot(B, X33, Y33, S33),
  105.         spot(B, X0b, Y0b, 0),
  106.         y_power(A, Y0), y_power(B, Y1), %write(A, " ", Y0), nl, write(B, " ", Y1), nl,
  107.         C = abs(X11-1) + abs(Y11-1) + abs(X21-2) + abs(Y21-1) + abs(X31-3) + abs(Y31-1) +
  108.             abs(X12-1) + abs(Y12-2) + abs(X22-2) + abs(Y22-2) + abs(X32-3) + abs(Y32-2) +
  109.             abs(X13-1) + abs(Y13-3) + abs(X23-2) + abs(Y23-3) + abs(X33-3) + abs(Y33-3) -
  110.             abs(X0b-X0a) - abs(Y0b-Y0a) + 3 * abs(Y1 - Y0).
  111.  
  112.     y_power(A, B) :-
  113.         y_powerS(A, B1, 1),
  114.         y_powerS(A, B2, 2),
  115.         y_powerS(A, B3, 3),
  116.         y_powerS(A, B4, 4),
  117.         y_powerS(A, B5, 5),
  118.         y_powerS(A, B6, 6),
  119.         y_powerS(A, B7, 7),
  120.         y_powerS(A, B8, 8),
  121.         B = B1 + B2 + B3 + B4 + B5 + B6 + B7 + B8.
  122.  
  123.     y_powerS(A, 1, S) :- spot(A, 2, 2, S), !.
  124.  
  125.     y_powerS(A, 0, 8) :- spot(A, 1, 1, 8), spot(A, 2, 1, 1), !.
  126.     y_powerS(A, 0, S) :- spot(A, 1, 1, S), spot(A, 2, 1, S1), S1 = S + 1, !.
  127.     y_powerS(A, 2, S) :- spot(A, 1, 1, S), spot(A, 2, 1, _), !.
  128.  
  129.     y_powerS(A, 0, 8) :- spot(A, 2, 1, 8), spot(A, 3, 1, 1), !.
  130.     y_powerS(A, 0, S) :- spot(A, 2, 1, S), spot(A, 3, 1, S1), S1 = S + 1, !.
  131.     y_powerS(A, 2, S) :- spot(A, 2, 1, S), spot(A, 3, 1, _), !.
  132.  
  133.     y_powerS(A, 0, 8) :- spot(A, 3, 1, 8), spot(A, 3, 2, 1), !.
  134.     y_powerS(A, 0, S) :- spot(A, 3, 1, S), spot(A, 3, 2, S1), S1 = S + 1, !.
  135.     y_powerS(A, 2, S) :- spot(A, 3, 1, S), spot(A, 3, 2, _), !.
  136.  
  137.     y_powerS(A, 0, 8) :- spot(A, 3, 2, 8), spot(A, 3, 3, 1), !.
  138.     y_powerS(A, 0, S) :- spot(A, 3, 2, S), spot(A, 3, 3, S1), S1 = S + 1, !.
  139.     y_powerS(A, 2, S) :- spot(A, 3, 2, S), spot(A, 3, 3, _), !.
  140.  
  141.     y_powerS(A, 0, 8) :- spot(A, 3, 3, 8), spot(A, 2, 3, 1), !.
  142.     y_powerS(A, 0, S) :- spot(A, 3, 3, S), spot(A, 2, 3, S1), S1 = S + 1, !.
  143.     y_powerS(A, 2, S) :- spot(A, 3, 3, S), spot(A, 2, 3, _), !.
  144.  
  145.     y_powerS(A, 0, 8) :- spot(A, 2, 3, 8), spot(A, 1, 3, 1), !.
  146.     y_powerS(A, 0, S) :- spot(A, 2, 3, S), spot(A, 1, 3, S1), S1 = S + 1, !.
  147.     y_powerS(A, 2, S) :- spot(A, 2, 3, S), spot(A, 1, 3, _), !.
  148.  
  149.     y_powerS(A, 0, 8) :- spot(A, 1, 3, 8), spot(A, 1, 2, 1), !.
  150.     y_powerS(A, 0, S) :- spot(A, 1, 3, S), spot(A, 1, 2, S1), S1 = S + 1, !.
  151.     y_powerS(A, 2, S) :- spot(A, 1, 3, S), spot(A, 1, 2, _), !.
  152.  
  153.     y_powerS(A, 0, 8) :- spot(A, 1, 2, 8), spot(A, 1, 1, 1), !.
  154.     y_powerS(A, 0, S) :- spot(A, 1, 2, S), spot(A, 1, 1, S1), S1 = S + 1, !.
  155.     y_powerS(A, 2, S) :- spot(A, 1, 2, S), spot(A, 1, 1, _), !.
  156.  
  157.     setBM(A, B, _) :- move(B, C), not(passed(C)), moves(A, _, B, K), N = K + 1, assert(moves(A, B, C, N)),
  158.             difference(A, C, X), F = N + X, assert(lastmoves(A, B, C, F)), fail.
  159.  
  160.     setBM(A, B, _) :- retract(lastmoves(A, _, B, _)), fail.
  161.  
  162.     setBM(A, _, _) :- retract(bestmove(A, _, _, _)), fail.
  163.  
  164.     setBM(A, _, _) :- lastmoves(A, B, C, D), assert(bestmove(A, B, C, D)), fail.
  165.  
  166.     setBM(A, _, _) :- bestmove(A, _, _, Q), bestmove(A, B, C, D), Q < D, retract(bestmove(A, B, C, D)), fail.
  167.  
  168.     setBM(A, _, C) :- bestmove(A, _, C, _), !.
  169.  
  170.     routeprint :- route(A, P, G), write(A, " ", P, " ", G), nl, fail.
  171.  
  172.     solve(A,B,P) :- equal(A,B), !, Q = max - P, moves(A, _, B, K), assert(route(B, K, Q)).
  173.    
  174. %   solve(A, B, N) :- K = max - N, G = K div 100, K = G * 100, write(K), nl, storage(), write(B), nl, bestmove(A, Q, W, E), write(Q, " ", W, " ", E), nl, nl, fail.
  175.  
  176.     solve(A,B,N) :- !, N > 0, K = N - 1, setBM(A, B, C), solve(A, C, K).
  177.  
  178.     move(B,C) :- spot(B, Xb, Yb, 0), Xb<3, Xc=Xb+1, Yc=Yb, spot(B, Xc, Yc, S),
  179.             str_int(STR1, S), concat(" -<|", STR1, STR2), concat(STR2, "|<- ", STR),
  180.             C=[STR/*-Ноль вправо-"*/|B], equalWO2(B,C,S).
  181.  
  182.     move(B,C) :- spot(B, Xb, Yb, 0), Xb>1, Xc=Xb-1, Yc=Yb, spot(B, Xc, Yc, S),
  183.             str_int(STR1, S), concat(" ->|", STR1, STR2), concat(STR2, "|>- ", STR),
  184.             C=[STR/*"-Ноль влево-"*/|B], equalWO2(B,C,S).
  185.  
  186.     move(B,C) :- spot(B, Xb, Yb, 0), Yb<3, Yc=Yb+1, Xc=Xb, spot(B, Xc, Yc, S),
  187.             str_int(STR1, S), concat(" -^|", STR1, STR2), concat(STR2, "|^- ", STR),
  188.             C=[STR/*"-Ноль вниз-"*/|B], equalWO2(B,C,S).
  189.  
  190.     move(B,C) :- spot(B, Xb, Yb, 0), Yb>1, Yc=Yb-1, Xc=Xb, spot(B, Xc, Yc, S),
  191.             str_int(STR1, S), concat(" -v|", STR1, STR2), concat(STR2, "|v- ", STR),
  192.             C=[STR/*"-Ноль вверх-"*/|B], equalWO2(B,C,S).
  193.  
  194.     equalWO2(A,B,S) :-
  195.         spot(A, 1, 1, S11),
  196.         spot(A, 2, 1, S21),
  197.         spot(A, 3, 1, S31),
  198.         spot(A, 1, 2, S12),
  199.         spot(A, 2, 2, S22),
  200.         spot(A, 3, 2, S32),
  201.         spot(A, 1, 3, S13),
  202.         spot(A, 2, 3, S23),
  203.         spot(A, 3, 3, S33),
  204.         spotWOS(B, 1, 1, S11, S),
  205.         spotWOS(B, 2, 1, S21, S),
  206.         spotWOS(B, 3, 1, S31, S),
  207.         spotWOS(B, 1, 2, S12, S),
  208.         spotWOS(B, 2, 2, S22, S),
  209.         spotWOS(B, 3, 2, S32, S),
  210.         spotWOS(B, 1, 3, S13, S),
  211.         spotWOS(B, 2, 3, S23, S),
  212.         spotWOS(B, 3, 3, S33, S).
  213.                
  214.     spotWOS(A, X, Y, 0, S) :- not(spot(A, X, Y, S)), assert(spot(A, X, Y, S)).
  215.     spotWOS(A, X, Y, S, S) :- not(spot(A, X, Y, 0)), assert(spot(A, X, Y, 0)).
  216.     spotWOS(A, X, Y, S1, S) :- S1 <> 0, S1 <> S, not(spot(A, X, Y, S1)), assert(spot(A, X, Y, S1)).
  217.  
  218.     equal(A,A).
  219.  
  220.     equal(A,B) :-
  221.         spot(A, 1, 1, S11),
  222.         spot(A, 2, 1, S21),
  223.         spot(A, 3, 1, S31),
  224.         spot(A, 1, 2, S12),
  225.         spot(A, 2, 2, S22),
  226.         spot(A, 3, 2, S32),
  227.         spot(A, 1, 3, S13),
  228.         spot(A, 2, 3, S23),
  229.         spot(A, 3, 3, S33),
  230.         spot(B, 1, 1, S11),
  231.         spot(B, 2, 1, S21),
  232.         spot(B, 3, 1, S31),
  233.         spot(B, 1, 2, S12),
  234.         spot(B, 2, 2, S22),
  235.         spot(B, 3, 2, S32),
  236.         spot(B, 1, 3, S13),
  237.         spot(B, 2, 3, S23),
  238.         spot(B, 3, 3, S33).
  239.  
  240.     passed(A) :- moves(_, _, B, _), equal(A,B).
  241.  
  242. goal
  243.     mygoal.
Add Comment
Please, Sign In to add comment