Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- my @typeNames = ( "Fire","Water","Grass","Neutral","Electric","Ice","Ground","Dark","Dragon");
- my @typeWeaknesses =
- (
- # Defenders
- # F W Gra N E I Gro Da Dr
- [0.5,0.5,2.0,1.0,1.0,2.0,1.0,1.0,1.0], #F - attackers
- [2.0,0.5,1.0,1.0,0.5,1.0,1.0,1.0,1.0], #W
- [0.5,1.0,0.5,1.0,1.0,1.0,2.0,1.0,1.0], #Gra
- [1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0], #N
- [1.0,2.0,1.0,1.0,0.5,1.0,0.5,1.0,1.0], #E
- [0.5,1.0,1.0,1.0,1.0,0.5,1.0,1.0,2.0], #I
- [1.0,1.0,0.5,1.0,2.0,1.0,0.5,1.0,1.0], #Gro
- [1.0,1.0,1.0,2.0,1.0,1.0,1.0,0.5,0.5], #Da
- [1.0,1.0,1.0,1.0,1.0,0.5,1.0,2.0,0.5] #Dra
- );
- my @teamInfo = loadTeamInfo();
- my ( $testString ) = @ARGV;
- my %enemyData = getDataFromCouple($testString);
- my $ETR = $enemyData{'types'};
- my @enemyTypes = @$ETR;
- my $EAR = $enemyData{'attacks'};
- my @enemyAttacks = @$EAR;
- #print "@enemyTypes\n";
- my $enemyString = typeInfoString(@enemyTypes);
- print "Enemy : $enemyString\n\n";
- my @battleInfo;
- for ( @teamInfo ) {
- my %data = %$_;
- my $typeRef = $data{'types'};
- my $info = typeInfoString(@$typeRef);
- my $aveAttack = getAverageAttack($data{'attacks'},\@enemyTypes,$data{'types'});
- my $aveWeakness = getAverageAttack(\@enemyAttacks,$data{'types'},\@enemyTypes);
- my $battleRate = $aveAttack * ( 1 / $aveWeakness ) ;
- #print "$battleRate : $info - $aveAttack / $aveWeakness\n";
- my %bInfo = (name => $info, battle => $battleRate, attack => $aveAttack, weakness => $aveWeakness );
- push @battleInfo , \%bInfo;
- }
- my @sortedBattle = sort sortFunc @battleInfo;
- #my @sortedBattle = @battleInfo;
- print "Rating Name Atk Weak\n";
- foreach ( @sortedBattle ) {
- my $hashRef = $_;
- my %hash = %$hashRef;
- #print "$hash{'battle'} - $hash{'name'} : $hash{'attack'} / $hash{'weakness'}\n";
- printf "%4.2f - %-13s : %4.2f / %4.2f\n", $hash{'battle'},$hash{'name'},$hash{'attack'},$hash{'weakness'};
- #print "$hashRef\n";
- }
- sub sortFunc
- {
- my %hashA = %$a;
- my %hashB = %$b;
- return $hashB{'battle'} <=> $hashA{'battle'};
- }
- sub typeInfoString {
- # print "typeInfoString : @_\n";
- my @types = @_;
- if ( @types == 1 ) {
- return "$typeNames[$types[0]]";
- } else {
- return "$typeNames[$types[0]]/$typeNames[$types[1]]";
- }
- }
- sub getDataFromCouple {
- # print "getDataFromCouple @_\n";
- my ($typeStr) = @_;
- my (@attacks,@types);
- # print(" getTypesFromCouple: $typeStr\n");
- if ( $typeStr =~ /:/ ) {
- $typeStr =~ /(.+):(.+)/;
- $typeStr = $1;
- my @attackNames = split(/,/,$2);
- @attacks = map {getTypeFromStr($_)} @attackNames;
- }
- if ( $typeStr =~ /\// ) {
- $typeStr =~ /(.+)\/(.+)/;
- @types = (getTypeFromStr($1),getTypeFromStr($2));
- if ( @attacks == 0)
- {
- @attacks = (getTypeFromStr($1),getTypeFromStr($2));
- }
- }
- else
- {
- @types = (getTypeFromStr($typeStr));
- if ( @attacks == 0 )
- {
- @attacks = (getTypeFromStr($typeStr));
- }
- }
- return ( 'types' => \@types, 'attacks' => \@attacks );
- }
- sub getTypeFromStr {
- # print " getTypeFromStr - @_\n";
- my ($typeStr) = @_;
- for (my $i=0; $i < @typeNames; $i++) {
- my $type = uc($typeNames[$i]);
- $typeStr = uc($typeStr);
- # print "$typeStr vs $type \n";
- if ( $type =~ /^$typeStr/ ) { return $i; }
- }
- die "Bad Type : $typeStr";
- }
- sub loadTeamInfo {
- open(INPUT,"PalTeams.txt") or die "Could not open PalTeams.txt\n";
- my @team;
- while (<INPUT>) {
- chomp;
- # print "reading $_\n";
- my %data = getDataFromCouple($_);
- push @team,\%data;
- }
- return @team;
- }
- sub getAverageAttack {
- my ($aRef,$dRef,$aTRef) = @_;
- my @attackTypes = @$aRef;
- my @defendTypes = @$dRef;
- my @attackerTypes = @$aTRef;
- my $attackNum = 0;
- my $attackTotal = 0;
- for my $attackType (@attackTypes) {
- my $damMulti = 1;
- $attackNum++;
- for my $defendType (@defendTypes) {
- my $atkMulti = getTypeMulti($attackType,$defendType);
- $damMulti *= $atkMulti;
- # print " $attackNum : $typeNames[$attackType] vs $typeNames[$defendType] : $atkMulti\n";
- }
- #print " total damMulti: $damMulti\n";
- if ( $attackType == $attackerTypes[0] || ( @attackerTypes > 1 && $attackType == $attackerTypes[1] ) ) {
- $damMulti *= 1.2;
- }
- $attackTotal += $damMulti;
- }
- return $attackTotal / $attackNum;
- }
- sub getTypeMulti {
- my ($atk,$def,@types) = @_;
- my $value = $typeWeaknesses[$atk][$def];
- # if ( $atk == $types[0] || ( @types > 1 && $atk == $types[1] ) )
- # {
- # $value *= 1.2;
- # }
- return $value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement