Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Dear Monks, I seek your enlightenment so that I may one day fulfill my life's dream of writing my own MUD. The following contains some simple code please comment. Am I utilizing OO correctly? Would my example be correct for sending objects to combat? Thanks Monks

#!/usr/bin/perl use strict; use warnings; use Combat; use Peasant; my $henry = Peasant->new(name => "Henry"); my $joe = Peasant->new(name => "Joe"); my $combat = Combat->new(name => "combat object"); $combat->initiative($joe, $henry); $combat->combat($joe, $henry); ------------------------------------------------------------ package Combat; use strict; use warnings; sub new { my ($class, %arg) = @_; my $objref = { _turns => $arg{name} || 0, _name => $arg{name} || "unknown" }; bless $objref, $class; return $objref; } sub combat { my ($combatref, $self, $self2) = @_; $self->attack($self2); $self2->attack($self); } sub initiative { my ($combatref) = shift @_; my @combatants = @_; foreach(@combatants) { #check initiative code here } } 1; ------------------------------------------------------------ package Peasant; use strict; use warnings; sub new { my ($class, %arg) = @_; my $objref = { _name => $arg{name} || "unknown", _dex => $arg{dex} || int(rand(10)) + 2, _hp => $arg{hp} || int(rand(10)) + 2, _gold => $arg{gold} || int(rand(30)) + 2, _title => $arg{title} || "farmboy" }; bless $objref, $class; return $objref; } sub attack { my ($self,$target) = @_; my $amount = int(rand(10)) + 2; print "$self->{_name} attacks $target->{_name} for $amount!\n" +; $target->damage( $amount, $self ); } sub acquiregold { my ($self, $amount) = @_; $self->{_gold} += $amount; } sub damage { my ($self, $amount, $attacker) = @_; $self->{_hp} -= $amount; if ( $self->{_hp} <= 0 ) { print "$attacker->{_name} has killed $self->{_name}!!! +\n"; print "$attacker->{_name} gained $self->{_gold} gold!! +!\n"; $attacker->acquiregold($self->{_gold}); } } sub hitpoints { my ($self, $hits) = @_; print "$self->{_name} HP $self->{_hp}\n"; } 1;

Considered by CountZero: Delete: no content
Content restored by Arunbear, using the mirror of g0n.

In reply to Advice on Perl OO program structure (MUD) by yoda54

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    Jar. Jar!...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (5)
    As of 2018-06-25 01:12 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.