root/lang/perl/Games-Go-Rank/trunk/lib/Games/Go/Rank.pm @ 12415

Revision 12415, 2.7 kB (checked in by hanekomu, 5 years ago)

r1742@88-117-77-175: marcel | 2008-05-25 19:01:27 +0200

  • brought back warnings and strict to appease Perl::Critic, although Moose
    turns them on

Line 
1package Games::Go::Rank;
2
3use warnings;
4use strict;
5use Moose;
6
7our $VERSION = '0.05';
8
9use overload
10    '""'  => 'stringify',
11    '<=>' => 'num_cmp';
12
13has 'rank' => (is => 'rw');
14
15
16sub stringify {
17    my $self = shift;
18    $self->rank;
19}
20
21
22sub as_value {
23    my $self = shift;
24    my $rank = $self->rank;
25    return unless defined $rank;   # so m// doesn't complain
26
27    if    ($rank =~ /^\s*(\d+)[\s-]*k/) { return -$1 + 1 }
28    elsif ($rank =~ /^\s*(\d+)[\s-]*d/) { return  $1     }
29    elsif ($rank =~ /^\s*(\d+)[\s-]*p/) { return  $1 + 7 }
30    else                                { return         }
31}
32
33
34sub from_value {
35    my ($self, $value) = @_;
36    if ($value <= 0) {
37        $self->rank(sprintf "%sk" => -$value + 1)
38    } elsif ($value > 0 && $value <= 7) {
39        $self->rank(sprintf "%sd" => $value)
40    } elsif ($value > 7) {
41        $self->rank(sprintf "%sp" => $value - 7)
42    } else {
43        $self->clear_rank;
44    }
45    return $self;
46}
47
48
49sub num_cmp {
50    my ($lhs, $rhs, $reversed) = @_;
51    for ($lhs, $rhs) {
52        if (ref $_ eq __PACKAGE__) {
53            $_ = $_->as_value;
54        } else {
55            $_ = __PACKAGE__->new(rank => $_)->as_value;
56        }
57    }
58    ($lhs, $rhs) = ($rhs, $lhs) if $reversed;
59    for ($lhs, $rhs) { $_ = -99 unless defined($_) }
60    $lhs <=> $rhs;
61}
62
63
641;
65
66
67__END__
68
69{% USE p = PodGenerated %}
70
71=head1 NAME
72
73Games::Go::Rank - represents a player's rank in the game of Go
74
75=head1 SYNOPSIS
76
77    use Games::Go::Rank;
78
79    my $black_rank = Games::Go::Rank->new(rank => '1k');
80    my $white_rank = Games::Go::Rank->new(rank => '2d');
81    if ($white_rank > $black_rank) { ... }
82
83=head1 DESCRIPTION
84
85This class represents a player's rank in the game of Go. Rank objects can be
86compared to see whether two ranks are equal or whether one rank is higher than
87the other. Rank objects stringify to the rank notation.
88
89Use the standard notation for ranks such as C<30k>, C<5k>, C<1d>, C<2p> and so
90on. You can also use other common formats such as C<6-dan> or C<2 dan>.
91Anything after the first C<k>, C<d> or C<p> is ignored. So C<6-dan*> is the
92same as C<6-dan>, which is the same as C<6d>.
93
94=head1 METHODS
95
96=over 4
97
98{% p.write_methods %}
99
100=item as_value
101
102Returns a number representing the rank. C<1k> is returned as C<0>, lower kyu
103ranks are returned as negative numbers (C<2K> is C<-1>, C<3k> is C<-2> etc.).
104Dan ranks are returned as positive numbers, with pro ranks coming immediately
105after dan ranks. For example, C<1d> == C<1>, C<7d> == C<7>, C<1p> == C<8>,
106C<2p> == C<9>. Only dan ranks up to 7d are recognized as amateur ranks -
107that is, C<8d> == C<1p>.
108
109=item from_value
110
111Sets the rank from a numerical value that is interpreted as described above.
112
113=back
114
115{% p.write_inheritance %}
116
117{% PROCESS standard_pod %}
118
119=cut
120
Note: See TracBrowser for help on using the browser.