Skip to content

Instantly share code, notes, and snippets.

Created November 12, 2016 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/29a7efa54ed78a6597ff58d32b93cc0d to your computer and use it in GitHub Desktop.
Save anonymous/29a7efa54ed78a6597ff58d32b93cc0d to your computer and use it in GitHub Desktop.
=begin pod
=TITLE Hangman
=head2 Introduction
This is a Hangman program written in Perl6. It lets you make guesses
about which letters are in an unknown word. On the eighth incorrect
guess you lose.
The structure of the hangman program will look like this:
=begin code :spurt('hangman.pl6')
'Welcome to hangman!'.say;
§ Setup
my UInt:D $lives-left = 8;
while $lives-left > 0
{
§ User input
§ Check input
§ Check win
}
§ End
=end code
=head2 The Setup
First, we have the computer come up with a secret word which it chooses
randomly from a list of words read from a text file.
=begin code :name<Setup>
my Str:D @words = 'words.txt'.IO.lines.split(/\s+/);
my Str:D $secret-word = @words.pick;
=end code
Next we initialize the variable to hold the dashes.
=begin code :name<Setup>
my Str:D @dashes = ('-' x $secret-word.chars).comb;
=end code
=head2 Getting User Input
Now we can start the game. We ask for the user's guess and store it in
the C<$guess> variable.
=begin code :name('User input')
§ print dashes array
"You have $lives-left lives left".say;
"What's your guess? ".print;
my Str:D $guess = $*IN.get;
''.say;
=end code
=head2 Checking the User's Guess
We loop through the secret word, checking if any of its letters were
guessed. If they were, reveal that letter in the dashes array. If
none of the letters in secret word were equal to the guess, then
C<$got-one-correct> will be false, and one guess will be added.
=begin code :name('Check input')
my Bool:D $got-one-correct = False;
loop (my UInt:D $i = 0; $i < $secret-word.chars; $i++)
{
if $secret-word.comb[$i] eq $guess
{
$got-one-correct = True;
@dashes[$i] = $guess;
}
}
$lives-left -= 1 unless $got-one-correct;
=end code
=head2 Checking for Victory
Now we should check if the user has guessed all the letters.
Here we see if there are any dashes left in the array that holds the
dashes. If there aren't, the user has won.
=begin code :name('Check win')
if @dashes.grep('-').not
{
"You win! The word was $secret-word".say;
exit 0;
}
=end code
=head2 Pretty Printing the Dashes
We want the dashes to look pretty when they are printed, not look like
an array of chars. Instead of C<['-', '-', '-', '-']>, we want C<---->.
=begin code :name('print dashes array')
@dashes.join.say;
=end code
=head2 The End
=begin code :name<End>
"You lose. The word was $secret-word".say;
=end code
=head2 Words
Here is the file containing all the words for the game. It's just a
simple text file with words split by whitespace.
=begin code :spurt('words.txt')
able about account acid across act addition adjustment
advertisement after again against agreement almost among
attempt attention attraction authority automatic awake
baby back bad bag balance ball band base basin basket bath be
beautiful because bed bee before behaviour belief bell
bent berry between bird birth bit bite bitter black blade blood
carriage cart cat cause certain chain chalk chance
change cheap cheese chemical chest chief chin church circle clean clear
clock cloth cloud coal coat cold collar colour comb
come comfort committee common company comparison competition complete
complex condition connection conscious control cook copper copy
cord cork cotton cough country cover cow crack credit crime
delicate dependent design desire destruction detail development
different digestion direction dirty discovery discussion disease
last late laugh law lead leaf learning leather left letter level
library lift light like limit line linen lip liquid
morning mother motion mountain mouth move much muscle music nail
name narrow nation natural near necessary neck need needle
private probable process produce profit property prose protest public
pull pump punishment purpose push put quality question
seem selection self send sense separate serious servant shade shake
shame sharp sheep shelf ship shirt shock shoe short
square stage stamp star start statement station steam steel stem step
stick sticky stiff still stitch stocking stomach stone
stop store story straight strange street stretch strong structure
substance such sudden sugar suggestion summer sun support surprise
very vessel view violent voice waiting walk wall war warm wash waste
watch water wave wax way weather week weight well west
wet wheel when where while whip whistle white who why wide will wind
window wine wing winter wire wise with woman wood wool word
work worm wound writing wrong year yellow yesterday young
=end code
=end pod
# vim: ft=perl6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment