Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Edit: Updated code in a reply to this reply. Use it instead of the one I posted in this node. It probably works better.

Before explaining my cool use for Perl, I would like to tell you that Perl has been absent from my life for a few months now and me, being unhappy with this fact, have tried to become more involved and active with the language. Consider this paragraph the "me telling you that Perl has been absent from my life".

Moving on. If you are like me in the fact that you enjoy playing a nice game of hangman over the Internet, then you might like the cool use for Perl I have (re)found. You might not like it too, if you dislike cheatingbeing assisted.

I have here a hangman helper, ready to find the word that you are looking for. Simply follow the simple instructions provided in the comments, and you will be on your simple way.

Take note of the Todo. The helper works fine without me implementing the feature, but it is still something that should be done to give maximum help.

#!/usr/bin/perl # Todo: # Narrow possibilties by eliminating words with repeat letters when I +have # guessed one of the repeats letters but the letter I guessed is eithe +r not a # repeat in the target word or is in a different position. # Example: # "rustlers" is a possible word, I guess 'r' and am presented with # "r _ _ _ r _ _ _", meaning the word has two r's, just in the wrong s +pot, # and therefore rustlers should be eliminated. use warnings; use strict; use 5.010; # Simple instructions: # perl $0 "w _ r d" "previousfailedguesses" say $ARGV[0]; my @word = split(/ /, $ARGV[0]); my $guessed = $ARGV[1] ? join('|', split(//, $ARGV[1])) : "0"; say $guessed; my %wordlist; # Hash of word-length arrays open(WORD, '<', '/usr/share/dict/american-english') or die $!; while (<WORD>) { chomp; next if /[^a-z]/; # Lazy way out~ my @chars = split(//, $_); push @{$wordlist{$#chars}}, $_; } close WORD; my @narrowed = @{$wordlist{$#word}}; # Narrowed possible answers by si +ze OUTER: for (my $i = 0; $i <= $#narrowed; $i++) { my @chars = split(//, $narrowed[$i]); # Narrowed by previous guesses if ($narrowed[$i] =~ /$guessed/) { splice(@narrowed, $i, 1); $i--; # Decrement counter now that word has been removed next OUTER; } # Narrowed by matching characters for (my $pos = 0; $pos <= $#word; $pos++) { next if $word[$pos] eq '_'; if ($word[$pos] ne $chars[$pos]) { splice(@narrowed, $i, 1); $i--; next OUTER; } } } # %alphabet holds the number of times a letter occurs within all words # %seen holds the number times a letter occurs in one word my %alphabet; $alphabet{$_} = 0 foreach ('a'..'z'); foreach my $word (@narrowed) { my %seen; $seen{$_} = 0 foreach ('a'..'z'); my @chars = split(//, $word); foreach my $char (@chars) { $alphabet{$char}++ if $seen{$char} == 0; # Limit 1 increment f +or each letter once per word $seen{$char}++; } undef %seen; } say $_ foreach @narrowed; # Word list say sort { $alphabet{$b} <=> $alphabet{$a} } keys %alphabet; # Most co +mmon letter, including ones already guessed

I have provided an example to go with it (for free!) too.

I have removed my first guess (which was the letter e) because the list of possible words was extremely large.

Word: _ _ _ _ _ _ _ _

I guess 'e' because the hangman helper told me it is the most common letter in all the words.

Word : e _ _ e _ _ _ _

I once again enter it, along with any incorrect guesses (none so far) into the hangman helper.

$ perl hangman.pl "e _ _ e _ _ _ _" "" e _ _ e _ _ _ _ 0 eagerest eateries echelons eclectic edgeways edgewise effected egresses embedded embezzle emceeing emperors endeared endeavor endemics enfeeble engender enmeshed enmeshes ensemble ententes entering envelope envelops especial essences esteemed ethereal eugenics eutectic exceeded excelled excepted excerpts excesses expected expedite expelled expended expenses expertly extended exterior external ensdtcxrlpimagohbvwyufzjkq

The most common letter I have not guessed is 'n', so that is what I will guess.

Word: e _ _ e _ _ _ _
Incorrect: 'n'

Aha! A letter not in the word. Let me tell Mr. Hangman.pl this and see what he thinks.

$ perl hangman.pl "e _ _ e _ _ _ _" "n" e _ _ e _ _ _ _ n eagerest eateries eclectic edgeways edgewise effected egresses embedded embezzle emperors especial esteemed ethereal eutectic exceeded excelled excepted excerpts excesses expected expedite expelled expertly exterior etdxscrpilagmwybouhfzjknvq

He thinks the next letter should be 't', which I guessed.

Word: e _ _ e _ t _ _
Incorrect: 'n'

$ perl hangman.pl "e _ _ e _ t _ _" "n" e _ _ e _ t _ _ n eclectic effected eutectic excepted expected expertly tecxdpilryufwajkhgnvmsqbzo

'x' is the next letter I should guess.

Word: e _ _ e _ t _ _
Incorrect: 'n', 'x'

$ perl hangman.pl "e _ _ e _ t _ _" "nx" e _ _ e _ t _ _ n|x eclectic effected eutectic teciduflwraxjykhgnvmspqbzo

I guess 'i' as it suggests.

Word: e _ _ e _ t i _
Incorrect: 'n', 'x'

$ perl hangman.pl "e _ _ e _ t i _" "nx" e _ _ e _ t i _ n|x eclectic eutectic tieculwraxdjykhgfnvmspqbzo

It wants me to guess 'u', I guessed 'u'.

Word: e _ _ e _ t i _
Incorrect: 'n', 'x', 'u'

$ perl hangman.pl "e _ _ e _ t i _" "nxu" e _ _ e _ t i _ n|x|u eclectic tielcwraxdjyukhgfnvmspqbzo

The word was eclectic! With only three wrong guesses, Mr. hangman.pl and I won! Yay!

I don't mind occasionally having to reinvent a wheel; I don't even mind using someone's reinvented wheel occasionally. But it helps a lot if it is symmetric, contains no fewer than ten sides, and has the axle centered. I do tire of trapezoidal wheels with offset axles. --Joseph Newcomer


In reply to Hangman Assistant by Lawliet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-23 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found