[m-users.] [Beginner] Optimally representing a diamond-shaped grid

Peter Wang novalazy at gmail.com
Sun Oct 16 11:15:38 AEDT 2022


On Sat, 15 Oct 2022 15:30:37 +0530 Razetime <rraghu.11502 at gmail.com> wrote:
> I have a grid like follows:
> 
>     1
>   2 3 4
> 5 6 7 8 9
>   A B C
>     D
> 
> for a pointer that spawns on any position on this grid, the possible
> options are up, down, left and right, which move 1 step in each of
> those directions. If a direction is given and there is no spot in that
> direction, the point should remain in place.

I would represent positions by cartesian coordinates:

    :- type pos
	--->	pos(int, int).	% x, y

Then define a predicate that determines valid positions.
You could enumerate all valid positions (as below),
or define it mathematically:

    :- pred valid_position(pos::in) is semidet.

    valid_position(pos(2, 0)).	% position 1 above
    valid_position(pos(1, 1)).	% position 2 above
    % etc.

Then define predicates to move one step in each direction,
or fail if there is no valid step in that direction:

    :- pred down(pos::in, pos::out) is semidet.

    down(Pos0, Pos) :-
	Pos0 = pos(X, Y),
	Pos = pos(X, Y + 1),
	valid_position(Pos).

Peter


More information about the users mailing list