Assignment question about lists using findall and between

So based on what you have said so far the following two predicates are required to be used.

in_shape(pos(Line,Column), shape(square,Dimension)) :-
    %les position doivent etre > 0.
    positif_position(pos(Line, Column)),
    %verifications des positions et de la dimension
    Dimension >= 0, 
    Line =< Dimension, 
    Column =< Dimension,
    %optionnel?
    shape(square,Dimension).
%Movments in a room.
move((state(pos(Line ,Column ), room(Index,  shape(Shape ,Dimension )), item(TheItem      ))),
     (state(pos(Line2,Column2), room(Index2, shape(Shape2,Dimension2)), item(CollectedItem))), 
     TimeSpent,Time) :-
    %Verify if the movement is done in the same room
    %If the index of the room is different, then there is a portal???

    Index =:= Index2, Shape =:= Shape2, %this might change
    %Verify if the resulting position of the movement is in the shape.
    in_shape(pos(Line2,Column2), shape(Shape2,Dimension)),

    %Definition of possible movements
    Right is Column+1, 
    Left is Column-1, 
    Up is Line+1, 
    Down is Line-1,

    %This is an if else statement inside another one.
    %If the item is a diagonalizer, then we can move in diag,
    %CollectedItem is a diagonaliser and Time remains unchanged.
    %else, we cant move in diag, CollectedItem is nothing and Time
    %is Time+1.
    (
        item(TheItem) 
    -> 
        CollectedItem = diagonalizer, 
        Time is TimeSpent,
        member(Line2,[Up,Down]), 
        member(Column2,[Left,Right]) 
    ; %else
        CollectedItem = nothing, 
        Time is TimeSpent+1,
        (
            Column=:=Column2 
        -> 
            member(Line2,[Up,Down])
        ; 
            Line2=:=Line, 
            member(Column2,[Left,Right])
        )
    ).

Are these predicates as listed above the way they were given? Did you add the comments?

Also the names of the variables to not follow the standard convention. When a variable represents the same thing going in an and out but changes in the predicate the standard is to name the incoming argument with an ending 0, the outgoing argument with no number and increment the ending number as the argument changes in the predicate.

In move/4 the variable Dimension2 is only used in the head of the predicate. That is a serious problem. Did the teacher just give you the head of the predicate, or the entire predicate?

Everything written in these predicates are from me, including the arguments. all he did is giving us the names inshape&move to help us structure the project.
This is what he gave us (only those names) :
in_shhape(Pos, Shape)
state(Position, Room, TheItem)
move(State1, State2, Time)
thats it

1 Like

Please post the entire assignment as given, even if it is in French, then I can translate it to may language. :smiley:

i think im going to get in trouble if i do :sob:

The current problem I have is what is valid for shape in in_shhape(Pos, Shape). You are using squares, but can it be rectangle, triangle, or what? Also what is to ensure that two shapes don’t overlap. Typically these problems are given with a list of facts that describe the rooms and then it easier to work on the problem.

If you are not sure if you can post the problem, then you really should see your teacher for help.

One way to get neighbouring diagonal squares in Prolog is like so:

% South East
getmove(pos(Line0, Column0), Dimension, pos(Line1, Column1)) :-
    Line0 < Dimension,
    Column0 < Dimension,
    succ(Line0, Line1),
    succ(Column0, Column1).

% South West
getmove(pos(Line0, Column0), Dimension, pos(Line1, Column1)) :-
    Line0 < Dimension,
    Column0 > 1,
    succ(Line0, Line1),
    succ(Column1, Column0).

% North East
getmove(pos(Line0, Column0), Dimension, pos(Line1, Column1)) :-
    Line0 > 1,
    Column0 < Dimension,
    succ(Line1, Line0),
    succ(Column0, Column1).

% North West
getmove(pos(Line0, Column0), _Dimension, pos(Line1, Column1)) :-
    Line0 > 1,
    Column0 > 1,
    succ(Line1, Line0),
    succ(Column1, Column0).

getmoves(pos(Line0, Column0), Dimension, Moves) :-
    findall(pos(Line1, Column1),
            getmove(pos(Line0, Column0),
                    Dimension,
                    pos(Line1, Column1)),
            Moves).

Note I’ve labeled the top left corner as pos(1,1) (indexing from one rather than zero) and the bottom right as pos(Dimension, Dimension).

Since there are up to four diagonal neighbours of a given square, this is an example of nondeterminism, where findall comes in handy to collect all possible answers into a list, which I’ve done with getmoves.

?- getmoves(pos(2,3), 8, Moves).
Moves = [pos(3, 4), pos(3, 2), pos(1, 4), pos(1, 2)].

?- getmoves(pos(1,1), 8, Moves).
Moves = [pos(2, 2)].

?- getmoves(pos(8,8), 8, Moves).
Moves = [pos(7, 7)].

?- getmoves(pos(1,4), 8, Moves).
Moves = [pos(2, 5), pos(2, 3)].

each room has a shape.
But i want to focus on square for now. two rooms cant overlap because each room has a shape and an index.(my choice)
(i dont know if i think too much in OOP)

OK

Then instead of walls each coordinate of the x,y system will be a room, e.g.

m n o p
i j k l
e f g h
a b c d

So as facts they would be

room(a,0,0).
room(b,1,0).
...
room(o,2,3).
room(p,3,3).

Now if a room was a rectangle, it could be

a a

or

a
a

or something similar

and in a building you could have

c h h g
c e f g 
c d d b
a a a b

but just focusing on squares and in_shape/2 for now.

In most programming languages you have inputs and outputs but in Prolog each argument can be an input, output or both as also noted by @joeblog. For now with in_shape/2 let just say that with in_shape(Pos,Shape) Pos is an output and Shape is an input.

With a set of facts for the room

g h i
d e f
a b c
room(a,0,0).
room(b,1,0).
room(c,2,0).
room(d,0,1).
room(e,1,1),
room(f,2,1).
room(g,0,2).
room(h,0,1).
room(i,0,2).

then in_shape/2 is as simple as

in_shape(pos(X,Y),Shape) :-
    room(Shape,X,Y).
?- in_shape(Pos,a).
Pos = pos(0, 0).

?- in_shape(Pos,b).
Pos = pos(1, 0).

?- in_shape(Pos,g).
Pos = pos(0, 2).

?- in_shape(Pos,h).
Pos = pos(0, 1).

extending this for other shapes is just a matter of changing the room/3 facts.

e.g.

a a a b
room(a,0,0).
room(a,1,0).
room(a,2,0).
room(b,3,0).

This even allows for rooms that are not square or rectangle, e.g.

a b c
a a a 

At this point it looks like you should have enough with your understanding of move to get from one room to the next. (This assumes all rooms have a door to each other). Latter you can add facts that state which rooms connect to which rooms and those will be used to remove moves that are not allowed.

I used facts to represent the rooms because so far based on your explaining the assignment to me that is allowed.

The next step if I were doing this would be to do something like @joeblog did with finding the rooms that are adjacent to each other. Then each of these moves can be combined into the final path as it seems that the problem is that you are given a starting potion and an item at a location and you have to find the shortest path from location to item using dijkstra algorithm

LMAO wait I think we dont see the same thing.

room1           room2           room3
. . . . .       . . . . x       . . . . e
. t . . .       . . . . .       . . . x .
. . . . .       . . t . .       . . . . .
. . . . .       . . . . .       . . . . .
s . . . .       . . . . .       . . . . .

These are the rooms.
s= start ->pos(Line,Column)
e= end
t&x = portals

Are we good ?

Is this meant for me? I take it that it does because of the image.

You are using the word portal, but to me that means something like in a game that takes you one from place to another instantly even over long distances. A portal can also mean a door that connects two places that are right next to each other. So when you say portal you do not mean door? In other words you are using one word for portal and another for door?

1 Like

i use one word its portal, not really a door because doors need to have walls.
Its just a hole you jump in and you pop up in a specific position in the other room (its a view from up ofc)

Nevermind i solved it :wink:

1 Like

Great!

Maybe you should share what you found. We could look it over and give you some feedback.

Some of us can read French. :wink:
And “Prolog” derives from “Programmation en Logique”.

One way to proceed is to think about how to represent your problem’s facts in a relational database … this can be directly translated into Prolog; predicates can be written that validate the constraints you desire, or that generate a set of possible solutions that are then combined with the constraints (so-called “generate&test”).

1 Like

hahaha yeahh sure but after the deadline tho,
So i have no risk having my solution copied x) !!

Then send it to me as a private message and I can give you feed back without others seeing it. Just click my icon and then click message in the opened page.

1 Like

haha newbies cant send messages x)

Thanks, hopefully I can remember that next time I send such a message.

You are now Trust Level 1, try it now.