home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / DEMO / CRYPT.LF < prev    next >
Text File  |  1996-06-04  |  7KB  |  349 lines

  1. % FILE. . . . . /amd/brie/home/brie/hak/life/crypt.lf
  2. % EDIT BY . . . Hassan Ait-Kaci
  3. % ON MACHINE. . Chaumes.Cs.Sfu.Ca
  4. % STARTED ON. . Fri Nov 18 10:01:47 1994
  5.  
  6. % Last modified on Mon Dec 05 14:32:35 1994 by Hak
  7.  
  8. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  9. % The SEND+MORE=MONEY puzzle.
  10. % Author: Hassan Ait-Kaci
  11.  
  12. module("crypt") ?
  13. public(crypt) ?
  14.  
  15. %%%%%%%%%%%%%%%%%%%
  16. % THE PUZZLE SOLVER
  17. %%%%%%%%%%%%%%%%%%%
  18.  
  19. clues([B0,B1,B2,B3,B4])
  20.     :-
  21.     B0 = @                    (bot => M),
  22.     B1 = @(fst => S, snd => M, thd => O),
  23.     B2 = @(fst => E, snd => O, thd => N),
  24.     B3 = @(fst => N, snd => R, thd => E),
  25.     B4 = @(fst => D, snd => E, thd => Y),
  26.  
  27.     % The all-distinct constraints:
  28.     diff_list([S,E,N,D,M,O,R,Y]),
  29.  
  30.     % Solutions where M=0 are uninteresting:
  31.     M=1,
  32.     
  33.     % The arithmetic constraints:
  34.     C3 + S + M = O + 10*M,
  35.           C2 + E + O = N + 10*C3,
  36.           C1 + N + R = E + 10*C2,
  37.              D + E = Y + 10*C1,
  38.  
  39.     % Generating binary digits:
  40.     C3=carry,
  41.     C2=carry,
  42.     C1=carry,
  43.  
  44.     % Generating decimal digits:
  45.           S=decimal,
  46.           E=decimal,
  47.           N=decimal,
  48.           D=decimal,
  49.     O=decimal,
  50.           R=decimal,
  51.           Y=decimal.
  52.  
  53. decimal -> {0;1;2;3;4;5;6;7;8;9}.
  54.  
  55. carry -> {0;1}.
  56.  
  57. diff_list([]).
  58. diff_list([H|T]) :-
  59.     generate_diffs(H,T),
  60.     diff_list(T),
  61.     H>=0, H=<9.
  62.  
  63. generate_diffs(H,[]).
  64. generate_diffs(H,[A|T]) :-
  65.     generate_diffs(H,T),
  66.     A=\=H.
  67.  
  68. %%%%%%%%%%%%%%%%%%%%%%%%%
  69. % THE GRAPHICAL INTERFACE
  70. %%%%%%%%%%%%%%%%%%%%%%%%%
  71.  
  72. import("xtools") ?
  73. import("xtools_utils")?
  74.  
  75. % The necessary widgets:
  76.  
  77. s_box <| frame.
  78. s_box <| field.
  79. s_box <| text_box.
  80.  
  81. :: s_box(frame_state => true,
  82.      text => " ").
  83.  
  84. slot_box := s_box(text_color_id => z_color,
  85.           font_id => title_font).
  86.  
  87. time_box := s_box(font_id => z_font,
  88.           text_color_id => z_color).
  89.  
  90. l_box := text_box(offset => -d_border,
  91.           font_id => title_font).
  92.  
  93. p_button := push_button(font_id => z_font).
  94.  
  95. col_box(S1:string,S2:string,S3:string) -> B
  96.     |
  97.     Fst = num_box(S1),
  98.     Snd = num_box(S2),
  99.     Thd = num_box(S3),
  100.  
  101.     same_size([Fst,Snd,Thd]),
  102.  
  103.     B = vc_list [Top:vc_list[Fst,Snd],v_box(20),Thd],
  104.  
  105.     B = @(fst => Fst.slot, snd => Snd.slot, thd => Thd.slot, top => Top).
  106.  
  107. num_box(S:string) -> B
  108.     |
  109.     B = hc_list [l_box(text => S),Slot:slot_box],
  110.     B = @(slot => Slot).
  111.  
  112. fst_box(S:string) -> B
  113.     |
  114.     B = vc_list [Top:box,v_box(20), Bot:num_box(S)],
  115.     B = @(bot => Bot.slot, top => Top).
  116.     
  117. % The control and display panel:
  118.  
  119. crypt :-
  120.     (
  121.         C = get_choice,
  122.         Q = p_button(text => "QUIT",
  123.              action => (set_choice(C),fail)),
  124.  
  125.         G = p_button(text => "SOLVE",
  126.              action => (solve(Ls),
  127.                     reset_state(M,false))),
  128.  
  129.         M = p_button(text => "MORE",
  130.              action => (reset_text(TB,"Retrying..."),
  131.                     reset_boxes(Ls),
  132.                     try_more,
  133.                     beep,
  134.                     reset_text(TB,"No more!"))),
  135.  
  136.         A = p_button(text => "ANIMATE",
  137.              action => (animate(Ls),
  138.                     reset_state(R,false),
  139.                     reset_state(M,false))),
  140.  
  141.         R = p_button(text => "RESET",
  142.              action => (stop(Stop),
  143.                     reset_all(Ls))),
  144.  
  145.         same_size([Q,G,M,A,R]),
  146.  
  147.             B0 = fst_box        ("M"),
  148.         B1 = col_box    ("S","M","O"),
  149.         B2 = col_box    ("E","O","N"),
  150.         B3 = col_box    ("N","R","E"),
  151.         B4 = col_box    ("D","E","Y"),
  152.  
  153.         same_size(Bs:[B0,B1,B2,B3,B4]),
  154.  
  155.         same_size([B0.top,B1.top]),
  156.  
  157.         Columns = frame_box(ht_list Bs, padding => 10),
  158.  
  159.         TitleBox = fancy_text_box(text => "Send+More=Money",
  160.                       font => title_font,
  161.                       colors => [r,i,g,b,y]),
  162.         
  163.         TimeBox = padded_box
  164.           (padding => 10,
  165.            vc_list [frame_box(TB:time_box(width => 120,
  166.                           height => 50,
  167.                           text => " "),
  168.                   padding => 3),
  169.             text_box(text => "Total Time",
  170.                  font_id => z_font)
  171.                ]
  172.          ),
  173.  
  174.         Panel = panel(title => "Cryptarithmetic")
  175.           containing
  176.           padded_box(padding => 20,
  177.              ht_list [vl_list [Q, v_box(10),
  178.                        G, v_box(10),
  179.                        M, v_box(10),
  180.                        A, v_box(10),
  181.                        R
  182.                       ],
  183.                   vr_list [TitleBox,
  184.                        v_box(30),
  185.                        hb_list
  186.                          [h_box(10),TimeBox,
  187.                           h_box(10),Columns]
  188.                       ]
  189.                  ]),
  190.  
  191.         create_box(Panel),
  192.  
  193.         Ls = map(boxify,Bs)
  194.            & @(timebox => TB,morebox=>M,stopbox=>R,stop=>Stop)
  195.     ;
  196.         succeed
  197.     ).
  198.     
  199. boxify(B) -> @(box=>B).
  200.  
  201. % Displaying the solutions:
  202.  
  203. display_digits([F|R]) :- display_fst(F), display_cols(R).
  204.  
  205. display_fst(@(box=>@(bot=>Bot_box),
  206.           bot=>Bot))
  207.     :- set_slot(Bot_box,Bot).
  208.  
  209. display_cols([H|Ls]) :- !, display_col(H), display_cols(Ls).
  210. display_cols([]).
  211.  
  212. display_col(@(box=>@(fst=>Fst_box,
  213.              snd=>Snd_box,
  214.              thd=>Thd_box),
  215.           fst => Fst,
  216.           snd => Snd,
  217.           thd => Thd))
  218.     :-
  219.     set_slot(Fst_box,Fst),
  220.     set_slot(Snd_box,Snd),
  221.     set_slot(Thd_box,Thd).
  222.  
  223. set_slot(B,Value:int)
  224.     -> true
  225.     |
  226.     (0 =< Value, Value =< 9, !,
  227.      (   reset_text(B,psi2str(Value))
  228.      ;   % Erase this slot upon backtracking
  229.          reset_text(B," "),
  230.          fail
  231.      )
  232.     )
  233.     ;
  234.     succeed.
  235.     
  236. % Actions for the control panel buttons:
  237.  
  238. persistent(is_reset)?
  239. is_reset <<- true?
  240.  
  241. solve(Ls) :-
  242.     cond(is_reset,
  243.          succeed,
  244.          reset(Ls)),
  245.     is_reset <<- false,
  246.     reset_text(Ls.timebox,"Solving..."),
  247.     solution(Ls),
  248.     display_digits(Ls).
  249.  
  250. animate(Ls:@(timebox=>TB,
  251.          morebox=>M,
  252.          stop=>Stop))
  253.     :-
  254.     cond(is_reset, succeed, reset(Ls)),
  255.     reset_text(TB,"Animating..."),
  256.     (
  257.         Stop = get_choice,
  258.         display_digits(Ls),
  259.         solution(Ls)
  260.     ;
  261.         reset(Ls) % reached by STOP only
  262.     ).
  263.  
  264. stop(Stop) :-
  265.     cond(is_value(Stop),
  266.          (set_choice(Stop),fail),
  267.          succeed).
  268.  
  269. reset_all(Ls) :- cond(is_reset,succeed,reset(Ls)).
  270.  
  271. reset(Ls:[@(box=>B1),
  272.       @(box=>B2),
  273.       @(box=>B3),
  274.       @(box=>B4),
  275.       @(box=>B5)]
  276.     (timebox => TB,morebox=>M,stopbox=>S,stop=>Stop))
  277.     :-
  278.     reset_text(TB," "),
  279.     reset_boxes(Ls),
  280.     Ls <- [@(box=>B1),@(box=>B2),@(box=>B3),@(box=>B4),@(box=>B5)],
  281.     Ls = @(timebox => TB,morebox=>M,stopbox=>S,stop=>Stop),
  282.     is_reset <<- true.
  283.  
  284. reset_boxes([F|R]) :- reset_fst(F), reset_cols(R).
  285.  
  286. reset_fst(@(box=>@(bot=>Bot_box))) :- reset_text(Bot_box," ").
  287.  
  288. reset_cols([H|Ls]) :- !, reset_col(H), reset_cols(Ls).
  289. reset_cols([]).
  290.  
  291. reset_col(@(box=>@(fst=>Fst_box,
  292.            snd=>Snd_box,
  293.            thd=>Thd_box)))
  294.     :-
  295.     reset_text(Fst_box," "),
  296.     reset_text(Snd_box," "),
  297.     reset_text(Thd_box," ").
  298.  
  299. % Colors and fonts
  300.  
  301. color('Blue') -> blue.
  302. color('Green') -> green.
  303. color('Ivory') -> wheat.
  304. color('Red') -> red.
  305. color('Yellow') -> yellow.
  306.  
  307. def_color(main_colors,r,red)?
  308. def_color(main_colors,i,wheat)?
  309. def_color(main_colors,g,green)?
  310. def_color(main_colors,b,blue)?
  311. def_color(main_colors,y,yellow)?
  312.  
  313. def_color(main_colors,z_color,'blue violet')?
  314.  
  315. def_font(z_font,
  316.      new_font(cond(life_demo#using_demo_fonts,
  317.                "terminal_bold_narrow18",
  318.                "-adobe-helvetica-bold-r-narrow--17-120-100-100-p-72-*")))?
  319.  
  320. def_font(title_font,
  321.      new_font(cond(life_demo#using_demo_fonts,
  322.                "terminal_bold_narrow28",
  323.                "-adobe-helvetica-bold-r-narrow--25-180-100-100-p-116-*")))?
  324.  
  325. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  326. % PUTTING TOGETHER THE SOLVER AND THE GRAPHICAL INTERFACE
  327. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  328.  
  329. persistent(try_more)?
  330. try_more <<- succeed?
  331.  
  332. solution(Ls:@(timebox => B))
  333.  
  334.     :-
  335.     try_more <<- fail,
  336.     A=cpu_time,
  337.     clues(Ls),
  338.     Time = cpu_time-A,
  339.     reset_text(B,strcon(psi2str(Time)," s")).
  340.  
  341. solution(list(timebox => B))
  342.     :-
  343.     reset_text(B,"No more!"),
  344.     is_reset <<- false,
  345.     beep,
  346.     try_more <<- succeed.
  347.  
  348. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  349.