Solving game “Blobs” with Prolog

So my teacher give me an assigment that to me seems out of this worls or at least of my class, and well maybe is bacause we barely had practice with prolog, what I have to do is write a prolog program that solves the game Blobs from the page “mindgames”, I think I could do this on python or C++ cause I’m familiar with this codes, but I’m not at all familiar with prolog, I’m struggling a lot. This is what I think I should do:

  1. Have a function to tells me what has been painted and what not.
  2. Recursively search all the possible paths of known neighborgs that I should paint.
  3. Make a restriction of moves. Save the choosen color (or the move) in a list that I should print as an answer.

Lately what I did was search on the web on how to make list, and how to use matrix and actually trying to understand how it works. But prolog is not documented in a way that is easy to understand for me. My teacher said that I should separate the problem like this:

  1. Expand your search and mark eveyone that have the same color at the beggining.
  2. Paint the neighborgs that you can.
  3. Save this on a list to print
  4. Do it recursively

From internet I got the way to find nighborgs, and how to index a list into a matrix, but thats all, I don’t think I’m understanding how prolog works to do this.

What I have, and I don’t know if this would work for my problem

:- use_module(library(clpfd)).

% at, permite indexar la matriz
at(Mat, [Row, Col], Val) :- nth1(Row, Mat, ARow), nth1(Col, ARow, Val).

% indices, generador de indices de la matriz
add(X,L,Ls) :- Ls = [X|[L]].
indices1(L,X,Ls) :- maplist(add(X),L,Ls).
indices2(L1,L2,Li) :- maplist(indices1(L2),L1,Ls), append(Ls,Li).
indices(M,N,Is) :- numlist(1,M,LMs), numlist(1,N,LNs), indices2(LMs,LNs,Is).

% vecino, posiciones vecinas de [X,Y]
vecino([X,Y],[X,Y1]) :- Y1 #= Y+1.
vecino([X,Y],[X,Y1]) :- Y1 #= Y-1.
vecino([X,Y],[X1,Y]) :- X1 #= X+1.
vecino([X,Y],[X1,Y]) :- X1 #= X-1.

Thanks in advance.

Hi Daniela

I’ve seen your question on SO, identical to this post, but you should explain something more about the game itself. Searching on “mindgames”, seems the game is pretty visual, so you should first attempt to encode the relevant structure in a simple program (Prolog or whatever), and only then apply the relevant search or emulation techniques to reach a solution.

Also, I would advice that clp(FD) is an advanced topic, much better first to learn how plain Prolog can solve the task.

In my experience, clp(FD) not always gives a clearer or faster solution. You must follow its peculiar programming model to get a solution, something that requires from you a further learning step, and - most likely - is not required for your assignment.

Ciao, Carlo