我可以将现有的事实值与 defrule 的 RHS 上的随机数进行比较吗?
Can I compare existing fact values to random numbers on the RHS of a defrule?
我正在开发一个 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 之间的随机数。有没有办法确保我在执行 (bind ?move (random 1 24)) 之后还没有在 RHS 上选择移动编号?
您可以使用 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)))
我正在开发一个 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 之间的随机数。有没有办法确保我在执行 (bind ?move (random 1 24)) 之后还没有在 RHS 上选择移动编号?
您可以使用 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)))