Skip to content

Instantly share code, notes, and snippets.

Created November 12, 2016 22:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/adb4f36e8487761dd617cdf22bb9d8f5 to your computer and use it in GitHub Desktop.
Finn example (markdown dialect)
# Hangman
## 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:
--- >hangman.pl6
'Welcome to hangman!'.say;
§ Setup
my UInt:D $lives-left = 8;
while $lives-left > 0
{
§ User input
§ Check input
§ Check win
}
§ End
---
## 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.
--- Setup
my Str:D @words = 'words.txt'.IO.lines.split(/\s+/);
my Str:D $secret-word = @words.pick;
---
Next we initialize the variable to hold the dashes.
--- Setup
my Str:D @dashes = ('-' x $secret-word.chars).comb;
---
## Getting User Input
Now we can start the game. We ask for the user's guess and store it in
the `$guess` variable.
--- User input
§ print dashes array
"You have $lives-left lives left".say;
"What's your guess? ".print;
my Str:D $guess = $*IN.get;
''.say;
---
## 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
`$got-one-correct` will be false, and one guess will be added.
--- 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;
---
## 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.
--- Check win
if @dashes.grep('-').not
{
"You win! The word was $secret-word".say;
exit 0;
}
---
## Pretty Printing the Dashes
We want the dashes to look pretty when they are printed, not look like
an array of chars. Instead of `['-', '-', '-', '-']`, we want `----`.
--- print dashes array
@dashes.join.say;
---
## The End
--- End
"You lose. The word was $secret-word".say;
---
## Words
Here is the file containing all the words for the game. It's just a
simple text file with words split by whitespace.
--- >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
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment