Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl -w use strict; my @words; my $dict = '/usr/dict/words'; my @letters = ( 'a' .. 'z'); my $minlen = 4; # 0 1 2 3 4 5 6 7 8 + 9 10 11 12 my @h = ( 10000000, -90000, 10000, -9000, 1000, -900, 500, -400, 100, +-90, 10, -9, 1); open(DICT,"< $dict") or die "Can't open /usr/dict/words: $!\n"; @words = map { chomp; tr/A-Z0-9.,' -/a-z/d; (length($_) >= $minlen)?$_ +:() } <DICT>; close(DICT); my $word = ''; my $wordtest = '^\w$'; # the RE of the vaild next words while(<>) { chomp; tr/A-Z/a-z/; if(not $_ =~ /$wordtest/) { print "Invalid character. Target: '$word'\n"; next; } $word = $_; @words = grep /$word/i, @words; if(scalar @words == 0) { print "'$word' is not part of any word. You loose.\n"; last; } if(is_exact($word)) { print "'$word' is exactly matched. You win. Game over.\n"; last; } print "Player:\t\t$word\n"; $word = do_computer($word,\@words); print "Computer:\t$word\n"; if(is_exact($word)) { print "'$word' is exactly matched. You loose. Game over\n"; last; } $wordtest = "^\\w$word\$|^$word\\w\$"; } exit; sub is_exact { my ($word) = @_; return scalar (grep /^$word$/, @words); } sub do_computer { my ($word,$words) = @_; my @words = @$words; my %prechars; my %postchars; my $h; my @list; my @pres; my @posts; my $re; my $char; # first off, see what characters are available foreach (@words) { m/(.)$word/; if(defined $1 and $1 ne '') { $prechars{$1} = 0; } m/$word(.)/; if(defined $a and $1 ne '') { $postchars{$1} = 0; } $postchars{$1} = 0; } # now, we've got a list of all the good chars # so we now need to find the best character to choose. # what is best? # even number left to target word foreach $char (keys %prechars) { $h = 0; $re = qr/$char$word/; @list = grep s/$re//, @words; foreach (@list) { $h += length($_); } $prechars{$char} = $h; } foreach $char (keys %postchars) { { $h = 0; $re = qr/$word$char/; @list = grep s/$re//, @words; foreach (@list) { $h += length($_); } $postchars{$char} = $h; } @pres = sort { $prechars{$b} <=> $prechars{$a} } keys %prechars +; @posts = sort { $postchars{$b} <=> $postchars{$a} } keys %postchar +s; if($prechars{$pres[0]} > $postchars{$posts[0]}) { $word = $pres[0] . $word; } else { $word = $word . $posts[0]; } return $word; }

In reply to Ghost (wordgame) by m_turner

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 chilling in the Monastery: (4)
As of 2024-04-19 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found