File Coverage

blib/lib/Acme/Hospital/Bed.pm
Criterion Covered Total %
statement 46 71 64.7
branch 7 22 31.8
condition 4 11 36.3
subroutine 8 12 66.6
pod 5 5 100.0
total 70 121 57.8


line stmt bran cond sub pod time code
1             package Acme::Hospital::Bed;
2              
3 2     2   125082 use 5.006;
  2         16  
4 2     2   10 use strict;
  2         3  
  2         38  
5 2     2   8 use warnings;
  2         3  
  2         77  
6 2     2   12 use feature qw/say/;
  2         3  
  2         1995  
7             our $VERSION = '0.02';
8              
9             sub new {
10 1     1 1 78 my ($package, %args) = @_;
11 1   50     9 $args{total_num_of_rooms} ||= 20;
12 1   50     5 $args{lifes} ||= 3;
13 1   50     5 $args{rooms} ||= [];
14 1   33     7 $args{max_length_of_stay} ||= $args{total_num_of_rooms}*2;
15 1         3 my $self = bless \%args, $package;
16 1 50       6 unless ($self->{names}) {
17             $self->{names} = [
18 1         5 qw/rob robert ben tom dean dennis ruby roxy jane michelle larry liane leanne anne axel/
19             ];
20             }
21 1 50       3 unless ($self->{phrases}) {
22             $self->{phrases} = [
23             [
24 1         8 "Hello, I am fine.",
25             "I still have my mind.",
26             ],
27             [
28             "Hello, I am still fine.",
29             "I still have my mind."
30             ],
31             [
32             "Hello, I am also fine.",
33             "I still have my mind."
34             ],
35             [
36             "Hello, I think I'm still fine.",
37             "I still have my mind."
38             ],
39             [
40             "Hello, I think you think I'm fine.",
41             "I still have my mind."
42             ],
43             [
44             "Hello, am I still fine.",
45             "Where is my mind?",
46             ],
47             [
48             "Hello, do you still think I'm fine.",
49             "Where is my mind?",
50             ],
51             [
52             "Hello, I don't feel so fine.",
53             "Where is my mind?",
54             ],
55             [
56             "Hello, This is not fine.",
57             "Where is my mind?",
58             ],
59             [
60             "Hello, I'm not fine.",
61             "Where is my mind?",
62             ]
63             ];
64             }
65 1         3 return $self;
66             }
67              
68             sub start {
69 0     0 1 0 my $self = shift;
70 0 0       0 unless (ref $self) {
71 0         0 $self = __PACKAGE__->new;
72             }
73 0         0 $self->next_patient;
74             }
75              
76             sub available_rooms {
77 38     38 1 41 return $_[0]->{total_num_of_rooms} - scalar @{$_[0]->{rooms}};
  38         226  
78             }
79              
80             sub next_patient {
81 19     19 1 58 $_[0]->check_patients_out;
82 19         31 my $available = $_[0]->available_rooms;
83 19 50       30 if ($available == 0) {
84 0         0 say 'You won this time.';
85 0         0 exit;
86             }
87 19         34 my %patient = $_[0]->_generate_patient();
88 19         73 my @phrases = @{ $_[0]->{phrases}->[$patient{level}] };
  19         38  
89 19         69 my $phrase = $phrases[int(rand(@phrases))];
90 19         43 say sprintf 'You have %s available rooms', $_[0]->available_rooms;
91 19         145 say sprintf('The next patients name is: %s. The patient will stay for: %s.', $patient{name}, $patient{length});
92 19         109 say $phrase;
93 19         45 my $ans = _wait_answer();
94 19 50       65 if ($ans eq 'y') {
    0          
95             $patient{level} < 6 ? do {
96 0         0 $_[0]->_lose_a_life();
97 19 50       30 } : do {
98 19         20 push @{$_[0]->{rooms}}, \%patient;
  19         41  
99             };
100             } elsif ($patient{level} > 5) {
101 0         0 $_[0]->_lose_a_life();
102             }
103 19 50       55 $_[0]->next_patient() unless $_[1];
104             }
105              
106             sub check_patients_out {
107 19     19 1 24 my $i = 0;
108 19         23 for (@{$_[0]->{rooms}}) {
  19         29  
109 171         177 $_->{length}--;
110 171 50       231 if ($_->{length} == 0) {
111 0         0 splice @{$_[0]->{rooms}}, $i, 1;
  0         0  
112 0         0 say 'Patient checked out: ' . $_->{name};
113             }
114 171         193 $i++;
115             }
116             }
117              
118             sub _lose_a_life {
119 0 0   0     if ($_[0]->{lifes}-- == 0) {
120 0           say 'GAME OVER!';
121 0           exit;
122             }
123 0           say sprintf 'You lose a life, %s lifes remaining.', $_[0]->{lifes};
124             }
125              
126             sub _wait_answer {
127 0     0     say 'Should they go to hospital? (y/n) ';
128 0           my $answer = ;
129 0           chomp $answer;
130 0 0         if ($answer !~ m/y|n/) {
131 0           return _wait_answer();
132             }
133 0           return $answer;
134             }
135              
136             sub _generate_patient {
137 0     0     my @names = @{$_[0]->{names}};
  0            
138             return (
139 0           name => sprintf('%s %s', map { $names[int(rand(@names))] } 0 .. 1),
140             level => int(rand(10)),
141 0   0       length => int(rand($_[0]->{max_length_of_stay})) || 1
142             );
143             }
144              
145             1;
146              
147             __END__