(* This function generates a table of all possible dice throws (not ordered yet, it could be optimized so ordering occurs here, and not in the Testa function). *)

Dice[At_, De_] := 
        Flatten[Table[{Reverse[
                Join[k1 = Mod[i - 1, 6] + 1; {k1}, 
                  k2 = Mod[(((i - k1))/6), 6] + 1; If[At > 1, {k2}, {}], 
                  If[At == 3, {Quotient[i - 1, 36] + 1}, {}]]], 
              Reverse[Join[k3 = Mod[j - 1, 6] + 1; {k3}, 
                  k4 = Mod[(((j - k3))/6), 6] + 1; If[De > 1, {k4}, {}], 
                  If[De == 3, {Quotient[j - 1, 36] + 1}, {}]]]}, {i, 1, 
              (6^At)}, {j, 1, (6^De)}], 1];

(* This function takes the list of all possible dice throws and shows the percentage of each outcome (first element corresponds to the attacker winning all armies in play, further elements show progressively worse results for attacker).
Lista 3 has the count of all throws with the same outcome, Lista4 has the relative frequency. *)

\(Testa[At_, De_] := \((Lista1 = Dice[At, De]; 

        Lista2 = 
          Table[l1 = Take[Sort[Lista1[\([j, 1]\)], Greater], Min[At, De]]; 
            l2 = Take[Sort[Lista1[\([j, 2]\)], Greater], Min[At, De]]; 
            Apply[Plus, 
              Table[If[l1[\([i]\)] > l2[\([i]\)], 1, 0], {i, 1, 
                  Min[At, De]}]], {j, 1, 
              6\^\(At + De\)}]; 
Lista3 = 
          Table[Count[Lista2, Min[At, De] + 1 - k], {k, 1, 
              Min[At, De] + 1}]; 
Lista4 = 
          Map[#1\/6\^\(At + De\) &, Lista3\ ]; \ N[Lista4])\)\)

(* Tabela1 has the probabilities of all possible outcomes of all combinations of attack and defense.
        The rows follow (At x De):
    1x1,2x1,3x1,1x2,2x2,3x2,1x3,2x3,3x3
      The columns correspond to the losses (At x De; 0x1 means At loses no army, De loses 1 army):
    0x1,1x0 for rows 1,2,3,4,7; 0x2,1x1,2x0 for rows 5,6,8; 0x3,1x2,2x1,3x0 for row 9 *)

Tabela1 = 
        Flatten[Table[Testa[j, i], {i, 1, 3}, {j, 1, 3}], 
          1];
TableForm[Tabela1]

(* Tabela2 generates the probabilities for conquering a territory (i.e. all De armies are lost) when At and De are less than or equal to 3; rows for At, columns for De*)

Tabela2 = {
{Tabela1[[1, 1]], 
            Tabela1[[4, 1]] Tabela1[[1, 1]], 
            Tabela1[[7, 1]] Tabela1[[4, 1]] 
              Tabela1[[1, 1]]}, 
{Tabela1[[2, 1]] + 
              Tabela1[[2, 2]] Tabela1[[1, 1]], 
            Tabela1[[5, 1]] + Tabela1[[5, 2]] Tabela1[[1, 1]], 
            Tabela1[[8, 1]] Tabela1[[2, 1]] + 
              Tabela1[[8, 2]] 
                Tabela1[[1, 2]]}, 
{Tabela1[[3, 
                  1]] + Tabela1[[3, 2]] Tabela1[[2, 1]], 
            Tabela1[[6, 1]] + Tabela1[[6, 2]] Tabela1[[2, 1]] + 
              Tabela1[[6, 3]] Tabela1[[1, 2]], 
            Tabela1[[9, 1]] + Tabela1[[9, 2]] Tabela1[[2, 1]] + 
              Tabela1[[9, 3]] 
                Tabela1[[1, 
                    2]]}
};
TableForm[Tabela2]

(* Recursion function, looks into Tabela2 and Tabela1 *)

Probab[At_, 
          De_] :=  Probab[At, De] =  
If[
            And[At ≤ 3, De ≤ 3], 
If[
              Or[At == 0, De == 0], 1, 
              Tabela2[[At, De]]], 
If[At == 1, 
              Tabela1[[7, 1]] Probab[At, De - 1], 
If[
                At == 2, 
                Tabela1[[8, 1]] Probab[At, De - 2] + 
                  Tabela1[[8, 2]] 
                    Probab[At - 1, De - 1], 
If[
                  At == 3, 
                  Tabela1[[9, 1]] Probab[At, De - 3] + 
                    Tabela1[[9, 2]] Probab[At - 1, De - 2] + 
                    Tabela1[[9, 3]] 
                      Probab[At - 2, De - 1], 
If[De == 1, 
                    Tabela1[[3, 1]] + 
                      Tabela1[[3, 2]] 
                        Probab[At - 1, De], 
If[De == 2, 
                      Tabela1[[6, 1]] + 
                        Tabela1[[6, 2]] Probab[At - 1, De - 1] + 
                        Tabela1[[6, 3]] 
                          Probab[At - 2, De], 
If[
                        De == 3, 
                        Tabela1[[9, 1]] + 
                          Tabela1[[9, 2]] Probab[At - 1, De - 2] + 
                          Tabela1[[9, 3]] Probab[At - 2, De - 1] + 
                          Tabela1[[9, 4]] 
                            Probab[At - 3, De], 
Apply[
                          Plus, Table[
                            Tabela1[[9, i]] 
                              Probab[At + 1 - i, De + i - 4], {i, 1, 
                              4}] ]
]]]]]]];

(* Final probabilities of conquering territory, rows for At, columns for De; 4 armies should guarantee a conquest against 1 defender *)

TableForm[
      Table[If[i == 1, j - 1, 
          If[j == 1, i - 1, 100  Probab[i - 1, j - 1] ] ], {i, 1, 31}, {j, 
          1, 11}]]

(* \(One\ could\ look\ at\ how\ many\ attackers\ one\ needs\ to\ get\ chances\ better\ than\ 90  %\), and the data:(({{1, 3}, {2, 6}, {3, 10}, {4, 13}, {5, 16}})) would give us a rule of thumb of At = 3 De  +  1 *)