Advertisement
Major_Tuvok

A Vein

Dec 15th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. public class Vein {
  2.     private int sum;
  3.     private List<Vein> subveins;
  4.     private String name;
  5.     private int weight;
  6.    
  7.     public Vein(String name, int weight) {
  8.         this.name = Objects.requireNonNull(name);
  9.         this.weight = Math.max(weight, 1);
  10.         this.sum = 0;
  11.         this.subveins = new ArrayList<>();
  12.     }
  13.    
  14.     public void addSubVein(Vein other) {
  15.         subveins.add(Objects.requireNonNull(other))
  16.         sum+= other.getWeight();
  17.     }
  18.    
  19.     pubic Vein selectVein() {
  20.         if (subveins.isEmpty())
  21.             return this;
  22.         int random = Math.floor(Math.random()*sum);
  23.         int curSum = 0;
  24.         int i = 0;
  25.         for (; i < subveins.size() && curSum < random; ++i) {
  26.             curSum+=subveins.get(i).getWeight();
  27.         }
  28.         return subveins.get(Math.min(i, subveins.size()-1));
  29.     }
  30.    
  31.     pubic int getWeight() {
  32.         return weight;
  33.     }
  34.    
  35.     public String getName() {
  36.         return name;
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement