// Souffle - A Datalog Compiler
// Copyright (c) 2016, The Souffle Developers. All rights reserved
// Licensed under the Universal Permissive License v 1.0 as shown at:
// - https://opensource.org/licenses/UPL
// - <souffle root>/licenses/SOUFFLE-UPL.txt

#include "grid.dl"

// Minesweeper
// Generates minesweeper boards, a cell containing a mine takes the value -1, while all other cells take the value of the number of mines in their local neighbourhood (i.e. in the eight cells to which they are adjacent.

// A mine count for a given cell is valid if it is between 0 and 8 inclusive.
.decl is_valid(x:number)
is_valid(0).
is_valid(x+1) :- is_valid(x), x < 8.

// The cells of the game board, taking the value -1 if the backing grid contains a value for the cell at the specified row and column, of the number of surrounding mines otherwise.
.decl cell(row:number, column:number, variable:number)
.output cell()
cell(i, j, -1) :- x != 0, element(i, j, x).
cell(i, j, x) :-
  x = tl + tc + tr + ml + mr + bl + bc + br,
  is_valid(x),
  neighbourhood(i, j, tl, tc, tr, ml, 0, mr, bl, bc, br).


