position(1, side1, 0). position(2, side1, 0). position(5, side1, 0). position(10, side1, 0). torch(side1, 0). opposite(side1, side2). opposite(side2, side1). %position(0, W, N) :- torch(W, N). win(N) :- position(1, side2, N), position(2, side2, N), position(5, side2, N), position(10, side2, N). finished() :- win(N). step(W, N) :- torch(W, N), not win(N). chose1(P, N) :- step(W, N), position(P, W, N), P != 0, not otherchoice1(P, N). otherchoice1(P2, N) :- chose1(P1, N), position(P2, W, N), P1 != P2. chose2(P2, N) :- chose1(P1, N), step(W, N), position(P2, W, N), P1 >= P2, not otherchoice2(P2, N). otherchoice2(P2, N) :- chose2(P1, N), position(P2, W, N), P1 != P2. transition(N, N+P) :- chose1(P, N), N+P <= 17. torch(W2, D) :- transition(N, D), torch(W1, N), opposite(W1, W2). position(P, W2, D) :- transition(N, D), chose1(P, N), position(P, W1, N), opposite(W1, W2). position(P, W2, D) :- transition(N, D), chose2(P, N), position(P, W1, N), opposite(W1, W2). position(P, W, D) :- transition(N, D), position(P, W, N), not chose1(P, N), not chose2(P, N). :- not finished().