Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[examples] make hangman.pl compatible with Rakudo
git-svn-id: http://svn.pugscode.org/pugs@30682 c213334d-75ef-0310-aa23-eaa082d1ae64
  • Loading branch information
svatsan committed May 18, 2010
1 parent 51fb5a4 commit 18f4d16
Showing 1 changed file with 41 additions and 24 deletions.
65 changes: 41 additions & 24 deletions examples/games/hangman.pl
@@ -1,4 +1,5 @@
use v6;
use File::Spec::Unix;

## declare global variables (globals RULE dude!)

Expand All @@ -11,23 +12,23 @@

## do our functions

sub cls {
sub cls returns Void {
run(($?OS eq any <MSWin32 mingw cygwin>) ?? 'cls' !! 'clear');
}

sub get_committer_list (Str $dict_file) returns List {
my @committers;
my $dict = open($dict_file) or die "Couldn't open the AUTHORS file.\nYou must run this script from within the main pugs\ndirectory or within the examples/ sub-directory.";
my $dict = open($dict_file) or die "Couldn't open the AUTHORS file.\n";

# Skip the intro text
1 while $dict.lines ~~ rx:P5/\S/;
1 while $dict.lines ~~ /\S/;

for $dict.lines -> $name {
# Capture the real name part
if ($name ~~ rx:P5/^(.+?)(?:\s\s|$)/) {
if $name ~~ /(<alpha>+)>>\s*/ {
my $realname = $0;
# Remove nickname
$realname ~~ s:P5/\s*".*"\s*/ /;
$realname ~~ s/\s*\".*\"\s*/ /;
@committers.push($realname);
}
}
Expand All @@ -41,19 +42,29 @@ sub cls {

sub draw_board returns Str {
my $output = '';
for (0 .. (+@letters - 1)) -> $i {
if (@letters[$i] ~~ rx:P5/[-.,\s]/) {
$output ~= @letters[$i];
@solution[$i] = @letters[$i];
}
elsif (@solution[$i] ne '') {
$output ~= @solution[$i];
}
else {
$output ~= '_';
}
}
return $output;
my @letters if !defined(@letters);
# for (0...+@letters-1) -> $i {
# if @letters[$i] ~~ /[-.,\s]/ {
# $output ~= @letters[$i];
# @solution[$i] = @letters[$i];
# }
# elsif @solution[$i] ne '' {
# $output ~= @solution[$i];
# }
# else {
# $output ~= '_';
# }
# }
for(0..+@letters-1) -> $item {
if $item ~~ /<[\-\.,\s]>/ {
$output = @letters[$item];
} elsif @solution[$item] ne '' {
$output = @solution[$item];
} else {
$output = '_';
}
}
return $output;
}

sub has_won returns Bool {
Expand All @@ -68,7 +79,7 @@ sub cls {
my $success = 0;
my $i;
for 0 .. +@letters - 1 -> $i {
if (uc(@letters[$i]) eq $guess) {
if uc(@letters[$i]) eq $guess {
@solution[$i] = @letters[$i];
$success = 1;
}
Expand Down Expand Up @@ -101,12 +112,14 @@ sub cls {
}

## main loop
use lib 'ext/File-Spec/lib', '../ext/File-Spec/lib', '../../ext/File-Spec/lib';
use File::Spec;
#use lib 'ext/File-Spec/lib', '../ext/File-Spec/lib', '../../ext/File-Spec/lib';
#use File::Spec;

my $progdir = splitpath($*PROGRAM_NAME)[1] || ".";
my $dict = canonpath("$progdir/../../AUTHORS");
say "Getting committers list...";
my @committers = get_committer_list($dict);
say "I got committers list..." ~ @committers>>.Str.join(",").perl.say;
my $current_committer = pick_committer(@committers);

@letters = split("", $current_committer);
Expand All @@ -117,15 +130,15 @@ sub cls {
while my $letter = $*IN.get {
cls;

if (guess($letter)) {
if (has_won()) {
if guess($letter) {
if has_won() {
print draw_hangman("You won!!!!\n");
exit;
}
}
else {
$number_of_bad_guesses++;
if ($number_of_bad_guesses >= $allowed_bad_guesses) {
if $number_of_bad_guesses >= $allowed_bad_guesses {
print draw_hangman(
"You have exceeded the maximum number of tries.\n" ~
"Sorry, the committer was '$current_committer'\n"
Expand All @@ -137,6 +150,7 @@ sub cls {
print draw_hangman("guess a letter? ");
}


=begin pod
=head1 NAME
Expand Down Expand Up @@ -169,3 +183,6 @@ =head1 COPYRIGHT
See http://www.perl.com/perl/misc/Artistic.html
=end pod


## vim: ft=perl6

0 comments on commit 18f4d16

Please sign in to comment.