Reputation: 25
I am trying to get a grid to show in prolog, rather than getting the listing
of it.
Here is what I have so far
sB:-
showBoard.
showBoard(Row) :-
setof([Row,Col,Object,Visible],square(Row,Col,Object,Visible),RList),
showRow(RList),
writeln(''),
NextRow is Row - 1,
showBoard(NextRow).
This is something new that I am trying to test out to see if I can get this or not. Am I on the right track?
EDIT
For a task, we have to generate a grid through code,
Here is what I am trying to get....
I am using square/3
, getting back square(x,y,object). But I hope to step it up to square/4
, so I can bring in the visibility of the grid, meaning the robot can only see around him, one square left, right, up and down, until he finds his glasses.
== == == == == == == == == ==
|| x x x x x x x x ||
|| x x x x x x x x ||
|| x x x x x x x x ||
|| x x x x x x x x ||
|| x x x x x x x x ||
|| x x x x x x x x ||
|| x x x x x x x x ||
== == == == == == == == == ==
Upvotes: 0
Views: 139
Reputation:
The easiest would be to "scan" your board left-to-right, top-to-bottom (as this is how we output to a console), and see if there is anything to be shown for this particular square. Assuming that you are using format
for writing (easier to control the formatting of the output), and assuming that you know in advance the size of your board, and your x and y coordinates start at the top left corner, you need to:
Or, in code:
show_board(Rows, Cols) :-
show_rows(1, Rows, Cols).
show_rows(R, Rows, Cols) :-
( R =< Rows
-> show_row(R, 1, Cols),
R1 is R + 1,
show_rows(R1, Rows, Cols)
; true
).
show_row(R, C, Cols) :-
( C =< Cols
-> show_square(R, C) % or maybe show_square(C, R)?
C1 is C + 1,
show_row(R, C1, Cols)
; true
).
% show_square should always succeed!
show_square(R, C) :-
( square(R, C, Obj /* additional arguments? */)
-> draw(Obj /* additional arguments */)
; draw_not_visible
).
This could be a starting point. It could be done more "fancy" but this is a perfectly valid approach. Drawing an object depends on what your object is, and drawing the boundary around the grid is trivial.
Upvotes: 1