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