Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML]

Expand Messages
  • Jeff 'japhy' Pinyan
    ... Well, I will certainly remind you of that. ... Until the application is done, it s a good idea to use warnings; as well. ... I m guessing this is where the
    Message 1 of 2 , Sep 28, 2005
    • 0 Attachment
      On Sep 27, Robin said:

      > # Anyone want to critique this game. No need to flame, just critique-
      > what's good about its functionality even though it is unfinished. And
      > yes, I know the goto statement sucks.

      Well, I will certainly remind you of that.

      > #!/usr/bin/perl
      >
      > use strict;

      Until the application is done, it's a good idea to

      use warnings;

      as well.

      > sub fight;
      > sub returnai;
      >
      > TOP: my $charactername;

      I'm guessing this is where the 'goto' statement returns to...

      > my $charclass;
      > my %classvals;
      > my $version = "1.0b";
      >
      > system 'cls' and print "\n" x 90;
      > print "Welcome to fightsim version $version\n";
      > sleep 4;
      >
      > print "\n" x 90;

      > my %s = ('quitp' => "(enter 'q' to quit)"
      >
      > );

      As a purely stylistic note, this is a poorly written hash. I almost
      always keep a trailing comma at the end of my hash key-value pairs so that
      I can add more later without having to add a comma after the last set.

      my %s = (
      quitp => "(enter 'q' to quit)",
      );

      And after seeing $charactername, $charclass, and %classvals... the name
      '%s' is particularly upsetting. What does 's' mean?

      > while () {
      > print "Enter a name for your character: " . $s{'quitp'};

      > $charactername = <STDIN>;
      > chomp ($charactername);

      chomp($charactername = <STDIN>);

      > exit if $charactername eq 'q';
      > next if ! $charactername or $charactername =~ /^\s*$/;

      That line could also be written as

      next unless $charactername =~ /\S/;

      where that means "repeat unless $charactername has at least one non-space
      character in it".

      > last if $charactername;
      > }

      We can rewrite the while loop like so:

      my $charactername = "";

      until ($charactername =~ /\S/) {
      print "PROMPT> ";
      chomp($charactername = <STDIN>);
      exit if $charactername eq 'q';
      }

      > print "\n" x 90;
      > while () {
      > if ($charclass ne 'a' and $charclass ne 'z'
      > and $charclass ne 'g' and $charclass ne 'G'
      > and $charclass ne 'A' and $charclass ne 'v'
      > and $charclass ne 'm' and $charclass and $charclass !~
      > /^\s*$/)
      > {
      > print "\n\nInvalid Class!\n\n";
      > }
      > print "You character, $charactername, will be a what?\nSelect a
      > character class from the options below." . $s{'quitp'} ;
      > print "\n\n";
      > print "Enter 'a' for 'Archer'\n";
      > print "Enter 'z' for 'Zapper'\n";
      > print "Enter 'g' for 'Goddess of Death'\n";
      > print "Enter 'G' for 'God of Happiness'\n";
      > print "Enter 'A' for 'Angel'\n";
      > print "Enter 'v' for 'Violence Itself\n";
      > print "Enter 'm' for 'Marching Man with Gun\n";

      I'd be so much easier to store these in a hash.

      my %char_classes = (
      a => 'Archer',
      z => 'Zapper',
      g => 'Goddess of Death',
      G => 'God of Happiness',
      A => 'Angel',
      V => 'Violence Itself',
      m => 'Marching Man with Gun',
      );

      We'll seen this again soon...

      > print "\n";
      > $charclass = <STDIN>;
      > chomp ($charclass);
      > exit if $charclass eq "q";
      > next if ! $charclass or $charclass =~ /^\s*$/;
      > next if $charclass ne 'a' and $charclass ne 'z'
      > and $charclass ne 'g' and $charclass ne 'G'
      > and $charclass ne 'A' and $charclass ne 'v'
      > and $charclass ne 'm';
      > #last if $charclass = /^[a|z|g|G|A|v|m|]$/;

      That commented line... it's got two problems: you used '=' instead of
      '=~', and you're using '|' inside a character class (err, that's the
      *regex* term "character class").

      > last if $charclass;
      >
      > }

      Ok, so here's that while loop re-written again:

      my $charclass = "";

      until ($char_classes{$charclass}) {
      print "PROMPT> ";
      chomp($charclass = <STDIN>);
      # or maybe just...
      # $charclass = substr(<STDIN>, 0, 1);

      exit if $charclass eq 'q';
      }

      > my $strength;
      > my $stamina;
      > my $weaknesses;
      > my $attack;
      > my $defend;
      > my $good;
      > my $evil;
      > #let max be 40 total points for each total class.
      >
      > if ($charclass eq 'a')
      > {
      > $strength = 7;
      > $stamina = 5;
      > $weaknesses=4;
      > $attack = 6;
      > $defend = 8;
      > $good = 9;
      > $evil = 1;
      > }
      > if ($charclass eq 'z')

      That should be an 'elsif', since there's no reason to check to see if
      $charclass is 'z' if it's already known to be 'a'. And these should be
      set up as a hash of array references:

      my %settings = (
      a => [7, 5, 4, 6, 8, 9, 1],
      z => [5, 7, 8, 8, 3, 3, 6],
      # ...
      );

      my ($strength, $stamina, $weakness, $attack, $defend, $good, $evil) =
      @{ $settings{$charclass} };

      > print "Let the fight begin!";
      > print "\n\n\n";

      > fight();
      >
      >
      > sub fight
      > {
      > srand;

      Don't call srand(); Perl will take care of that for you.

      > my @actions = ('BEFRIENDS', 'ATTACKS', 'DEFENDS', 'USES SPECIAL
      > MOVE');
      >
      > my %ai1=returnai();
      > my $enchar = $ai1{'character'};
      > my $output=undef;
      > my $hitpoints = 100;
      > my $charclass2 = undef;
      > $charclass2 = 'Archer' if $charclass eq 'a';
      > $charclass2 = 'Zapper' if $charclass eq 'z';
      > $charclass2 = 'Goddess of Death' if $charclass eq 'g';
      > $charclass2 = 'God of Happiness' if $charclass eq 'G';
      > $charclass2 = 'Angel' if $charclass eq 'A';
      > $charclass2 = 'Violence Itself' if $charclass eq 'v';
      > $charclass2 = 'Marching Man with Gun' if $charclass eq 'm';

      You can get $charclass2 from the %char_classes hash.

      A lot of what I've said applies to the rest of your code, and I'm a bit
      too busy to continue analyzing it.

      --
      Jeff "japhy" Pinyan % How can we ever be the sold short or
      RPI Acacia Brother #734 % the cheated, we who for every service
      http://www.perlmonks.org/ % have long ago been overpaid?
      http://princeton.pm.org/ % -- Meister Eckhart
    Your message has been successfully submitted and would be delivered to recipients shortly.