Bidderlyn

aoe2 role handout distribution program

Jan 14th, 2021 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. package main;
  2. import javax.swing.*;
  3.  
  4. /**
  5.  * This script reproduces the method aoe2 uses to handle roles with "chance" triggers for players
  6.  * We divide an amount of roles that equal to the amount of players and choose the chance for each player.
  7.  * Each player can only get 1 role in every scenario.
  8.  * The program shall then tell us how much times each player got a certain role, after iterating scenarios X times.
  9.  * We can then keep tweaking the chance numbers until we get the deviation we require, using The law of large numbers.
  10.  *
  11.  * The program will show you exactly what will be the distribution of roles depending on the chances you give to each trigger
  12.  * @author Bidderlyn
  13.  */
  14. public class Main {
  15.  
  16.      
  17.     public static void main(String[] args) {
  18.         int amountOfPlayers; // the total amount of players. Input by user
  19.         String playersInput = JOptionPane.showInputDialog("Enter the amount of players");
  20.          amountOfPlayers = Integer.valueOf(playersInput);
  21.        
  22.         Player player[] = new Player[amountOfPlayers];
  23.         int roles = player.length;
  24.         int result[][] = new int[player.length][roles]; // first array represents the player N, 2nd array is the role. Index value is the amount of times player got the role.
  25.        
  26.         for(int j=0; j<player.length; j++)
  27.         {
  28.             player[j] = new Player(roles);
  29.         }
  30.        
  31.         for(int i=0; i<roles; i++)
  32.         {
  33.             // i = role iteration
  34.             // j = player iteration
  35.  
  36.             for(int j=0; j<player.length; j++)
  37.             {
  38.                 // Setting the "chance" for each player in each role
  39.                 String chanceInput = JOptionPane.showInputDialog("Set the 'chance' condition for player"+(j+1)+" to get role number "+(i+1));
  40.                 player[j].chance[i] = Integer.valueOf(chanceInput);
  41.             }
  42.         }
  43.        
  44.         // SETUP INPUTS DONE
  45.         // testing the scenario X times and posting the distribution. X determined by user.
  46.         String testInput = JOptionPane.showInputDialog("Enter the amount of scenario simulations youd like to run");
  47.         Integer scenarios = Integer.valueOf(testInput);
  48.         for(int z=0; z<scenarios; z++)
  49.         {
  50.            
  51.             // z = scenario iteration
  52.             // i = role iteration
  53.             // j = player iteration
  54.            
  55.             for(int i=0; i<player.length; i++)
  56.             {
  57.                 // resetting all players data from previous scenario
  58.                 player[i].hasRole = false;
  59.                 player[i].role = 0;
  60.             }
  61.            
  62.             for(int i=0; i<roles; i++ )
  63.             {
  64.                 // handing out role <i>
  65.                 boolean roleHanded = false;
  66.                 while(!roleHanded)
  67.                 {
  68.                     for(int j=0; j<player.length; j++ )
  69.                     {
  70.                         // Attempting to give the role to each player by order, loops back to 1st player if no one got the role.
  71.                         if(!roleHanded && !player[j].hasRole && player[j].chance[i] > (Math.random() * 100) ) // player's chance against a random number between 1-100
  72.                         {
  73.                             //player[j] got the role
  74.                             roleHanded = true;
  75.                             player[j].hasRole = true;
  76.                             player[j].role = i;
  77.                             result[j][i]++; // updating the external results
  78.                         }
  79.                         // iterate to the next player.
  80.                     }
  81.                     // if no one got the role, it loops back to the 1st player and tries to hand it out again
  82.                     // if the role was given, while breaks and role iteration increases to hand out the next role.
  83.                 } // a role was handed 
  84.             } // finished handing all roles
  85.         } // finished iteration scenarios
  86.         for(int i=0; i<roles; i++)
  87.         {
  88.             String output = "";
  89.             for(int j=0; j<player.length; j++)
  90.             {
  91.                 output +=  "player"+(j+1)+" got role "+(i+1)+" "+result[j][i]+" amount of times \n";
  92.             }
  93.            
  94.             //displaying the distribution to the user
  95.             JFrame f=new JFrame();  
  96.             JOptionPane.showMessageDialog(f,output);  
  97.         }
  98.     }
  99. }
  100.  
  101.  
  102.  
Add Comment
Please, Sign In to add comment