Поздравительная Открытка

Материал из wikiru.visual-prolog.com

Разработка студентов 3-го курса (2007/2008) отделения интеллектуальных систем института лингвистики Российского государственного гуманитарного университета (Москва).

Для помещения текста в проект необходимо:

  • Создать проект с пользовательским интерфейсом типа console в Visual Prolog 7.2.
  • В построенном проекте заменить полностью код файла "main.pro" кодом, предложенным ниже
  • При построении проекта ответить на предложение о включении дополнительных пакетов в проект "Add All".
/*****************************************************************************
 
                         RSUH, Moscow
                         Russian State University for Humanities
                         Intelligent Systems Department
                         visit us at http://www.rsuh.ru
 
Лучше всего просматривать в консоли, растянутой по вертикали в два или более раз.
Best viewed in a console twice or more higher than the standart one.
 
******************************************************************************/
 
implement main
    open core, console, math, list, programControl
 
constants
    className = "main".
    classVersion = "1.0".
 
clauses
    classInfo(className, classVersion).
 
class facts
    i : positive:= 0.
    xp : unsigned16:= 30.
    yp : unsigned16:= 37.
    phrase : string:= "".
    n : unsigned:= 4.
 
class predicates
    ellipse : (integer Rx,integer Ry,integer XCent,integer YCent,string Symbol,positive Sleep).
    arc : (integer Rx,integer Ry,integer XCent,integer YCent,real Percent_Beg,real Percent_End,string Symb,positive Sleep).
    line : (console_native::coord P1,console_native::coord P2,string,positive Sleep).
    coord : (real X0, real Xspeed,real T,real Y0,real Yspeed, real G, string Symbol).
    firework : (boolean IsDrawing,real X0,real Y0,real G,positive Sleep,string Symbol).
 
constants
    % Congratulation-phrases for random choice 
    phraseList : string* =
        [
            "Счастливого Нового Года и Рождества!",
            "ВСЕ УМЕЮ, ВСЕ МОГУ, Я - СТУДЕНТ РГГУ!",
            "Мы программисты!",
            "From Russia with love!",
            "We are the programmers, my friend!",
            "Vivat Visual Prolog!",
            "We wish you a Merry Christmas and a happy New Year!",
            "Joyeux Noёl et bonne Année!",
            "Frohe Weihnachten und ein gutes neues Jahr!",
            "Buon natale e felice anno nuovo!",
            "Feliz Navidad y Prospero Ano Nuevo!",
            "Glaedelig jul og godt nytår!",
            "Kurisumasu Omedeto!",
            "Natale hilareet Annum Faustum!"
         ].
 
    % Symbols that will be randomly used for building a firework
    symbolList : string* = ["@","#","$","%","&","*","+","=","x","?"].
 
clauses    
    % drawing an ellipse with the center in (XCent;YCent)
    ellipse(Rx,Ry,XCent,YCent,Symbol,Sleep):-
        arc(Rx,Ry,XCent,YCent,0,1,Symbol,Sleep).
 
    % drawing an arc with the center in (XCent;YCent)     
    arc(Rx,Ry,XCent,YCent,Percent_Beg,Percent_End,Symbol,Sleep):-
        foreach X=std::fromTo(round(Percent_Beg*31),round(Percent_End*31)) do
            Angle=(2*pi)*(X/32),
            XCur=convert(unsigned16,XCent+round(Rx*cos(Angle))),
            YCur=convert(unsigned16,YCent-round(Ry*sin(Angle))),
            setLocation(console_native::coord(XCur,YCur)),
            write(Symbol),
            sleep(Sleep)
        end foreach.
 
    % Drawing a horizontal line 
    line(console_native::coord(X,Y1),console_native::coord(X,Y2),Symbol,Sleep):- 
        !,
        foreach Y=std::fromTo(Y1,Y2) do
            setLocation(console_native::coord(X, convert(unsigned16,Y))),
            write(Symbol),
            sleep(Sleep)
        end foreach.            
    % Drawing a non-horizontal line 
    line(console_native::coord(X1,Y1),console_native::coord(X2,Y2),Symbol,Sleep):-  
        foreach X=std::fromTo(X1,X2) do
            Z1=X-X1,
            if Y2>=Y1 then Z2=Y2-Y1 else Z2=-1*(Y1-Y2) end if,
            Y=convert(unsigned16,round(Z1*Z2/(X2-X1)+Y1)),
            setLocation(console_native::coord(convert(unsigned16,X),Y)),
            write(Symbol),
            sleep(Sleep)
        end foreach.
 
    /* Calculation of the trajectory of a thrown object (symbol) for forming a firework line.
        The object is thrown at certain angle and falls with free-fall acceleration G */    
    coord(X0,Xspeed,T,Y0,Yspeed,G,Symbol):-
        X=convert(unsigned16,round(X0+XSpeed*T)),
        Y=convert(unsigned16,round(Y0-(YSpeed*T-G*T^2/2))),
        setLocation(console_native::coord(X,Y)),
        write(Symbol).
 
    % Drawing a firework using random symbols from the list 
    firework(true,Xc,Yc,G,Sleep,_Symbol):- 
        !,
        foreach I=std::fromTo(0,29) do
            coord(42-Xc,7,I*0.1,20+Yc,7,G,nth(convert(positive,random(10)),symbolList)),
            coord(38-Xc,-6,I*0.1,20+Yc,7,G,nth(convert(positive,random(10)),symbolList)),
            coord(42-Xc,4,I*0.1,18+Yc,9,G,nth(convert(positive,random(10)),symbolList)),
            coord(38-Xc,-3,I*0.1,18+Yc,9,G,nth(convert(positive,random(10)),symbolList)),
            coord(41-Xc,5,I*0.1,21+Yc,5,G,nth(convert(positive,random(10)),symbolList)),
            coord(39-Xc,-5,I*0.1,21+Yc,5,G,nth(convert(positive,random(10)),symbolList)),
            coord(39-Xc,-2,I*0.1,18+Yc,11,G,nth(convert(positive,random(10)),symbolList)),            
            sleep(Sleep)
        end foreach.
   % Replacing the firework by that of blank spaces       
     firework(_,Xc,Yc,G,Sleep,Symbol):-
        foreach I=std::fromTo(0,29) do
            coord(42-Xc,7,I*0.1,20+Yc,7,G,Symbol),
            coord(38-Xc,-6,I*0.1,20+Yc,7,G,Symbol),
            coord(42-Xc,4,I*0.1,18+Yc,9,G,Symbol),
            coord(38-Xc,-3,I*0.1,18+Yc,9,G,Symbol),
            coord(41-Xc,5,I*0.1,21+Yc,5,G,Symbol),
            coord(39-Xc,-5,I*0.1,21+Yc,5,G,Symbol),
            coord(39-Xc,-2,I*0.1,18+Yc,11,G,Symbol),            
            sleep(Sleep)
        end foreach.    
 
clauses
    run():-
        init(),
        FSleep=15,
        setConsoleTitle("Happy New Year"),
 
       %A circle that will contain the characters        
        ellipse(18,11,40,12,"*",0),
        vpi::alarm(2),
 
        % Three
        arc(5,4,40,7,-0.25,0.40,"$",FSleep),
        arc(7,5,40,16,-0.43,0.25,"$",FSleep),
        sleep(750),
 
        % Erasing 'three' 
        % To erase something we replace it by the same figure formed by blank spaces        
        arc(5,4,40,7,-0.25,0.40," ",FSleep),
        arc(7,5,40,16,-0.43,0.25," ",FSleep),
 
        vpi::alarm(2),        
        % Two
        arc(7,4,40,7,-0.1,0.50,"$",FSleep),
        line(console_native::coord(34,18),console_native::coord(46,9),"$",FSleep),
        line(console_native::coord(33,19),console_native::coord(48,19),"$",FSleep),
        sleep(750),
 
        % Erasing 'two'     
        arc(7,4,40,7,-0.1,0.50," ",0),
        line(console_native::coord(34,18),console_native::coord(46,9)," ",FSleep),
        line(console_native::coord(33,19),console_native::coord(48,19)," ",FSleep),
 
        vpi::alarm(2),
        % One 
        line(console_native::coord(33,11),console_native::coord(43,5),"$",FSleep),
        line(console_native::coord(43,6),console_native::coord(43,19),"$$",FSleep),
        sleep(750),
 
        % Erasing 'one'         
        line(console_native::coord(33,11),console_native::coord(43,5)," ",FSleep),
        line(console_native::coord(43,6),console_native::coord(43,19),"  ",FSleep),
 
        vpi::alarm(2),
        % Zero 
        ellipse(9,7,40,12,"$$",FSleep),
        sleep(200),
 
        % Erasing the initial circle        
        ellipse(18,11,40,12," ",0),
        % Erasing 'zero'
        ellipse(9,7,40,12,"  ",0),
 
        LengthOfPhraseList = convert(unsigned,length(phraseList)),
 
        vpi::alarm(3),
 
        /* Infinite cycle that draws fireworks and writes random congratulation-phrases
            in a random place */
        std::repeat(),
            Xc=random(11),
            Yc=random(5),
            G=4+random(3),
 
              /* The first time a congratulation-phrase appears 
                 (all in all one phrase exists during three fireworks) */
            if i=0 then
                setLocation(console_native::coord(xp,yp)),
                phrase:=nth(convert(positive,n),phraseList)
            end if,
 
               /* The last time the current phrase appears; it is replaced by a string of blank spaces.
                Then new random location is chosen and a new congratulation-phrase is written */
            if i mod 3=0, i<>0 then
                setLocation(console_native::coord(xp,yp)),
                write(string::create(55," ")),
                xp:=convert(unsigned16,random(50)),
                yp:=convert(unsigned16,random(42)),
                setLocation(console_native::coord(xp,yp)),
                phrase:=nth(convert(positive,n),phraseList),
                write(phrase)
 
            % Not the first or the last time the current phrase appears 
            else
                setLocation(console_native::coord(xp,yp)),
                write(phrase)
            end if,
            i:=i+1,
            n:=random(LengthOfPhraseList),
 
            % Drawing a firework 
            firework(true,Xc,Yc,G,30," "),
 
            % Erasing the firework             
            firework(false,Xc,Yc,G,3," "),
        fail;
        _=readLine(),
        succeed().
 
end implement main
 
goal
    mainExe::run(main::run).

Ссылки