FriedRike
FriedRike

Reputation: 145

How to create a pair and put it in a list in Prolog?

I'm programming in Prolog and I'm completely lost... I keep mixing things up programming languages even if they're nothing alike... I'm making a game called Meta-Forms as an assignment in which I have to place different kind of pieces in a 3x3 board according to some clues I'm given. For example, to put the yellow rectangle in the top left corner of the board 'Board', I'd have the following code: place(piece(rectangle, yellow), top, left, Board) According to the second and third argument, I have place it in the list B according to the position. [(top, left), (top, middle), (top, right), (center, left), (center, middle), (center, left), (bottom, left), (bottom, middle), (bottom, right)]. but instead of the position I need there to be "piece(rectangle, yellow)" Can you please help me make the "place" function? I've done this so far, but it doesn't work

put_piece(X, [X,_,_,_,_,_,_,_,_]) :- line_of(place(_,top,_,_), top), column_of(place(_,_,left,_), !.
put_piece(X, [_,X,_,_,_,_,_,_,_]) :- line_of(place(_,top,_,_), top), column_of(place(_,_,middle,_), middle), !.
put_piece(X, [_,_,X,_,_,_,_,_,_]) :- line_of(place(_,top,_,_), top), column_of(place(_,_,right,_), right), !.
put_piece(X, [_,_,_,X,_,_,_,_,_]) :- line_of(place(_,center,_,_), center), column_of(place(_,_,left,_), left), !.
put_piece(X, [_,_,_,_,X,_,_,_,_]) :- line_of(place(_,center,_,_), center), column_of(place(_,_,middle,_), middle), !.
put_piece(X, [_,_,_,_,_,X,_,_,_]) :- line_of(place(_,center,_,_), center), column_of(place(_,_,right,_), right), !.
put_piece(X, [_,_,_,_,_,_,X,_,_]) :- line_of(place(_,bottom,_,_), bottom), column_of(place(_,_,left,_), left), !.
put_piece(X, [_,_,_,_,_,_,_,X,_]) :- line_of(place(_,bottom,_,_), bottom), column_of(place(_,_,middle,_), middle), !.
put_piece(X, [_,_,_,_,_,_,_,_,X]) :- line_of(place(_,bottom,_,_), bottom), column_of(place(_,_,right,_), right), !.

line_of(place(_,Line,_,_), Line).
column_of(place(_,_,Column,_), Column).
cor_de(defPiece(_,Color), Color).
forma_de(defPiece(Form,_), Form).

place(P, L, Col, Tab) :-
    put_piece(P, Tab),
    line_of(place(_,L,_,_), L),
    column_of(place(_,_,Col,_), Col),
    color_of(Piece, cor),
    form_of(P, forma).

Also, I kind of need your help on how to create a pair. A piece is supposed to be a pair containing a form and a color such as position is supposed to be a pair of a column and a line.

EXAMPLE:

challenge(1, Board) :- 
    place(piece(square, blue), bottom, left, Board),
    place(piece(square, yellow), top, left, Board),
    place(piece(circle, blue), center, right, Board),
    place(piece(circle, red), top, middle, Board),
    place(piece(square, red), bottom, right, Board),
    place(piece(triangle, blue), top, right, Board),
    place(piece(circle, yellow), bottom, middle, Board),
    place(piece(triangle, red), center, left, Board),
    place(piece(triangle, yellow), center, middle, Board).

which should print this:

[piece(square,yellow),piece(circle,red),piece(triangle,blue),piece(triangle,red),piece(triangle,yellow),piece(circle,blue),piece(square,blue),piece(circle,yellow),piece(square,red)]

Upvotes: 1

Views: 393

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49920

Try redefining put_piece; for example, placing a piece in the top left would be:

put_piece(X, [X,_,_,_,_,_,_,_,_], top, left).

Then simplify place accordingly:

place(P,L,C,T) :-
    put_piece( P, T, L, C ).

I have no idea what you're doing with form/forma.

Upvotes: 1

Related Questions