<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
%
% Risk game win probabilities
%
% mmeri /15.11.2004
%

% You can direct the output to a file using tell('logfile') before
% and told predicates before and after the call to main predicate ,
% main-predicate below.  This exemplifies the value instantiation order of
% variables and generaes the hands in the same order as the 
% C-language version.  So this is a generate and test solution.
% The global variable type of effect is implemented using
% the assert and retract predicates. This might cause some overhead, but 
% provides a meand to avoid constructing lists of answers, which might 
% become a hurdle memore space vice if there were mode hands 
%(consider risk with  larger numbers of die casted).

% Extract from the end of output:
% Attacker wins 2  with probability 0.371656
% Defender wins 2  with probability 0.292567
% Tie (1/1)  with probability 0.335777
% Attacker lose expectation 0.920910

die(X) :- member(X,[1,2,3,4,5,6]).

atac(A) :-
    A = [X,Y,Z],
    die(X),
    die(Y),
    die(Z),
    X &gt;= Y,
    Y &gt;= Z.

defe(A) :-
    A = [X,Y],
    die(X),
    die(Y),
    X &gt;= Y.

roll(A,B) :-
    atac(A),
    defe(B),
    report(A,B).

prob_class([A1,A2,A3],6) :-
    A1 \= A2,
    A2 \= A3,
    A1 \= A3.
prob_class([A1,A2,A3],3) :-
    A1 = A2,
    A2 \= A3.
prob_class([A1,A2,A3],3) :-
    A1 = A3,
    A2 \= A3.
prob_class([A1,A2,A3],3) :-
    A2 = A3,
    A1 \= A3.
prob_class([A1,A2,A3],1) :-
    A2 = A3,
    A1 = A2.
prob_class([D1,D2],2) :-
    D1 \= D2.
prob_class([D1,D2],1) :-
    D1 = D2.

report([A1,A2,A3],[D1,D2]) :-
    A1 &gt; D1,
    A2 &gt; D2,
    write([A1,A2,A3]),tab(1),write([D1,D2]),tab(1),format("Atta 2"),
    !,
    att(N),
    retractall(att(_)),
    prob_class([A1,A2,A3],C1),
    prob_class([D1,D2],C2),
    N1 is N + C1 * C2,
    tab(2),format("~d ~d \n",[C1,C2]),
    assert(att(N1)).

report([A1,A2,A3],[D1,D2]) :-
    A1 =&lt; D1,
    A2 &gt; D2,
    write([A1,A2,A3]),tab(1),write([D1,D2]),tab(1),format("Tie a "),
    !,
    tie(N),
    retractall(tie(_)),
    prob_class([A1,A2,A3],C1),
    prob_class([D1,D2],C2),
    N1 is N + C1 * C2,
    tab(2),format("~d ~d \n",[C1,C2]),
    assert(tie(N1)).

report([A1,A2,A3],[D1,D2]) :-
    A1 &gt; D1,
    A2 =&lt; D2,
    write([A1,A2,A3]),tab(1),write([D1,D2]),tab(1),format("Tie b "),
    !,
    tie(N),
    retractall(tie(_)),
    prob_class([A1,A2,A3],C1),
    prob_class([D1,D2],C2),
    N1 is N + C1 * C2,
    tab(2),format("~d ~d \n",[C1,C2]),
    assert(tie(N1)).

report([A1,A2,A3],[D1,D2]) :-
    A1 =&lt; D1,
    A2 =&lt; D2,
    write([A1,A2,A3]),tab(1),write([D1,D2]),tab(1),format("Defe 2"),
    !,
    def(N),
    retractall(def(_)),
    prob_class([A1,A2,A3],C1),
    prob_class([D1,D2],C2),
    N1 is N + C1 * C2,
    tab(2),format("~d ~d \n",[C1,C2]),
    assert(def(N1)).

report(_,_) :-
    err(N),
    retractall(err(_)),
    N1 is N + 1,
    assert(err(N1)),
    format("virhe\n").
        
go :-    
    roll(A,B),
    last(A,B).

last([7,7,7],[7,7]).

show :-
    att(A2),    
    tie(T1),
    def(D2),
    N is A2 + T1 + D2,
    A is A2 / N,
    T is T1 / N,
    D is D2 / N,
    Exp is (2*D2 + 1*T1) / N,
    format('Attacker wins 2  with probability ~f \n', A),
    format('Defender wins 2  with probability ~f \n', D),
    format('Tie (1/1)  with probability ~f \n', T),
    format('Attacker lose expectation ~f \n', Exp).

clear :-
    retractall(att(_)),
    retractall(tie(_)),
    retractall(def(_)),
    retractall(err(_)).

init :-
    assert(att(0)),
    assert(tie(0)),
    assert(def(0)),
    assert(err(0)).

main :-
    clear,
    init,
    go.

main :-
    show.
    
</pre></body></html>