Acme-Hospital-Bed

 view release on metacpan or  search on metacpan

lib/Acme/Hospital/Bed.pm  view on Meta::CPAN

use 5.006;
use strict;
use warnings;
our $VERSION = '0.05';

sub new {
	my ($package, %args) = @_;
	$args{total_num_of_rooms} ||= 20;
	$args{lifes} ||= 3;
	$args{rooms} ||= [];
	$args{max_length_of_stay} ||= $args{total_num_of_rooms}*2;
	my $self = bless \%args, $package;
	unless ($self->{names}) {
		$self->{names} = [
			qw/rob robert ben tom dean dennis ruby roxy jane michelle larry liane leanne anne axel/
		];
	}
	unless ($self->{phrases}) {
		$self->{phrases} = [
			[
				"Hello, I am fine.",

lib/Acme/Hospital/Bed.pm  view on Meta::CPAN

	$_[0]->check_patients_out;
	my $available = $_[0]->available_rooms;
	if ($available == 0) {
		say('You won this time.');
		exit;
	}
	my %patient = $_[0]->_generate_patient();
	my @phrases = @{ $_[0]->{phrases}->[$patient{level}] };
	my $phrase = $phrases[int(rand(@phrases))];
	say(sprintf 'You have %s available rooms', $_[0]->available_rooms);
	say( sprintf('The next patients name is: %s. The patient will stay for: %s days.', $patient{name}, $patient{length}));
	say($phrase);
	my $ans = _wait_answer();
	if ($ans eq 'y') {
		$patient{level} < 6 ? do {
			$_[0]->_lose_a_life();
		} : do {
			push @{$_[0]->{rooms}}, \%patient;
		};
	} elsif ($patient{level} > 5) {
		$_[0]->_lose_a_life();
	}
	$_[0]->next_patient() unless $_[1];
}

sub check_patients_out {
	my $i = 0;
	for (@{$_[0]->{rooms}}) {
		$_->{length}--;
		if ($_->{length} == 0) {
			splice @{$_[0]->{rooms}}, $i, 1; 
			say('Patient checked out: ' . $_->{name});
		}
		$i++;
	}
}

sub _lose_a_life {
	if ($_[0]->{lifes}-- == 0) {
		say('GAME OVER!');

lib/Acme/Hospital/Bed.pm  view on Meta::CPAN

		return _wait_answer();
	}
	return $answer;
}

sub _generate_patient {
	my @names = @{$_[0]->{names}};
	return (
		name => sprintf('%s %s', map { $names[int(rand(@names))] } 0 .. 1),
		level => int(rand(10)),
		length => int(rand($_[0]->{max_length_of_stay})) || 1
	);
}

sub say {
	print $_[0] . "\n";
}

1;

__END__

lib/Acme/Hospital/Bed.pm  view on Meta::CPAN

	Acme::Hospital::Bed->new(%options)->next_patient(1);	

=head2 available_rooms

This method will return the number of available rooms left for the current game.

	$ahb->available_rooms;

=head2 check_patients_out

This method will itterate the current rooms arrayref, deducting the length of stay by 1 day and will remove (check out) any patients that have reached 0.

	$ahb->check_patients_out

=head1 AUTHOR

LNATION, C<< <email at lnation.org> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-acme-hospital-bed at rt.cpan.org>, or through

t/01-testing.t  view on Meta::CPAN

use Test::More;
use Acme::Hospital::Bed;
BEGIN {
	no warnings 'redefine';
	*Acme::Hospital::Bed::_generate_patient = sub {
		return (
			name => 'Any Name',
			level => 6,
			length => 25
		);
	};
	*Acme::Hospital::Bed::_wait_answer = sub { return 'y' };
}
my $ahb = Acme::Hospital::Bed->new;
$ahb->next_patient(1) for 1..19;
is(scalar @{$ahb->{rooms}}, 19);
is($ahb->{lifes}, 3);
is($ahb->{total_num_of_rooms}, 20);
is($ahb->{max_length_of_stay}, 40);
done_testing();



( run in 0.345 second using v1.01-cache-2.11-cpan-65fba6d93b7 )