| 1 | package Games::LMSolve; | ||||||||||
| 2 | 3 | $Games::LMSolve::VERSION = '0.14.0'; | |||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 3 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use Getopt::Long; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use Pod::Usage; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # You can think of this module as a Factory[1] for the solver classes. | ||||||||||
| 12 | # It reads the -g/--game/--preset [variant name] command line option | ||||||||||
| 13 | # and determines which class to instantiate based on it. | ||||||||||
| 14 | # | ||||||||||
| 15 | # Note that it does not touch the other command line options so the | ||||||||||
| 16 | # GetOptions() called by the main() function of the class will be | ||||||||||
| 17 | # able to process them. | ||||||||||
| 18 | # | ||||||||||
| 19 | # [1] - Refer to the book "Design Patterns" by Erich Gamma et. al. | ||||||||||
| 20 | # | ||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | sub new | ||||||||||
| 24 | { | ||||||||||
| 25 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 26 | |||||||||||
| 27 | 0 | my $self = {}; | |||||||||
| 28 | |||||||||||
| 29 | 0 | bless $self, $class; | |||||||||
| 30 | |||||||||||
| 31 | 0 | $self->_initialize(@_); | |||||||||
| 32 | |||||||||||
| 33 | 0 | return $self; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | sub _initialize | ||||||||||
| 37 | { | ||||||||||
| 38 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_initialize: not called CC=2 SCAR=17.9
| |||||||||||
| 39 | |||||||||||
| 40 | 0 | my %args = @_; | |||||||||
| 41 | |||||||||||
| 42 | 0 | $self->{'games_solvers'} = {}; | |||||||||
| 43 | |||||||||||
| 44 | 0 | $self->register_all_solvers(); | |||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | if ( exists( $args{'default_variant'} ) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | { | ||||||||||
| 48 | 0 | $self->set_default_variant( $args{'default_variant'} ); | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 0 | return 0; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | sub set_default_variant | ||||||||||
| 56 | { | ||||||||||
| 57 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
set_default_variant: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 58 | |||||||||||
| 59 | 0 | my $variant = shift; | |||||||||
| 60 | |||||||||||
| 61 | 0 | $self->{'default_variant'} = $variant; | |||||||||
| 62 | |||||||||||
| 63 | 0 | return 0; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | |||||||||||
| 67 | sub register_solvers | ||||||||||
| 68 | { | ||||||||||
| 69 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
register_solvers: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 70 | |||||||||||
| 71 | 0 | my $games = shift; | |||||||||
| 72 | |||||||||||
| 73 | 0 | $self->{'games_solvers'} = { %{ $self->{'games_solvers'} }, %$games }; | |||||||||
| 74 | |||||||||||
| 75 | 0 | return 0; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | |||||||||||
| 79 | sub register_all_solvers | ||||||||||
| 80 | { | ||||||||||
| 81 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
register_all_solvers: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | |||||||||||
| 83 | 0 | return 0; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | |||||||||||
| 87 | sub main | ||||||||||
| 88 | { | ||||||||||
| 89 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
main: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 90 | |||||||||||
| 91 | 0 | my $variant = $self->{'default_variant'}; | |||||||||
| 92 | 0 | my $help = 0; | |||||||||
| 93 | 0 | my $man = 0; | |||||||||
| 94 | |||||||||||
| 95 | 0 | Getopt::Long::Configure('pass_through'); | |||||||||
| 96 | 0 | ▶ | GetOptions( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | "g|game=s" => \$variant, | ||||||||||
| 98 | 'help|h|?' => \$help, | ||||||||||
| 99 | 'man' => \$man | ||||||||||
| 100 | ) or pod2usage(2); | ||||||||||
| 101 | |||||||||||
| 102 | 0 | ▶ | pod2usage(1) if $help; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | ▶ | pod2usage( -exitstatus => 0, -verbose => 2 ) if $man; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | |||||||||||
| 105 | 0 | ▶ | if ( !exists( $self->{'games_solvers'}->{$variant} ) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | { | ||||||||||
| 107 | 0 | die "Unknown game variant \"$variant\""; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 0 | my $class = $self->{'games_solvers'}->{$variant}; | |||||||||
| 111 | 0 | my $game; | |||||||||
| 112 | 0 | ▶ | if ( ref($class) eq "CODE" ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | { | ||||||||||
| 114 | 0 | $game = $class->(); | |||||||||
| 115 | } | ||||||||||
| 116 | else | ||||||||||
| 117 | { | ||||||||||
| 118 | 0 | $game = $class->new(); | |||||||||
| 119 | } | ||||||||||
| 120 | 0 | $game->main(); | |||||||||
| 121 | } | ||||||||||
| 122 | 3 | 1; | |||||||||
| 123 | |||||||||||
| 124 | __END__ | ||||||||||