简体   繁体   中英

Prolog: where to begin solving Minesweeper-like puzzle?

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM