Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

I made a hangman program...

Name: Anonymous 2008-04-27 1:15

...with perl. I am so proud.

Name: Anonymous 2008-04-27 1:16

I suppose I should post it.

$dict = $ARGV[1] || 'words.list';
die "Dict file \"$dict\" does not exist." unless (-e $dict);

open DICT, $dict;
srand; rand $. < 1 and length $_ > 8 and chomp ($word = lc $_) while <DICT>;
close DICT;

$word =~ s/[^ \&\-a-z]//g;
$max_gs = $ARGV[0] || length $word;

($w_gs,$r_gs) = ('','');
print "Hangman!\n";
$_ = $word; s/[^ \&\-$r_gs]/_/g ; print "$_\n";

while (1) {
    print "Guess: ";
    chomp ($g = lc <STDIN>);
    redo if $g !~ /^[a-z]$/ || "$r_gs$w_gs" =~ /$g/;
 
    ($word =~ /$g/ ? $r_gs : $w_gs) .= $g;
 
    print (length $w_gs); print "/$max_gs\t";

    $_ = $word; s/[^ \&\-$r_gs]/_/g ; print "$_\n";

    if ($_ eq $word) {
        print "\nLIFE!\n";
        last;
    }
    if(length $w_gs >= $max_gs) {
        print "\nDEATH!\n";
        print "The word was $word\n";
        last;
    }
}

Name: Anonymous 2008-04-27 1:20

Hey, nice job.

Name: Anonymous 2008-04-27 1:27

Wow, Perl is hideous.

Name: Anonymous 2008-04-27 1:43

$_ = $word; s/[^ \&\-$r_gs]/_/g ; print "$_\n";

ewww

Name: Anonymous 2008-04-27 2:40

if only I had a dict :(

Name: Anonymous 2008-04-27 3:49

>>6
/usr/share/dict/words

Name: Anonymous 2008-04-27 3:50

>>6
/usr/share/dict/words

Name: Anonymous 2008-04-27 4:20

>>7,8
/usr/dict/words

Name: Anonymous 2008-04-27 4:25

My eyes died.

Name: Anonymous 2008-04-27 14:02

>>5
Whut? How would you put it in one line? Yes, I was purposely trying to make it as small as possible.

Name: Anonymous 2008-04-27 14:59

print "$_\n"; = say;

Name: Anonymous 2008-04-27 15:03

>>11
s/[^ &\$rgs-]/_/g, print for "$word\n";

Name: Anonymous 2008-04-27 15:08

Don't try to make it small, try to make it easy to read;

Name: Anonymous 2008-04-27 16:08

>>13
Nice trick, I am actually using $_ later in the while loop but point for the same sentence before the while loop.

>>14
Where is the fun in that? This was actually a big change on a hangman program I did a while back which was all readable and long and boring. I actually split the letters into an array and then used for loops on it, orz, but then I discovered regexes and this followed.

Name: Anonymous 2008-04-27 19:56

>>14
I think that you haven't finished to write your message. Specifically, i miss the reference to guido van rossum and his forced indentation of the game

Name: Anonymous 2008-04-27 19:58

Ignore waht everyone here says. THey areny expert programmers like me. I think your software is great, because it is exactly how a program should be written; it should have many sigils and be very small so that it could be read in 1 day.
Thank you for posting.

Name: Anonymous 2008-04-27 22:42

>>1
explicit use of $_ considered harmful.

my $max_gs = shift;
push '/usr/share/dict/words' unless @ARGV;
die "Dict file \"$dict\" does not exist." unless (-e) for @ARGV;

my @words;
s/[^ &a-z-]//g and length > 8 and push @words, chomp lc while <>;
die 'No words in dict.' unless @words;

my $word = $words(rand @words);
$max_gs ||= length $word;

do {
 print "Guess: ";
 chomp ($g = lc <>);
 redo if $g !~ /^[a-z]$/ || $r_gs . $w_gs =~ /$g/;
 for ("$word\n") {
  (/$g/ ? $r_gs : $w_gs) .= $g;
  ${($r_gs, $w_gs)[!/$g/]} .= $g;
  s/[^ &$r_gs-]/_/g
  print length $w_gs, "/$max_gs\t";
  print;
  print "\nLIFE!\n" and exit if /^$word$/;
 }
} while (length $w_gw >= $max_gs);

print "\nDEATH!\n";
print "The word was $word\n";

Name: Anonymous 2008-04-27 22:56

>>18
oops...

do {
 print "Guess: ";
 chomp ($g = lc <>);
 redo if $g !~ /^[a-z]$/ || $r_gs . $w_gs =~ /$g/;
 for ("$word\n") {
  (/$g/ ? $r_gs : $w_gs) .= $g;
  s/[^ &$r_gs-]/_/g
  print length $w_gs, "/$max_gs\t";
  print;
  print "\nLIFE!\n" and exit if /^$word$/;
 }
} while (length $w_gw >= $max_gs);


that extra line ended up in there because of a bad CTRL-C CTRL-V.

Name: Anonymous 2008-04-28 19:03

>>18,19
Nice job, especially the use of $_. Your random line finder uses up a lot of memory but it's correct. I just realized that mine is wrong because it could just go through a file without picking a line at all. Good solution is to actually have a decent word file so you don't have to filter it.

Name: Anonymous 2008-04-28 23:37

>>20
Actually, there is a much better solution. fseek() to random position in file, read until newline, and next line will be your word. You won't have read all contents.

Name: Anonymous 2008-04-29 0:11

>>21
but that doesn't empty @ARGV... and doesn't allow using multiple files...

Name: Anonymous 2008-04-29 7:32

>>22
And also isn't uniform. Chances favor any word which directly follows another word whose length is greater than the average word length.

Name: Anonymous 2008-04-29 7:50

Read the words into a list and use List::Shuffle

Name: Anonymous 2008-04-29 8:23

>>22,23
Still, you will read much less data from file.

Name: Anonymous 2008-04-29 8:35

/prog/ sucks at perl today


#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use List::Util qw[first shuffle];
use File::Slurp;
use feature 'say';

my @files = ( $ARGV[1], 'words.list', '/usr/share/dict/words' );
my $dict = first { $_ && -e $_ } @files;
croak "Couldn't find a dict file" unless defined $dict;

my @words = map { lc } read_file $dict;
@words = shuffle @words;

say "Fucking hangman!";

thegame: foreach my $word (@words) {
  chomp $word;

  my $guesses     = 8;
  my $fails       = 0;
  my @underscores = (q[_]) x ( length $word );

  while ($guesses) {
    print join q[ ], @underscores;
    say q[ ] . q[|] x $fails;
    my $guess = lc <STDIN>;

    if ( $guess eq $word ) {
      say "Winnar!";
      next thegame;
    }

    $guess = substr $guess, 0, 1;
    say $guess;
    redo if $guess !~ /[a-z]/;

    my $found = 0;
    for my $i ( 0 .. $#underscores ) {
      if ( substr( $word, $i, 1 ) eq $guess ) {
        $underscores[$i] = $guess;
        $found = 1;
      }
    }
    $fails++ unless $found;

    if ( join( q[], @underscores ) eq $word ) {
      say "Winrar!\n$word";
      next thegame;
    }

    last if $fails == $guesses;
  }

  say "Fail\n$word";
}


Name: Anonymous 2008-04-29 8:38

my @words = map { lc } read_file $dict;
@words = shuffle @words;


wouldn't just my @words = shuffle map { lc } read_file $dict; be enough?

Name: Anonymous 2008-04-29 8:49

>>27
Yes, yes it would. Not much loss, though.

Name: Anonymous 2008-04-29 11:49

You know word files are usually megabytes long. So reading a file into a 20k element array and shuffling it is a bad idea.

Name: Anonymous 2008-04-29 11:52

@perl $is %terrible

Name: Anonymous 2008-04-29 12:12

>>30
lol opinions

Name: Anonymous 2008-04-29 13:06

>>31
lol facts
Fixed

Name: Anonymous 2008-04-29 13:26

>>32
hax my anus
Fixed

Name: Anonymous 2008-04-29 13:39

>>32
Prease terr me how these are facts.

Name: Anonymous 2008-04-29 13:45

>>34
If you don't know, you're not sucessfur businessman.

Name: Anonymous 2008-04-29 14:37

>>26
uranus sucks at perl today.

my $max_gs = shift;
push '/usr/share/dict/words' unless @ARGV;
die "Dict file \"$dict\" does not exist." unless (-e) for @ARGV;

my @words;
s/[^ &a-z-]//g and length > 8 and push @words, chomp lc while <>;
die 'No words in dict.' unless @words;

my $word = $words[rand @words];
$max_gs ||= length $word;

do {
 print "Guess: ";
 chomp ($g = lc <>);
 redo if $g !~ /^[a-z]$/ || $r_gs . $w_gs =~ /$g/;
 for ("$word\n") {
  (/$g/ ? $r_gs : $w_gs) .= $g;
  s/[^ &$r_gs-]/_/g
  print length $w_gs, "/$max_gs\t";
  print;
  print "\nLIFE!\n" and exit if /^$word$/;
 }
} while (length $w_gw >= $max_gs);

print "\nDEATH!\n";
print "The word was $word\n";

Name: Anonymous 2008-04-29 16:02

>>35
Fucking furries.

Name: Anonymous 2009-02-25 6:36

doesn't come as default.

Name: Anonymous 2010-12-20 22:59

Name: Anonymous 2010-12-26 20:41

Name: Sgt.Kabukiman䖉 2012-05-22 23:30

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Name: James Gosling 2013-04-06 16:11

GAWWWWWWWWZMACSSSSSS FLABBERGASTS MY AUDIENCE

Name: Anonymous 2013-09-01 14:01


Of course there are multiple solutions that all mean something different:

Name: Anonymous 2013-09-01 15:32


There is now a 1.54 patch. Also there is a patch to update from 1.50 to 1.54 (and from 1.52 to 1.54 if you have the DL version).
This update adds a third version of the "Secret Basement", the dungeon that generates random items in your inventory each floor.

Don't change these.
Name: Email:
Entire Thread Thread List