[m-users.] [Beginner] Optimally representing a diamond-shaped grid
Razetime
rraghu.11502 at gmail.com
Sun Oct 16 12:39:02 AEDT 2022
array2d was very helpful, this is what i ended up doing:
:- pred move2(dir::in,{int,int}::in,{int,int}::out) is det.
move2(D,{R,C},E):-
(D=u,{R0,C0}={R-1,C}
;D=d,{R0,C0}={R+1,C}
;D=l,{R0,C0}={R,C-1}
;D=r,{R0,C0}={R,C+1}),
(if in_bounds(gridp2,R0,C0)
then (if lookup(gridp2,R0,C0,'x')
then E={R,C}
else E={R0,C0})
else E={R,C}).
:- func gridp2=array2d(character) is det.
gridp2=from_lists([['x','x','1','x','x'],
['x','2','3','4','x'],
['5','6','7','8','9'],
['x','A','B','C','x'],
['x','x','D','x','x']]).
On 10/16/22, Peter Wang <novalazy at gmail.com> wrote:
> 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