Advertisement
Guest User

Pokemon Showdown data cruncher

a guest
Jan 4th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.13 KB | None | 0 0
  1. package squashed.crunchit;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.PrintStream;
  6. import java.util.Arrays;
  7. import java.util.Collections;
  8. import java.util.HashMap;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.Scanner;
  13.  
  14. public class Crunchit {
  15.  
  16.     public static void main(String[] args) throws IOException {
  17.         //You need to have this file.
  18.         Scanner scanner = new Scanner(new File("december_ou.txt"));
  19.         PrintStream out = System.out;
  20.        
  21.         List<Pokemon> allPokemon = new LinkedList<Pokemon>();
  22.         Map<String, Pokemon> pokemonByName = new HashMap<String, Pokemon>();
  23.         while(scanner.hasNextLine()) {
  24.             Pokemon pok = new Pokemon(scanner);
  25.             allPokemon.add(pok);
  26.             pokemonByName.put(pok.name, pok);
  27.         }
  28.        
  29.         for(Pokemon pok : allPokemon) {
  30.             pok.weighCounters(pokemonByName);
  31.         }
  32.        
  33.         //Change this to use different pokemon.
  34.         //The size of the list doesn't matter.
  35.         //Spell them the way they're spelled in the report,
  36.         //it doesn't know that "Rotom-W" means "Rotom-Wash"
  37.         List<Pokemon> team = Arrays.asList(
  38.                 pokemonByName.get("Breloom"),
  39.                 pokemonByName.get("Dragonite"),
  40.                 pokemonByName.get("Talonflame"),
  41.                 pokemonByName.get("Espeon"),
  42.                 pokemonByName.get("Excadrill")
  43.             );
  44.        
  45.         //Crunch for counters
  46.         Map<String, PokemonRef> teamCounters = new HashMap<String, PokemonRef>();
  47.         for(Pokemon pok : team) {
  48.             for(PokemonRef counter : pok.weighedCounters) {
  49.                 PokemonRef oldCount = teamCounters.get(counter.name);
  50.                 double c = oldCount == null ? 0 : oldCount.weight;
  51.                 c += counter.weight;
  52.                
  53.                 teamCounters.put(counter.name, new PokemonRef(counter.name, c));
  54.             }
  55.         }
  56.        
  57.         List<PokemonRef> threatList = new LinkedList<PokemonRef>(teamCounters.values());
  58.        
  59.         //Uncrunch counters for team mates
  60.         List<PokemonRef> uncrunchedThreats = new LinkedList<PokemonRef>();
  61.         for(PokemonRef counter : threatList) {
  62.             Pokemon counterPok = pokemonByName.get(counter.name);
  63.             double uncounter = 0;
  64.             for(PokemonRef cc : counterPok.counters) {
  65.                 if(team.contains(pokemonByName.get(cc.name))) {
  66.                     uncounter += cc.weight;
  67.                 }
  68.             }
  69.            
  70.             double newWeight = counter.weight - uncounter;
  71.             if(newWeight > 0) {
  72.                 uncrunchedThreats.add(new PokemonRef(counter.name, newWeight));
  73.             }
  74.         }
  75.        
  76.         Collections.sort(uncrunchedThreats);
  77.        
  78.         out.println("Threats to your team:");
  79.         for(PokemonRef ref : uncrunchedThreats) {
  80.             out.println(ref.name + "\t" + ref.weight);
  81.         }
  82.        
  83.         //Find recommendations to take out threats
  84.         out.println();
  85.         out.println("Recommendations based on threats:");
  86.  
  87.         Map<String, PokemonRef> counterCounters = new HashMap<String, PokemonRef>();
  88.         for(PokemonRef ref :uncrunchedThreats) {
  89.             Pokemon pok = pokemonByName.get(ref.name);
  90.             for(PokemonRef counter : pok.counters) {
  91.                 PokemonRef oldCount = counterCounters.get(counter.name);
  92.                 double c = oldCount == null ? 0 : oldCount.weight;
  93.                 c += counter.weight * pok.weight;
  94.                
  95.                 counterCounters.put(counter.name, new PokemonRef(counter.name, c));
  96.             }
  97.         }
  98.        
  99.         List<PokemonRef> helperList = new LinkedList<PokemonRef>(counterCounters.values());
  100.         Collections.sort(helperList);
  101.        
  102.         for(PokemonRef ref : helperList) {
  103.             out.println(ref.name + "\t" + ref.weight);
  104.         }
  105.     }
  106.    
  107.     private static class Pokemon {
  108.         private String name;
  109.         private double weight;
  110.         private List<PokemonRef> teammates = new LinkedList<PokemonRef>();
  111.         private List<PokemonRef> counters = new LinkedList<PokemonRef>();
  112.         private List<PokemonRef> weighedCounters = new LinkedList<PokemonRef>();
  113.         //Teammates - "you have these pokemon on your team, maybe you'd like to have this"
  114.         //Weighed Counters - "these are the most likely pokemon to rock your team"
  115.         //Counters - "these pokemon kill whatever we specified n weighed counters"
  116.        
  117.         private Pokemon(Scanner source) {
  118.             source.nextLine(); //Start of name block
  119.             String nameLine = source.nextLine();
  120.             name = nameLine.substring(" | ".length(), nameLine.length() - 3).trim();
  121.             source.nextLine(); //End of name block
  122.             source.nextLine(); //Raw count
  123.             String weightLine = source.nextLine();
  124.             weight = Double.valueOf(
  125.                 weightLine.substring(" | Avg. weight: ".length(), weightLine.length() - 3).trim()
  126.                 );
  127.             source.nextLine(); //End of count block
  128.             skipSection(source); //Abilities
  129.             skipSection(source); //Items
  130.             skipSection(source); //Spreads
  131.             skipSection(source); //Moves
  132.             source.nextLine(); //"Teammates" header
  133.             String teamLine = source.nextLine();
  134.             while(!teamLine.contains("--------")) {
  135.                 String teamName = teamLine.substring(" | ".length(), teamLine.indexOf("+")).trim();
  136.                 String teamStat = teamLine.substring(teamLine.indexOf("+")+1, teamLine.indexOf("%"));
  137.                 teammates.add(new PokemonRef(teamName, Double.valueOf(teamStat)));
  138.                 teamLine = source.nextLine();
  139.             }
  140.  
  141.             source.nextLine(); //"Checks and Counters" header
  142.             String checksLine = source.nextLine();
  143.             while(!checksLine.contains("--------")) {
  144.                 String checkName = checksLine.substring(
  145.                         " | ".length(),
  146.                         checksLine.indexOf("(") - "91.522 ".length()
  147.                     ).trim();
  148.                 String checkStat = checksLine.substring(
  149.                         checksLine.indexOf("(") - "91.522 ".length(),
  150.                         checksLine.indexOf("(") - 1).trim();
  151.                 counters.add(new PokemonRef(checkName, Double.valueOf(checkStat)));
  152.                 source.nextLine(); //Ignore clarification KO/switch line
  153.                 checksLine = source.nextLine();
  154.             }
  155.            
  156.         }
  157.        
  158.         public void weighCounters(Map<String, Pokemon> pokemonByName) {
  159.             for(PokemonRef counter : counters) {
  160.                 Pokemon pok = pokemonByName.get(counter.name);
  161.                 if(pok != null) {
  162.                     double c = counter.weight;
  163.                     double p = pok.weight;
  164.                    
  165.                     weighedCounters.add(new PokemonRef(counter.name, c * p));
  166.                 }
  167.             }
  168.         }
  169.        
  170.         public void print(PrintStream out) {
  171.             out.print("{");
  172.             out.printf("name: \"%s\", ", name);
  173.             out.printf("weight: %s, ", weight);
  174.             out.print("team: [");
  175.             for(PokemonRef ref : teammates) {
  176.                 out.print("{");
  177.                 out.printf("name: \"%s\", ", ref.name);
  178.                 out.printf("weight: %s", ref.weight);
  179.                 out.print("}, ");
  180.             }
  181.             out.print("], ");
  182.             out.print("counters: [");
  183.             for(PokemonRef ref : counters) {
  184.                 out.print("{");
  185.                 out.printf("name: \"%s\", ", ref.name);
  186.                 out.printf("weight: %s", ref.weight);
  187.                 out.print("}, ");
  188.             }
  189.             out.print("], ");
  190.             out.print("weighedCounters: [");
  191.             for(PokemonRef ref : weighedCounters) {
  192.                 out.print("{");
  193.                 out.printf("name: \"%s\", ", ref.name);
  194.                 out.printf("weight: %s", ref.weight);
  195.                 out.print("}, ");
  196.             }
  197.             out.print("]");
  198.             out.print("},");
  199.             out.println();
  200.         }
  201.     }
  202.    
  203.     private static class PokemonRef implements Comparable<PokemonRef> {
  204.         private final String name;
  205.         private final double weight;
  206.        
  207.         public PokemonRef(String name, double weight) {
  208.             this.name = name;
  209.             this.weight = weight;
  210.         }
  211.        
  212.         @Override
  213.         public int compareTo(PokemonRef b) {
  214.             return (int) Math.signum(b.weight - weight);
  215.         }
  216.     }
  217.    
  218.     private static void skipSection(Scanner scanner) {
  219.         String line = scanner.nextLine();
  220.         while(!line.contains("--------")) {
  221.             line = scanner.nextLine();
  222.         }
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement