Prolog: where to begin solving Minesweeper-like puzzle?

我的未来我决定 提交于 2019-12-11 01:28:10

问题


I need to write something like a minesweeper in prolog. I am able to do that in "normal" language but when i try to start coding with prolog i totally don't know how to start. I need some sort of tips. Input specification:

Board size: m × n (m, n ∈ {1,...,16}), list of triples (i, j, k), where i ∈ {1,...,m}, j ∈ {1,...,n}, k ∈ {1,...,8}) describing fields with numbers.

For example:

5
5
[(1,1,1), (2,3,3), (2,5,2), (3,2,2), (3,4,4), (4,1,1), (4,3,1), (5,5,2)].

Output: list of digits and the atoms * (for treasure) and (for blank fields). It is a representation of puzzle solution.

Rules of this puzzle: In 20 fields of a board there are hidden treasures. A digit in a field represents how many neighbour-fields have a treasure. There are no treasures in fields with a digit. Mark all fields with a treasure.

You need to guess how many treasures are hidden in diagonals.

I would be grateful for any tips. I don't want full solution, I want to write it by my own, but without clues I am not able to do that.


回答1:


A matrix is usually handled as a list of list, you can build using length/2 and findall/3. A matrix of empty variables (where you will place values while guessing....)

build_matrix(NRows, NCols, Mat) :-
  findall(Row, (between(1, NRows, _), length(Row, NCols)), Mat).

Accessing elements via coordinates can be done using nth1 (see here for another answer where you can find some detail: see cell/3).

Then you place all your triples constraints: there is finite number of ways of consuming the 'hidden treasure' counters, let Prolog search all the ways, enumerating adjacents.

Process the list of triples, placing each counter in compatible cells, with a recursive predicate. When the list ends, you have a guess.

To keep your code simpler, don't worry about indexes out of matrix bounds, remember that failures are 'normal' when searching...



来源:https://stackoverflow.com/questions/9983206/prolog-where-to-begin-solving-minesweeper-like-puzzle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!