Advertisement
Guest User

Avenging Wrath

a guest
Feb 20th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. package com.comphenix.test;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Application {
  6.     public static void main(String[] args) {
  7.         final Random rnd = new Random();
  8.         final long total = 10000000;
  9.         long victory = 0;
  10.  
  11.         for (int i = 0; i < total; i++) {
  12.             int hpCharacters[] = new int[]{6, 5, 8, 1};
  13.             victory += simulateAvengingWrath(hpCharacters, 3, rnd) ? 1 : 0;
  14.         }
  15.         System.out.printf("Probability of victory: %s%n", (victory / (double) total) * 100);
  16.     }
  17.  
  18.     private static boolean simulateAvengingWrath(int[] hpCharacters, int enemyHeroIndex, Random rnd) {
  19.         for (int i = 0; i < 8; i++) {
  20.             int chosen = rnd.nextInt(hpCharacters.length);
  21.  
  22.             if (hpCharacters[chosen] > 0) {
  23.                 hpCharacters[chosen]--;
  24.             } else {
  25.                 // Repeat and try another character
  26.                 i--;
  27.             }
  28.  
  29.             // Did we just die? This also ensures we eventually exit this loop ...
  30.             if (hpCharacters[enemyHeroIndex] <= 0) {
  31.                 return false;
  32.             }
  33.         }
  34.         // Victory
  35.         return true;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement