Advertisement
Guest User

Lewa roll attacker win proability

a guest
Jul 15th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.20 KB | None | 0 0
  1. clear all
  2. clc
  3.  
  4. % set the number of dice
  5. minDice = 1;
  6. maxDice = 9;
  7.  
  8. % for each possible number of dice we will generate all permutations of
  9. % outcomes and impose some order
  10. possibleRolls = {};
  11. for N = minDice:maxDice;
  12.  
  13.     % generate all permutations of those dice rolls
  14.     d = genalldice(N,6);
  15.  
  16.     % sort each roll into descending dice value order (as we compare best dice
  17.     % first). I.e. as each row corresponds to a possible outcome of the dice,
  18.     % sort each row in descending order.
  19.     d = sort(d,2,'descend');
  20.  
  21.     % there's a non-strict ordering between any two combinations of dice as to
  22.     % which is better. E.g. using 2d6, a rolle of {6,1} is always at least as
  23.     % good as a {2,2}. We can easily impose this order on the set of possible
  24.     % rolls by sorting and rearranging based on the columns in reverse order
  25.     % (i.e. we rearrange the order of the rows so that in the entire matrix the
  26.     % element in the last column is sorted in descending order. Then we repeat
  27.     % for the second to last column, third to last, etc. until the first
  28.     % column).
  29.  
  30.     for i = size(d,2):-1:1
  31.         [~,I] = sort(d(:,i));
  32.         d = d(I,:);
  33.     end
  34.    
  35.     % store the result
  36.     possibleRolls{N} = d;
  37. end
  38.  
  39. % go through each combination of number of dice for the attacker and the
  40. % defender to compute the probabilities.
  41. pWin = nan(maxDice,maxDice);
  42. for attackerDice = minDice:maxDice
  43.     for defenderDice = minDice:maxDice
  44.         % ties goes to the defender
  45.         attackerWinsTies = false;
  46.        
  47.         % unless either player has more dice, then ties goes to that player
  48.         if attackerDice > defenderDice
  49.             attackerWinsTies = true;
  50.         end
  51.        
  52.         % Get the permutations of possible rolls for each player
  53.         attackerRolls = possibleRolls{attackerDice};
  54.         defenderRolls = possibleRolls{defenderDice};
  55.        
  56.         % if either side has more dice than the other, we reduce their roll
  57.         % matrix as we only need to compare dice until either player runs
  58.         % out
  59.         if attackerDice > defenderDice
  60.             attackerRolls = attackerRolls(:,1:defenderDice);
  61.         elseif attackerDice < defenderDice
  62.             defenderRolls = defenderRolls(:,1:attackerDice);
  63.         end
  64.        
  65.         % Since the rolls are ordered in increasing utility (i.e. roll on
  66.         % row N+1 wins over any rolls which the roll on row N wins over) we
  67.         % can go through the lists once each to find the win probability.
  68.         %
  69.         % Start with the first roll for the attacker. Compare to defender
  70.         % rolls until the defender wins. Store the number of wins for that
  71.         % attacker roll, then go to the next attacker roll. Start with a
  72.         % base value of wins equal to the previous roll, and start
  73.         % comparing to the first roll which the previous roll lost against.
  74.         % Continue until all attacker rolls have been taken into account.
  75.         attackerIndex = 1;
  76.         defenderIndex = 1;
  77.         totalWins = 0;
  78.         while attackerIndex <= size(attackerRolls,1)
  79.             % First, if the defender index is out-of-range, then the
  80.             % current and all remaining attacker indices
  81.             if defenderIndex > size(defenderRolls,1)
  82.                 totalWins = totalWins + (size(attackerRolls,1)-attackerIndex+1)*size(defenderRolls,1);
  83.                 break
  84.             end
  85.            
  86.             % Otherwise, compare roll and continue as normal. If the
  87.             % attacker wins the roll, increase the defender index
  88.             if winLewaRoll(attackerRolls(attackerIndex,:),defenderRolls(defenderIndex,:),attackerWinsTies);
  89.                 defenderIndex = defenderIndex + 1;
  90.                
  91.             % If the defender wins, add the number of wins for the
  92.             % attacking side and continue to the next attacker roll
  93.             else
  94.                 totalWins = totalWins + defenderIndex-1; % won against all earlier rolls
  95.                 attackerIndex = attackerIndex + 1;
  96.             end
  97.         end
  98.        
  99.         % Convert this to a win chance
  100.         pWin(attackerDice,defenderDice) = totalWins/(size(attackerRolls,1)*size(defenderRolls,1));        
  101.     end
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement