Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.util.Random;
- import java.util.Scanner;
- /**
- *
- * @author Dimitri
- */
- public class Calculator {
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Attacker troops: ");
- int a = sc.nextInt();
- System.out.println("Defender troops: ");
- int b = sc.nextInt();
- System.out.println("");
- System.out.println("Number of simulations: ");
- int c = sc.nextInt();
- System.out.println("");
- System.out.println("");
- System.out.println("Percent chance of victory: " + ((float)Simulate(a, b, c)/c));
- }
- public static long Simulate(int troopsA, int troopsB, long sims){
- int oTroopsA = troopsA;
- int oTroopsB = troopsB;
- int winCount = 0;
- long oSims = sims;
- while (sims > 0){
- //System.out.println(sims);
- while (troopsA != 0 && troopsB != 0){
- int diceA1 = 0;
- int diceA2 = 0;
- int diceA3 = 0;
- int diceB1 = 0;
- int diceB2 = 0;
- switch (troopsA){
- default:
- case 3:
- diceA3 = 6;
- case 2:
- diceA2 = 6;
- case 1:
- diceA1 = 6;
- }
- switch (troopsA){
- default:
- case 2:
- diceA3 = 6;
- case 1:
- diceA2 = 6;
- }
- int rollA1 = (int) (Math.random()*diceA1);
- int rollA2 = (int) (Math.random()*diceA2);
- int rollA3 = (int) (Math.random()*diceA3);
- int rollB1 = (int) (Math.random()*diceB1);
- int rollB2 = (int) (Math.random()*diceB2);
- int aMax = 0;
- int aMid = 0;
- int aMin = 0;
- int bMax = 0;
- int bMin = 0;
- if (rollA1 >= rollA2 && rollA1 >= rollA3){
- aMax = rollA1;
- if (rollA2 >= rollA3){
- aMid = rollA2;
- aMin = rollA3;
- } else{
- aMid = rollA3;
- aMin = rollA2;
- }
- } else if (rollA2 >= rollA3){
- aMax = rollA1;
- if (rollA1 >= rollA3){
- aMid = rollA1;
- aMin = rollA3;
- } else{
- aMid = rollA3;
- aMin = rollA1;
- }
- } else {
- aMax = rollA1;
- if (rollA2 >= rollA1){
- aMid = rollA2;
- aMin = rollA1;
- } else{
- aMid = rollA1;
- aMin = rollA2;
- }
- }
- if (rollA1 >= rollA2){
- bMax = rollB1;
- bMin = rollB2;
- }else{
- bMax = rollB2;
- bMin = rollB1;
- }
- if (aMax > bMax){
- troopsB --;
- }else{
- troopsA --;
- }
- if (aMid != 0 && bMin != 0){
- if (aMid > bMin){
- troopsB --;
- }else{
- troopsA --;
- }
- }
- }
- if (troopsB == 0){
- winCount ++;
- }
- sims --;
- troopsA = oTroopsA;
- troopsB = oTroopsB;
- }
- //System.out.println(winCount);
- double winPerc = (winCount / oSims);
- return winCount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement