我正在制作一个Dots-and-Boxes游戏,到目前为止我的代码是:
(deffacts Game_Start "Sets turn number to 1 and next turn to player"
(turn_num 1)
(next_turn p)
(Line_Taken 0))
(defrule First_Three_Moves "Used for the first 3 moves of the game"
?f <-(turn_num ?t_num)
(next_turn p)
(or(turn_num 1)
(turn_num 2)
(turn_num 3))
=>
(bind ?move (random 1 24))
(bind ?tn (+ ?t_num 1))
(assert (turn_num ?tn))
(retract ?f)
(assert(Line_Taken ?move))
(printout t "Take line #" ?move crlf)
(assert (next_turn c))))
(defrule Computer_Move
?turn <- (next_turn c)
=>
(printout t "Enter computer move: ")
(bind ?c_move (read))
(assert(Line_Taken ?c_move))
(retract ?turn)
(assert (next_turn p)))
我的前三个动作在1到24之间得到一个随机数。有没有办法确保我在RHS之后没有选择移动号码(绑定?移动(随机1 24))?
答案 0 :(得分:1)
您可以使用RHS上的事实查询功能来确定特定移动是否存在事实:
(defrule First_Three_Moves "Used for the first 3 moves of the game"
?f <-(turn_num ?t_num)
(next_turn p)
(or(turn_num 1)
(turn_num 2)
(turn_num 3))
=>
(bind ?move (random 1 24))
(while (any-factp ((?lt Line_Taken)) (member$ ?move ?lt:implied))
(bind ?move (random 1 24)))
(bind ?tn (+ ?t_num 1))
(assert (turn_num ?tn))
(retract ?f)
(assert(Line_Taken ?move))
(printout t "Take line #" ?move crlf)
(assert (next_turn c)))