2015-05-21 4 views
0

Я работаю на игре Dots-и-коробки и код у меня до сих пор является:Можно ли сравнить существующие значения фактов с случайными числами на RHS дефлектора?

(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 после того, как я это сделаю (bind? Move (random 1 24))?

ответ

1

Вы можете использовать функции факт запроса на РИТ, чтобы определить, является ли тот факт, существует для конкретного шага:

(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))) 
+0

Это прекрасно работает, спасибо! – kyle