Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | None | 0 0
  1. package dnd5e;
  2.  
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Random;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.IntStream;
  10.  
  11. import lombok.Value;
  12.  
  13. public final class CreateDist {
  14.     private static void addToPool(Map<StatPool, Integer> pool, StatPool newVal) {
  15.         if(pool.containsKey(newVal)) {
  16.             Integer val = pool.get(newVal);
  17.             pool.put(newVal, val + 1);
  18.         } else {
  19.             pool.put(newVal, 1);
  20.         }
  21.     }
  22.    
  23.     private static boolean poolFilled(Map<StatPool, Integer> pool) {
  24.         Integer checkVal =  pool.values().stream().sorted((a, b) -> Integer.compare(b, a)).skip(39)
  25.                 .findFirst().orElse(-1);
  26.         return checkVal >= 100;
  27.     }
  28.    
  29.  
  30.     private static void printTable(Map<StatPool, Integer> pool) {
  31.         List<StatPool> sort = pool.entrySet().stream().sorted((a, b) -> Integer.compare(b.getValue(), a.getValue()))
  32.             .limit(39).map(e -> e.getKey()).collect(Collectors.toList());
  33.         if(sort.size() < 39) {
  34.             throw new IllegalArgumentException("Size of the pool is < 39");
  35.         }
  36.         System.out.println("Roll;Distribution;BuyValue;Dist");
  37.         for(int i = 2; i <= 40; ++ i) {
  38.             int idx = 0;
  39.             if(i <= 21) {
  40.                 idx = 39 + 3 - 2 * i;
  41.             } else {
  42.                 idx = 2 * i - 43;
  43.             }
  44.             StatPool stat = sort.get(idx);
  45.             int[] arr = stat.stats;
  46.             System.out.println(String.format("%d;%d, %d, %d, %d, %d, %d;%d;%d",
  47.                 i, arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], stat.getValue(), pool.get(stat)));
  48.         }
  49.     }
  50.     public static void main(String[] args) {
  51.         Random rnd = new Random();
  52.         Map<StatPool, Integer> randomPeasant = new HashMap<>();
  53.         Map<StatPool, Integer> randomCompetent = new HashMap<>();
  54.         Map<StatPool, Integer> randomHeroic = new HashMap<>();
  55.         Map<StatPool, Integer> randomEpic = new HashMap<>();
  56.         Map<StatPool, Integer> randomDivine = new HashMap<>();
  57.        
  58.         // Pre-fill the random distributions with a million rolled ability sets
  59.         for(int i = 0; i < 1000000; ++ i) {
  60.             StatPool sp = StatPool.createRandom(rnd);
  61.             if(sp.getValue() >= 10 && sp.getValue() <= 59) {
  62.                 if(sp.getValue() <= 19) {
  63.                     addToPool(randomPeasant, sp);
  64.                 } else if(sp.getValue() <= 29) {
  65.                     addToPool(randomCompetent, sp);
  66.                 } else if(sp.getValue() <= 39) {
  67.                     addToPool(randomHeroic, sp);
  68.                 } else if(sp.getValue() <= 49) {
  69.                     addToPool(randomEpic, sp);
  70.                 } else {
  71.                     addToPool(randomDivine, sp);
  72.                 }
  73.             }
  74.         }
  75.        
  76.         // Make sure we have enough data now (the checks are damn slow so this should never run)
  77.         while(!poolFilled(randomPeasant) || !poolFilled(randomCompetent) || !poolFilled(randomHeroic)
  78.                 || !poolFilled(randomEpic) || !poolFilled(randomDivine)) {
  79.             StatPool sp = StatPool.createRandom(rnd);
  80.             if(sp.getValue() >= 10 && sp.getValue() <= 59) {
  81.                 if(sp.getValue() <= 19) {
  82.                     addToPool(randomPeasant, sp);
  83.                 } else if(sp.getValue() <= 29) {
  84.                     addToPool(randomCompetent, sp);
  85.                 } else if(sp.getValue() <= 39) {
  86.                     addToPool(randomHeroic, sp);
  87.                 } else if(sp.getValue() <= 49) {
  88.                     addToPool(randomEpic, sp);
  89.                 } else {
  90.                     addToPool(randomDivine, sp);
  91.                 }
  92.             }
  93.         }
  94.        
  95.         printTable(randomPeasant);
  96.         printTable(randomCompetent);
  97.         printTable(randomHeroic);
  98.         printTable(randomEpic);
  99.         printTable(randomDivine);
  100.     }
  101.  
  102.     @Value public static class StatPool {
  103.         private static final int[] INT_VALUES =
  104.             {-1000, -1000, -1000,
  105.                 -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 7, 9, 11, 14, 18, 1000};
  106.         public static StatPool createRandom() {
  107.             return createRandom(new Random());
  108.         }
  109.  
  110.         public static StatPool createRandom(Random rnd) {
  111.             StatPool res = new StatPool();
  112.             for(int i = 0; i < 6; ++ i) {
  113.                 res.stats[i] = IntStream.range(0, 4)
  114.                     .map(v -> rnd.nextInt(6) + 1).sorted().skip(1).sum();
  115.             }
  116.             IntStream.range(0, 6).forEach(i -> res.stats[i] *= -1);
  117.             Arrays.sort(res.stats);
  118.             IntStream.range(0, 6).forEach(i -> res.stats[i] *= -1);
  119.             return res;
  120.         }
  121.  
  122.         public int[] stats;
  123.        
  124.         private StatPool() {
  125.             this.stats = new int[6];
  126.         }
  127.        
  128.         public int getValue() {
  129.             return IntStream.of(stats).map(s -> INT_VALUES[s]).sum();
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement