Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. /*
  5.  * To change this license header, choose License Headers in Project Properties.
  6.  * To change this template file, choose Tools | Templates
  7.  * and open the template in the editor.
  8.  */
  9. import java.util.Random;
  10. import java.util.Scanner;
  11. /**
  12.  *
  13.  * @author Dimitri
  14.  */
  15. public class Calculator {
  16.     /*
  17.  * To change this license header, choose License Headers in Project Properties.
  18.  * To change this template file, choose Tools | Templates
  19.  * and open the template in the editor.
  20.  */
  21.     public static void main(String[] args) {
  22.          Scanner sc = new Scanner(System.in);
  23.          System.out.println("Attacker troops: ");
  24.          int a = sc.nextInt();
  25.          System.out.println("Defender troops: ");
  26.          int b = sc.nextInt();
  27.          System.out.println("");
  28.          System.out.println("Number of simulations: ");
  29.          int c = sc.nextInt();
  30.          System.out.println("");
  31.          System.out.println("");
  32.          
  33.          System.out.println("Percent chance of victory: " + ((float)Simulate(a, b, c)/c));
  34.     }
  35.    
  36.     public static long Simulate(int troopsA, int troopsB, long sims){
  37.         int oTroopsA = troopsA;
  38.         int oTroopsB = troopsB;
  39.         int winCount = 0;
  40.         long oSims = sims;
  41.         while (sims > 0){
  42.             //System.out.println(sims);
  43.         while (troopsA != 0 && troopsB != 0){
  44.            
  45.         int diceA1 = 0;
  46.         int diceA2 = 0;
  47.         int diceA3 = 0;
  48.        
  49.         int diceB1 = 0;
  50.         int diceB2 = 0;
  51.        
  52.         switch (troopsA){
  53.             default:
  54.             case 3:
  55.                 diceA3 = 6;
  56.             case 2:
  57.                 diceA2 = 6;
  58.             case 1:
  59.                 diceA1 = 6;
  60.         }
  61.        
  62.         switch (troopsA){
  63.             default:
  64.             case 2:
  65.                 diceA3 = 6;
  66.             case 1:
  67.                 diceA2 = 6;
  68.         }
  69.        
  70.         int rollA1 = (int) (Math.random()*diceA1);
  71.         int rollA2 = (int) (Math.random()*diceA2);
  72.         int rollA3 = (int) (Math.random()*diceA3);
  73.        
  74.         int rollB1 = (int) (Math.random()*diceB1);
  75.         int rollB2 = (int) (Math.random()*diceB2);
  76.        
  77.         int aMax = 0;
  78.         int aMid = 0;
  79.         int aMin = 0;
  80.        
  81.         int bMax = 0;
  82.         int bMin = 0;
  83.        
  84.         if (rollA1 >= rollA2 && rollA1 >= rollA3){
  85.             aMax = rollA1;
  86.             if (rollA2 >= rollA3){
  87.                 aMid = rollA2;
  88.                 aMin = rollA3;
  89.             } else{
  90.                 aMid = rollA3;
  91.                 aMin = rollA2;
  92.             }
  93.         } else if (rollA2 >= rollA3){
  94.             aMax = rollA1;
  95.             if (rollA1 >= rollA3){
  96.                 aMid = rollA1;
  97.                 aMin = rollA3;
  98.             } else{
  99.                 aMid = rollA3;
  100.                 aMin = rollA1;
  101.             }
  102.         } else {
  103.             aMax = rollA1;
  104.             if (rollA2 >= rollA1){
  105.                 aMid = rollA2;
  106.                 aMin = rollA1;
  107.             } else{
  108.                 aMid = rollA1;
  109.                 aMin = rollA2;
  110.             }
  111.         }
  112.        
  113.         if (rollA1 >= rollA2){
  114.             bMax = rollB1;
  115.             bMin = rollB2;
  116.         }else{
  117.             bMax = rollB2;
  118.             bMin = rollB1;
  119.         }
  120.        
  121.             if (aMax > bMax){
  122.             troopsB --;
  123.             }else{
  124.             troopsA --;
  125.             }
  126.            
  127.             if (aMid != 0 && bMin != 0){
  128.             if (aMid > bMin){
  129.             troopsB --;
  130.                
  131.             }else{
  132.             troopsA --;
  133.                
  134.             }
  135.             }
  136.         }
  137.         if (troopsB == 0){
  138.             winCount ++;
  139.         }
  140.         sims --;
  141.        
  142.         troopsA = oTroopsA;
  143.         troopsB = oTroopsB;
  144.         }
  145.        
  146.         //System.out.println(winCount);
  147.         double winPerc = (winCount / oSims);
  148.        
  149.         return winCount;
  150.     }
  151.    
  152.  
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement