确定下一个状态谓词序言

发布于 2025-01-21 03:45:44 字数 314 浏览 0 评论 0原文

我有一个包含 5 个元素、2 个黑色元素、2 个白色元素和一个空点(“v”)的列表。 我有一个由 [w, w, v, b, b] 表示的状态,其中有白人玩家和黑人玩家。

每个玩家轮流玩,并且可以向前移动他的一个元素。他无法将其向后移动。

我想写一个谓词来确定玩家 P 玩后所有可能的状态。

例如,我们将有:

next_state([w, w, v, b, b], W, L).
 L = [v, w, w, b, b];
 L = [w, v, w, b, b];
 false.

但我不知道如何进行。 谢谢

I have a list with 5 elements, 2 black elements, 2 white, and an empty spot ("v").
I have a state represented by [w, w, v, b, b] with White player and Black player.

Each player plays turn by turn, and can move one of his element forward. He can't move it backward.

I want to write a predicate that determines all possible states after player P played.

For exemple, we'll have:

next_state([w, w, v, b, b], W, L).
 L = [v, w, w, b, b];
 L = [w, v, w, b, b];
 false.

But I don't know how to procede.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喵星人汪星人 2025-01-28 03:45:44

如果跳过同名的件规则仅限于向前移动2个位置,那么这应该足够:

player(w, forwards).
player(b, backwards).


state_player_next(State, Player, State1) :-
    player(Player, Direction),
    (   Direction == forwards ->
        state_player_next_(State, Player, State1)
    ;   reverse(State, StateReverse),
        state_player_next_(StateReverse, Player, State1Reverse),
        reverse(State1Reverse, State1)
    ).


% Make move in correct direction, leaving behind space (v)
state_player_next_([Player, Next|T], Player, [v, Player|T]) :-
    % Can't take own piece
    dif(Player, Next).

% Jump over the player's 1 other piece
state_player_next_([Player, Player, Next|T], Player, [v, Player, Player|T]) :-
    % Can't take own piece
    dif(Player, Next).

% Piece (or space) stays where it is
state_player_next_([P|T], Player, [P|State1]) :-
    state_player_next_(T, Player, State1).

结果:Swi-Promog:

?- state_player_next([w, w, v, b, b], Player, LstNext).
Player = w,
LstNext = [v,w,w,b,b] ;
Player = w,
LstNext = [w,v,w,b,b] ;
Player = b,
LstNext = [w,w,b,b,v] ;
Player = b,
LstNext = [w,w,b,v,b] ;
false.

If the skipping-over-same-player's-piece rule is limited to moving a maximum of 2 positions forward, then this should suffice:

player(w, forwards).
player(b, backwards).


state_player_next(State, Player, State1) :-
    player(Player, Direction),
    (   Direction == forwards ->
        state_player_next_(State, Player, State1)
    ;   reverse(State, StateReverse),
        state_player_next_(StateReverse, Player, State1Reverse),
        reverse(State1Reverse, State1)
    ).


% Make move in correct direction, leaving behind space (v)
state_player_next_([Player, Next|T], Player, [v, Player|T]) :-
    % Can't take own piece
    dif(Player, Next).

% Jump over the player's 1 other piece
state_player_next_([Player, Player, Next|T], Player, [v, Player, Player|T]) :-
    % Can't take own piece
    dif(Player, Next).

% Piece (or space) stays where it is
state_player_next_([P|T], Player, [P|State1]) :-
    state_player_next_(T, Player, State1).

Result in swi-prolog:

?- state_player_next([w, w, v, b, b], Player, LstNext).
Player = w,
LstNext = [v,w,w,b,b] ;
Player = w,
LstNext = [w,v,w,b,b] ;
Player = b,
LstNext = [w,w,b,b,v] ;
Player = b,
LstNext = [w,w,b,v,b] ;
false.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文