[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] A "Cheating" hangman dealer



I've had mail problems, so prepare yourselves for multi-reply-mode...

 >>>
The Challenge
-------------

Create a "cheating hangman dealer", in Perl. This could be done in
a variety of ways, but I would like to see at least some solutions
that use QS, in order to see how the program's design changes.

The goal is to create something elegant and comprehensible, BTW, so
leave the white space in (:-). Also, I hope to write up this topic
in my UnixInsider (http://www.unixinsider.com) column, "Silicon Carny",
so please don't post any trade secrets or other IP you care about.

FWIW, I have downloaded the QP module, but haven't actually started
playing with it. I hope to have my own entries in the challenge, but
I will be just as happy to look at (and steal from :-) others' work...
 >>>

I didn't use Quantum::Superpositions, instead I used 
Quantum::Entanglement (on CPAN) which I wrote in a fit of pique a few 
weeks ago, the most interesting result of this is that you can play a 
version of the game where neither the dealer nor the player have any 
idea of what the word is until it is revealed.

I'd suggest that this is played with small words and don't forget to the 
give the program two arguements when you run it...  You can enter single 
letters, ranges ( [a-h] ) etc.  enter 'tell' to be given (one of) the 
possible words given the guesses made so far.

###


#!/usr/bin/perl -w

use Quantum::Entanglement;

my $num_letters = $ARGV[0];
my ($op, $t, $f);

if ($ARGV[1] =~ /play/) { # perfect player
   $op = '=~'; $t = ''; $f = ' not';
   $Quantum::Entanglement::conform = 1;
}
elsif ($ARGV[1] =~ /deal/) { # perfect dealer
   $op = '!~'; $t = ' not'; $f = '';
   $Quantum::Entanglement::conform = 1;
}
else { # Feynman style 20 questions
   $op = '=~'; $t = ''; $f = ' not';
   $Quantum::Entanglement::conform = 0;
}

print "Reading word list...";
open DICT, '</usr/share/dict/words' or die "dict: $!";
while (<DICT>) {
   chomp;
   push(@words,1,lc($_)) if length($_) == $num_letters;
}
close DICT; print "done\n";

my $word = entangle(@words);

while (<STDIN>) {
   chomp;
   print "So, you give up... $word\n" and last if /tell/;
   if (p_op($word, $op, qr/$_/)) {
     print " \$word does$t contain $_\n";
   }
   else {
     print " \$word does$f contain $_\n";
   }
}

__END__;

Usage:

./hangman.pl [num letters] [game type]

num letters => number of letters in word
game type   => /deal/ for a perfect dealer, so that the game is
                       weighted against the player.
                /play/ for a perfect player, so that every guess is
                       correct (if possible)
                other: for a Feynman style twenty questions type
                       game, where neither the player nor the dealer
                       know what the word will be, except that it must
                       remain consistant with the answers given by
                       the dealer

type in one letter at a time to guess, when you have had enough,
type in "tell" and you will be told one of the possible good words
which remain.

###

> Date: Fri, 16 Feb 2001 16:08:30 -0800
> From: Rich Morin <rdm@cfcl.com>
> Subject: Re: [FWP] A "Cheating" hangman dealer
> 
> At 5:59 PM -0500 2/16/01, Bernie Cosell wrote:
> 
>> Actually, is the 'true' correct? --- to handle this kind of thing
>> properly, don't you need to do fuzzy logic and shouldn't "$a>4" only
>> be "60% true"?
> 
> 
> Well, that's not the way it works in Damian's universe.  Why don't you
> look over the QP module and examples and see if it works for you?
> 

Quantum::Superpositions is useful beacuse it breaks this behaviour, the 
Quantum::Entanglement module is fun because it allows it (Hangman played 
in Feynman mode makes use of this, as do some of the examples provided 
in the demo/ of the module distribution).

> 
> Date: Fri, 16 Feb 2001 22:15:34 -0800
> From: Rich Morin <rdm@cfcl.com>
> Subject: Re: [FWP] A "Cheating" hangman dealer
> 
> Perhaps we should also commission a "cheating player", if only to
> test the durn thing...

Use the above example in perfect player mode...

--
Alex Gough (alex@rcon.org)
http://users.ox.ac.uk/~shug0957/

 


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe