algorithm - Prolog:井字游戏

标签 algorithm prolog

我有基本的 Tick-tack-toe 游戏,其中 2 名玩家都在 9 个单元格的网格上移动。问题是,在第一个玩家做出最后的胜利 Action 后,游戏不会停止,玩家 2 仍然可以玩。如果第二个玩家不知何故也做出了获胜的举动,他将成为赢家,尽管玩家 1 实际上首先获胜。如果第二个玩家首先获胜,它不会犯同样的错误。绘图工作正常。这是代码:

:- dynamic o/1.
:- dynamic x/1.

/* the various combinations of a successful horizontal, vertical
or diagonal line */

ordered_line(1,2,3).
ordered_line(4,5,6).
ordered_line(7,8,9).
ordered_line(1,4,7).
ordered_line(2,5,8).
ordered_line(3,6,9).
ordered_line(1,5,9).
ordered_line(3,5,7).

/*line predicate to complete lines

line(A,B,C) :- ordered_line(A,B,C).
line(A,B,C) :- ordered_line(A,C,B).
line(A,B,C) :- ordered_line(B,A,C).
line(A,B,C) :- ordered_line(B,C,A).
line(A,B,C) :- ordered_line(C,A,B).
line(A,B,C) :- ordered_line(C,B,A).


full(A) :- x(A).
full(A) :- o(A).

empty(A) :- not(full(A)).

all_full :- full(1),full(2),full(3),full(4),full(5),
full(6),full(7),full(8),full(9).


done :- ordered_line(A,B,C), x(A), x(B), x(C), write('Player 2 win.'),nl.

done :- ordered_line(A,B,C), o(A), o(B), o(C), write('Player 1 win.'),nl.

done :- all_full, write('Draw.'), nl.

move1 :- write('Player 1 (o) enter a move: '), read(X), between(1,9,X),  
empty(X), assert(o(X)).
move1:-all_full.
move2 :-  write('Player 2 (x) enter a move: '), read(X), between(1,9,X),  
empty(X),assert(x(X)).
move2:- all_full.
printsquare(N) :- o(N), write(' o ').
printsquare(N) :- x(N), write(' x ').
printsquare(N) :- empty(N), write('   ').

printboard :- printsquare(1),printsquare(2),printsquare(3),nl,
          printsquare(4),printsquare(5),printsquare(6),nl,
          printsquare(7),printsquare(8),printsquare(9),nl.

clear :- x(A), retract(x(A)), fail.
clear :- o(A), retract(o(A)), fail.

play :- not(clear), repeat, move1, printboard, move2,printboard, done.

这就是我得到的错误: Game doesnt stop when player 1 wins

希望你能帮助我 :) 在此先感谢。

编辑:“玩家 2 获胜”显示在“完成”谓词中。在成功完成 3 'o' 或 'x' 线后,游戏应该以玩家 1 获胜或玩家 2 获胜而结束。我将包括原始代码,这可能有助于理解我遇到的问题 Original code with comments

最佳答案

问题出在游戏的主谓词上:

play :- not(clear), repeat, move1, printboard, move2,printboard, done.

在玩家的第 1 步之后,您不会检查done

在Prolog的语法中,逻辑“或”写成;。因此,表达 play 谓词的一种方法是:

play :- not(clear), repeat, move1, printboard, (done; move2, printboard, done).

这表示“在移动 1 之后,检查游戏是否完成(即结束);如果没有,则玩家 2 移动,然后重复检查。

关于algorithm - Prolog:井字游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37523290/

相关文章:

列表和子列表的Python算法

java - 将链表分成两半并返回后半部分

检测路径的 Prolog 定义

macos - 如何设置 PrologScript

c# - 将字符串列表随机分组

ruby-on-rails - Rails 按算法排序

c++ - 搜索大素数时,整数常量对于 ‘long’ 类型来说太大

prolog - NU-Prolog 和 Gödel 的逻辑和声音 `if-then-else` 扩展

prolog - PROLOG 中的填字游戏解算器

prolog - 在 CNF 中表达知识库并在 prolog 中表达解析