Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear all
- clc
- % set the number of dice
- minDice = 1;
- maxDice = 9;
- % for each possible number of dice we will generate all permutations of
- % outcomes and impose some order
- possibleRolls = {};
- for N = minDice:maxDice;
- % generate all permutations of those dice rolls
- d = genalldice(N,6);
- % sort each roll into descending dice value order (as we compare best dice
- % first). I.e. as each row corresponds to a possible outcome of the dice,
- % sort each row in descending order.
- d = sort(d,2,'descend');
- % there's a non-strict ordering between any two combinations of dice as to
- % which is better. E.g. using 2d6, a rolle of {6,1} is always at least as
- % good as a {2,2}. We can easily impose this order on the set of possible
- % rolls by sorting and rearranging based on the columns in reverse order
- % (i.e. we rearrange the order of the rows so that in the entire matrix the
- % element in the last column is sorted in descending order. Then we repeat
- % for the second to last column, third to last, etc. until the first
- % column).
- for i = size(d,2):-1:1
- [~,I] = sort(d(:,i));
- d = d(I,:);
- end
- % store the result
- possibleRolls{N} = d;
- end
- % go through each combination of number of dice for the attacker and the
- % defender to compute the probabilities.
- pWin = nan(maxDice,maxDice);
- for attackerDice = minDice:maxDice
- for defenderDice = minDice:maxDice
- % ties goes to the defender
- attackerWinsTies = false;
- % unless either player has more dice, then ties goes to that player
- if attackerDice > defenderDice
- attackerWinsTies = true;
- end
- % Get the permutations of possible rolls for each player
- attackerRolls = possibleRolls{attackerDice};
- defenderRolls = possibleRolls{defenderDice};
- % if either side has more dice than the other, we reduce their roll
- % matrix as we only need to compare dice until either player runs
- % out
- if attackerDice > defenderDice
- attackerRolls = attackerRolls(:,1:defenderDice);
- elseif attackerDice < defenderDice
- defenderRolls = defenderRolls(:,1:attackerDice);
- end
- % Since the rolls are ordered in increasing utility (i.e. roll on
- % row N+1 wins over any rolls which the roll on row N wins over) we
- % can go through the lists once each to find the win probability.
- %
- % Start with the first roll for the attacker. Compare to defender
- % rolls until the defender wins. Store the number of wins for that
- % attacker roll, then go to the next attacker roll. Start with a
- % base value of wins equal to the previous roll, and start
- % comparing to the first roll which the previous roll lost against.
- % Continue until all attacker rolls have been taken into account.
- attackerIndex = 1;
- defenderIndex = 1;
- totalWins = 0;
- while attackerIndex <= size(attackerRolls,1)
- % First, if the defender index is out-of-range, then the
- % current and all remaining attacker indices
- if defenderIndex > size(defenderRolls,1)
- totalWins = totalWins + (size(attackerRolls,1)-attackerIndex+1)*size(defenderRolls,1);
- break
- end
- % Otherwise, compare roll and continue as normal. If the
- % attacker wins the roll, increase the defender index
- if winLewaRoll(attackerRolls(attackerIndex,:),defenderRolls(defenderIndex,:),attackerWinsTies);
- defenderIndex = defenderIndex + 1;
- % If the defender wins, add the number of wins for the
- % attacking side and continue to the next attacker roll
- else
- totalWins = totalWins + defenderIndex-1; % won against all earlier rolls
- attackerIndex = attackerIndex + 1;
- end
- end
- % Convert this to a win chance
- pWin(attackerDice,defenderDice) = totalWins/(size(attackerRolls,1)*size(defenderRolls,1));
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement