Difference between revisions of "Tutorial Project. Release 1"

From wiki.visual-prolog.com

m
m
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{spbCopyright}}
+
{{PolylineTemplateEn
 +
|goal=
 +
The demonstration of the simplest ways of the Visual Prolog language programming
 +
|goalContent=
 +
|code=
 +
*Author: Elena Efimova
 +
*The source:[http://www.progz.ru/forum/index.php?showtopic=34097&pid=153443&st=0&#entry153443#2 http://www.progz.ru/forum/index.php?showtopic=34097&pid=153443&st=0&#entry153443#2]
 +
*The User Interface type – console
 +
*The size of the playing field is set 5x6 points. It is not recommended to set the size of the field more than 36 points (say 6 x 6) due to the inefficient algorithm used
 +
|functionality=
 +
Please follow the instructions of the application.
 +
|install={{PolylineInstall}}
  
This program is part of the [[Tutorial Project Evolutions]] article.
+
|open=
 
+
{{Polyline1_10Open|project='''Examples\Polyline\Polyline1\Polyline1.prj'''}}
{{PolyLine Game Rules}}
 
 
 
{{note|content=
 
It is not recommended to set the size of the field more than 36 points (say 6 x 6) due to the inefficient algorithm used
 
}}  
 
 
 
The source: [http://www.progz.ru/forum/index.php?showtopic=34097&pid=153443&st=0&#entry153443#2 http://www.progz.ru/forum/index.php?showtopic=34097&pid=153443&st=0&#entry153443#2]
 
 
 
To run the game please make the steps as follows:
 
 
 
*Create the new project with the User Interface strategy Console
 
*In the created project replace the entire content of the file "main.pro" by the code shown below.
 
*While building the project answer "Add All", when any dialog with proposals to include any packages to the project appears.
 
  
 +
{{Polyline1-06Copy_Paste}}
 +
|run=Run the application using the “E” of the IDE or run the executable at the directory EXE.
 
<vip>
 
<vip>
 
/*****************************************************************************
 
/*****************************************************************************
Copyright (c) 2007-2010. Elena Efimova
 
Tested for Vip7.3
 
 
 
Written by Elena Efimova
 
Written by Elena Efimova
  
 
Translated by Victor Yukhtenko
 
Translated by Victor Yukhtenko
Copyright (c) 2007-2010. Victor Yukhtenko
 
 
******************************************************************************/
 
******************************************************************************/
 
goal
 
goal
Line 148: Line 144:
 
         _ = readLine().
 
         _ = readLine().
 
   
 
   
end implement main</vip>
+
end implement main
 +
</vip>
  
=References=
+
|rusver=
 
[[ru:Учебный Проект. Релиз 1]]
 
[[ru:Учебный Проект. Релиз 1]]
[[Category:Tutorial Project Evolutions]]
+
}}

Latest revision as of 14:34, 1 April 2011

Written by Victor Yukhtenko. Email: victor@pdc.spb.su

The programs listed below are part of the Evolutions Tutorial Project article.

Game Rules ...

Goal

The demonstration of the simplest ways of the Visual Prolog language programming

Functionality

Please follow the instructions of the application.

Code

  • Tested with Visual Prolog 7.3 build 7302.

Install

To get the full set of projects of the series of the Polyline projects, please download archives using links at PDC forum:

  • VipSpbSDK_PE_73_Examples_Polyline_1_14.zip
  • VipSpbSDK_PE_73_Tools_Polyline_1_14.zip]

to any convenient directory. The directory named VipSpbSDK will be created automatically.

If you have the VipSpbSDK installed, then please open project Examples\Polyline\Polyline1\Polyline1.prj.

If you do not have the full set of examples from the VipSpbSDK, then please do as follows:

  • Create the new project using the interface strategy console
  • Build the project
  • Replace the content of the file "main.pro" by the code proposed below.
  • Build the project again. While building the project please respond “Add All”, when there will be the proposal to include additional packages to the project.

Run

Run the application using the “E” of the IDE or run the executable at the directory EXE.

/*****************************************************************************
Written by Elena Efimova
 
Translated by Victor Yukhtenko
******************************************************************************/
goal
    mainExe::run(main::run).
 
 
implement main
    open core, console
 
constants
    className = "PolyLine".
    classVersion = "1.0".
 
clauses
    classInfo(className, classVersion).
 
domains
    cell = c(unsigned16, unsigned16).
    cells = cell*.
 
class predicates
    wins: (positive, positive, cells, cells) nondeterm (i,i,i,o).
clauses
    wins(_Player1, _Player2, PolyLine, PolyLine1):-
        step(PolyLine, PolyLine1, Cell),
        list::isMember(Cell, PolyLine).
    wins(Player1, Player2, PolyLine, PolyLine1):-
        step(PolyLine, PolyLine1, _),
        not(wins(Player2, Player1, PolyLine1, _)).
 
class predicates
    move: (positive, cells).
clauses
    move(2, PolyLine):-
        step(PolyLine, PolyLine1, Cell),
        list::isMember(Cell, PolyLine),
        !,
        show(PolyLine1),
        setLocation(console_native::coord(0, 14)),
        write("Computer Won!\n\n", PolyLine1).
    move(2, PolyLine):-
        wins(2, 1, PolyLine, PolyLine1),
        !,
        move(1, PolyLine1).
    move(2, PolyLine):-
        step(PolyLine, PolyLine1, _),
        !,
        move(1, PolyLine1).
    move(1, PolyLine):-
        show(PolyLine),
        setLocation(console_native::coord(0, 14)),
        write(PolyLine),
        std::repeat(),
            write( "\n\nIt is your turn.\nEnter the new cell as c(2,3): "),
            hasDomain(cell, К),
            К = read(), 
            clearInput(),
        step(PolyLine, PolyLine1, К),
        !,
        if list::isMember(К, PolyLine) then
            write("\nCongratulations! You have won!")
        else
            move(2, PolyLine1)
        end if.
    move(_, _).
 
class predicates
    step: (cells, cells, cell) nondeterm (i,o,o) (i,o,i).
clauses
    step([A, B | PolyLine], [X, A, B | PolyLine], X):-
        candidate(A, X),
        not(X = B).
    step(PolyLine, PolyLine1, X):-
        PolyLine2 = list::reverse(PolyLine),
        PolyLine2 = [A, B | _],
        candidate(A, X),
        not(X = B),
        PolyLine1 = list::append(PolyLine, [X]).
 
class predicates
    candidate: (cell, cell) nondeterm (i,o) (i,i).
clauses
    candidate(c(X, Y), c(X - 1, Y)):- X > 1.
    candidate(c(X, Y), c(X + 1, Y)):- X < 6.
    candidate(c(X, Y), c(X, Y - 1)):- Y > 1.
    candidate(c(X, Y), c(X, Y + 1)):- Y < 5.
 
class predicates
    show: (cells).
clauses
    show(PolyLine):-
        clearOutput(),
        foreach I = std::fromTo(1, 6) do
            setLocation(console_native::coord(3*I, 0)), write(I)
        end foreach,
        foreach J = std::fromTo(1, 5) do
            setLocation(console_native::coord(0, 2*J)), write(J)
        end foreach,
        foreach c(X, Y) = list::getMember_nd(PolyLine) do
            setLocation(console_native::coord(3*X, 2*Y)), write("*")
        end foreach.
 
clauses
    run():-
        init(),
        write("Wich player moves first? (1 - human, 2 - computer): "),
        Player = read(),
        clearInput(),
        if Player = 1 then
            write("Enter the start  of the line as  c(3,3): "),
            hasDomain(cell, К1),
            К1 = read(), 
            clearInput(),
            write("Enter the end of the line: "),
            hasDomain(cell, К2),
            К2 = read(), 
            clearInput(),
            move(2, [К1, К2])
        else
            move(1, [c(3, 3), c(4, 3)])
        end if,
        _ = readLine().
 
end implement main