Tutorial Project. Release 1

From wiki.visual-prolog.com

Jump to: navigation, search

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

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

Game Rules ...

Contents

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
Personal tools
In other languages