How to set up and Print out a Grid in GNU Prolog

673 views Asked by At

I'm reading through these prolog tutorials and I feel like i'm getting a decent grasp on some of the concepts they use but i'm having an issue trying to implement a grid like system.

I know I could set this up with a list, but so far i'm drawing blanks on how to implement it, is there a resource or some type of source code I look at to guide me through how to do a grid system in prolog?

The grid system I want to make from scratch would be a 4x4 and it should print out to be like this

(4,1) (4,2) (4,3) (4,4)

(3,1) (3,2) (3,3) (3,4)

(2,1) (2,2) (2,3) (2,4)

(1,1) (1,2) (1,3) (1,4)

For the comments

The reason I need this in a grid is because i'm starting to build a maze like structure for a wumpus world i'm creating, I think this is how I should approach this but i'm having an issue creating a type of list to satisfy this type of structure I want.

This is to where I can assign a square(piece of the grid) such as (3,2) to be a pit or wumpus or gold in any of the squares I want for my agent to traverse this grid.

If this is the wrong way to approach this I would appreciate why it isn't and hopefully get some feedback on what I need to be concentrating on.

If this wrong I would appreciate any feedback regardless.

1

There are 1 answers

0
migfilg On BEST ANSWER

You can use a list of lists to represent your grid
[[4,1,I41], [4,2,I42], ... [0,4,I04]]
where each sublist has the coordinates and information on what is in each cell.

This list could then be rewritten into another similar list as a result of applying some operator to it (for instance, moving the content of a cell to another one). The rewriting will be done by appropriate predicates and this is the usual Logic Programming recipe in contrast to what happens in imperative languages where the content of the data structure would be modified by assignments to the cells.

There is a more elaborate approach if the grid is large, using a cyclic-term (rational tree) for representing the grid and "moving" in it, with a separate term for the grid contents that is rewritten as above.