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

★【Challenge!】【Easy】 Hangman Solver

Name: !WIZardrY9E!UhEWzHM7+1tYUG2 2012-05-14 13:16

⚫ The Game of Hangman

A player guesses letters that are part of a word!
If he has not guessed the complete word before making six (6) mistakes, he has lost!

      _______
     |/      |
     |      (_)
     |      \|/   It is a game over!
     |       |
     |      / \
     |
  ___|___


⚫ Your Challenge

In a language of your choice, write a program that takes as its input (in some way) the target word (with blanks) and the letters that have been guessed already. It should then output the next letter to be guessed.
For example:

$ echo -n "p...s\npds" | ./guesser
e

You may assume the word to be guessed is an English word and contains only lowercase ASCII letters.
Provide code and instructions if usage is not obvious. Consider using a tripcode to ease conversation about your entry.

Entries will be judged on GOODITUDE (defined as its win/loss ratio over twenty random games), on SPEED, and on CLEVERNESS.

Your deadline is 23:59:59 on Sunday, May 20th. Results will be posted the following day.

 |∧_∧
 |´・ω・) < Good luck!♫
 ||と ノ

Name: Anonymous 2012-05-15 21:07

>>39
Maybe >>37-kun is Andrew S. Tanenbaum. You don't know.

Name: Anonymous 2012-05-15 23:46

This challenge is fucking dumb.

Name: Anonymous 2012-05-16 10:59

>>42
Your face is fucking dumb.

Name: Anonymous 2012-05-16 11:48

>>41
But Andrew S. Tanenbaum deserves more animosity from the systems programming community and beyond than anyone

Name: Anonymous 2012-05-16 11:59

>>44
why?

Name: Anonymous 2012-05-16 13:52

>>45
Modern Operating Systems, 3rd edition - Andrew S. Tanenbaum
By reading it, you will learn to hate him. In part due to his pandering to the Uni money-grubbing book market, by inflating every sentence to fill a fucking page; mostly however, because he's stuck in the '90s and refuses to acknowledge that a micro kernel isn't THE ONE TRUE WAY IN 100% OF CASES

Name: Anonymous 2012-05-16 17:19

>>46
actually it is, it's just that he doesn't realizes that C is fucking shit.

Name: Anonymous 2012-05-16 18:25

>>46
money-grubbing
Jealous, dumb goy?

Name: Anonymous 2012-05-16 19:15

>>40
I'm sorry to have to say this, but your code fucking sucks; also you don't have to comment every single line.

Name: Anonymous 2012-05-16 19:55

>>30
This doesn't seem to work.

$ echo -e "kangar..\nargnk" | ./hangman
hangman: Prelude.maximum: empty list
$ echo -e "p.ncre.s\nepsrcn" | ./hangman
hangman: Prelude.maximum: empty list

May just be the fact that I'm using GHC 6.12.1, though. I don't know enough Haskell anymore to tell.

Name: Anonymous 2012-05-16 20:03

>>37,40
You really need to take guessed letters into account. If the word isn't in your word list, it will guess wrong letters forever instead of eventually working its way to right ones.

Name: Anonymous 2012-05-16 20:08

>>49
It's a beginner's challenge. Be nice or constructive.

Name: Anonymous 2012-05-17 0:50

>>51
b-b-b-b-bbbbut how can we know what letters were already guessed if we only get the guess as input?
>>49
ss-s-s-sorrrry anon-kun, i will do better next time!

Name: Anonymous 2012-05-17 1:10

>>53
OH NO
I DIDN'T READ
ahhhhh
sorry anon-kun. im slow. forgive me!

Name: Anonymous 2012-05-17 9:19

>>50
I think you're missing the freqlist file

Name: Anonymous 2012-05-17 11:00

>>55
No, then it would say this:

hangman: freqlist: openFile: does not exist (No such file or directory)

Name: Anonymous 2012-05-17 17:35

>>56
I only know what GHC 7.0 complains about. I could imagine a readFile function that would just return empty if the file didn't exist. But yes, I did write it with GHC 7.0.4 and I honestly don't know enough about haskell to know how to fix it.

Name: Anonymous 2012-05-17 18:01

>>57
I upgraded to GHC 7.4.1 (the most recent version), and it's still doing the same thing as in >>50. Are you sure it works for you?

Name: Anonymous 2012-05-17 18:49

>>58
Just copied it from here and pastebin and it works fine.
$ echo -e "p.ncre.s\nepsrcn" | ./hangman
a

Maybe the freqlist file is improperly formatted on your end?

The first non-empty non-comment line is supposed to be the letters a-z with spaces. Followed by a line of 26 fp numbers, followed by a lot of lines like this:
a   0.0002835 0.0228302 0.0369041 0.0426290 0.0012216 0.0075739 0.0171385 0.0014659 0.0372661 0.0002353 0.0110124 0.0778259 0.0260757 0.2145354 0.0005459 0.0195213 0.0001749 0.1104770 0.0934290 0.1317960 0.0098029 0.0306574 0.0088799 0.0009562 0.0233701 0.0018701
where the first column is the prefix

Name: Anonymous 2012-05-17 18:53

>>59
I removed the commented and empty lines at the start of the freqlist file, and now it seems to work. I don't know why that should matter if it doesn't on your end, but I guess the important thing is that it works now.

Name: Anonymous 2012-05-17 19:43

>>60
You know what, I bet you're on OSX. I just tried with classic Mac line endings (ie. '\r') and Windows (ie. "\r\n") and got the same error. It's probably getting the '\r' in each line and misinterpreting it as a key character.

Name: Anonymous 2012-05-17 20:01

>>61
Nope, Linux here.
I did wget the file rather than copy/paste it, so I thought I might have gotten bad line endings that way because that's happened before, but apparently not.

              ∧_∧     
      ∧_∧    (´<_`  ) ∠ It's a mystery
     ( ´_ゝ`)    /  ⌒i  
 ̄\  /   / ̄ ̄ ̄ ̄ ̄/. |    
 ̄ ̄| /   ./         /. | |
 ̄| |(__ニつ/_____/_| |____
田| | \___))\    (u ⊃
ノ||| |       ⌒ ̄


Oh well, it works now.

Name: Anonymous 2012-05-18 15:27

Bampu pantsu. I see /prog/ is shitflooding again.

Name: Anonymous 2012-05-18 15:41

>>63
fuck you nigger I'm going to shitbump ever shitpost in the last 20 years

Name: Anonymous 2012-05-18 15:45

                           __, R,
                        f '/ Y ^ `┐ pieces _ ~I
                    _, Ino i / / ∧ヽ
                ー= off '_ input / - ─ ┴亠-, / ^ Isuzu,
                     / / / /'' ¯ ¯ l (c) `T -` _-, vinegar,
                  ヽpcs / / / / ∧ _ "J ij i`ヽsystem!!!!
                   / '|!! / / Haruna x re Buddha Ii l / l re
                ! {i Centro Do弌ヽapplication / ~I chair Te, ij |
                       Doヾthe / / / f ∧ re `i_ ji ::: ::::": |
                  / / :/ / c "" '`''' boards Roh l
                   / / :/ / / \ `'│ c" do a
               ¯ \ \ / / jl _ Doヽ, ~I. '| | ii
                - |} \ / (c) s nailゝ¯ `ーDo ~A | il
               /ヽ, _ /: / / :::::ヽ/ | ::::::> I | | '¯ `┐
                / / :: =:.! l │ :: - / /, | / :::: l ¯>
             / c ::: l ::::: {::::ヽ/ / J I-─ -: | l __ / ∧
            .!! / f {::::::: l i l :: re :: _ ::: <__ ~I> :::: 〃> He N.!
            〃 :::: f f =: i ii i i c I was ::::: :::::: /! ! ⌒ / ::::::: | Ha
             Ha l_ ∠ │ :::: 〃 Explosion 〃 butterfly i ii i / 8 / c `ーre :: :::::::::: i │! :> │ヾ
           to i │ヽ/ {c L__ i_i_ module j re f ::: ::::: ___ :: boards :: Roh ~I Lee |ヽ!.
            re | _ / \ ____ {=ヽ= `ー-} /ヽt"ヾ> I
           , '"/ I {ヽ- i /乂} /ヽ___
          ! / Lee r '- _} "i ∧ ∨ー
      , '"/ vector r-ya __ヽ_ -. tt ∧ヽ,
. Roh '¯ ¯, - ¬ - <'' ::: {Chi}ヽ\ `ー─' 'ヽ, 〃 / {=} <
two being '/ ::: :::::::::::::::::: {≠} \ / {switch} \ ::::ヽ, _ヽ- two knee ┐ '"
::::ヽf: ¯ ¯ :::::::::::::::::::: / :/ {∠}ヽ/ {∠} :::::: \ :::::::: `ー=, Le `f
c ::::: \ :::::::::::::::::::::: / :::: / :: <- Explosion {/ /> :::ヽ:::::::: ::::::::::::::::::::::ヽ- Lee seven ::::>
:::: :::: ∧ ::: \ ::::::::::::::::::::::: / :::::: Mu / / I f ≧} ::::::::: :::::::::::::::::::::::: / ::::::::::: / off ::::
::::::::ヽ:::::::::::::::::::::::::::::: / ::::::::::: The example `_ / eyes Do f ::::: :::::::::::::::::::::::::::::::::::::::::::: / :::: ∧
:::::::::::ヽ:::::::::::::::::::::::::::::::: ::::::: / :::: '/ ::::::::::: :::::::::::::::::::: Ha勹ヽ弌ヽ::::::::::::::::::: / ::::::: | f
::::::::::::::ヽ:::::: :::::::::::::::::::::::::: / :::::::::: \ ∨ f ┬ __, Kim 's / :::: Ha ::::::::::: :::::::::::::::::::::::::::::::::: / ::: :::::: |. _ system
\ :::::::::::: Ha :::::::::::::::::::::::::: Weゝ::::::::::::::::::: _ `:/ヾHim '" ::::::::::::::::::::: : Ha :::::::::::::::::::::::::::: / ::::::::::: | /

Name: Anonymous 2012-05-18 15:53

>>64
good l,uck bumping eVERY SINGLE POST in the HISTORY OF 4CHAN

Name: Anonymous 2012-05-18 16:02

>>66
AuTIsM is the disease, and DUBZ are the cure

Name: Anonymous 2012-05-18 16:05

no, rought peneratritooN Slave Bdsm harcode Bondage would be teh ecureurCuu
No wait, there is no cure.

Name: Anonymous 2012-05-18 16:13

>>68
yo'uRe wrog,n dubz aRE THE CURE!!!!1

Name: Anonymous 2012-05-18 16:21

>>69
ok butt why do you talk in AlTerNATE CapZ?

Name: Anonymous 2012-05-18 16:27

>>70
I am nOt comPLEtely cure.d

Name: Anonymous 2012-05-19 10:32

Bomp. Deadline is tomorrow and we've only had three entries.

Name: VIPPER 2012-05-19 10:39

>>72
Thanks for remind me, i wanted to send in a cheap one lol.
But i forgot about continuing working on it.

Name: Anonymous 2012-05-19 16:36

new entry. anon-kun, is this suitable?

import sys
validwords = filter(lambda tuplist : len(filter(lambda tup : tup[0] == '.' or tup[0] == tup[1], tuplist)) == len(tuplist), map(lambda word : zip(sys.argv[1], word), filter(lambda word : len(word) == len(sys.argv[1]), map(lambda word : word.strip(), open("words.txt", "r")))))
letters = {}
for word in validwords:
    used = []
    for guess, letter in word:
        if letter in letters:
            letters[letter] += 1
        else:
            letters[letter] = 1
if(len(sys.argv) == 3):
    letters = dict((k, v) for k,v in letters.iteritems() if k not in sys.argv[2])
chosen = max(letters.iterkeys(), key=lambda k : letters[k] )
print chosen

sytnax - python <script name> <guess> <guessed letters in a list ie abcde>
use . to indicate unguessed letters
also, this fails if the word isn't in the word list
word list format is just 1 word per line in "words.txt" in the same directory as the script

Name: Anonymous 2012-05-19 16:37

and i forgot to remove used = [] :(((((((((
crap, anon-kun, i always screw up somehow

import sys
validwords = filter(lambda tuplist : len(filter(lambda tup : tup[0] == '.' or tup[0] == tup[1], tuplist)) == len(tuplist), map(lambda word : zip(sys.argv[1], word), filter(lambda word : len(word) == len(sys.argv[1]), map(lambda word : word.strip(), open("words.txt", "r")))))
letters = {}
for word in validwords:
    for guess, letter in word:
        if letter in letters:
            letters[letter] += 1
        else:
            letters[letter] = 1
if(len(sys.argv) == 3):
    letters = dict((k, v) for k,v in letters.iteritems() if k not in sys.argv[2])
chosen = max(letters.iterkeys(), key=lambda k : letters[k] )
print chosen

Name: Anonymous 2012-05-19 21:55

Against random words the best strategy is to maximize your expected chance of winning, but because the game tree grows exponentially you can't brute force every possibility and have to use a heuristic. ``Most used character'' is a very good heuristic, and ``most entropy in result for this turn'' is also pretty good but harder to compute. How many of the top heuristic choices to examine is a trade-off, but because of diminishing returns I like 2. Perhaps a dynamic choice would be best.

On another note, don't forget to filter out words that have characters that have already been guessed and found not be in the word.

#lang racket/base

;; Usage: racket -t hangman.rkt [word misses]
;; Input is taken from stdin if not given on command line

(define word-list "corncob_lowercase.txt")
(define lives 6)
(define search-width 2)

(require racket/file racket/set srfi/1 srfi/13 racket/flonum)

(define argv (current-command-line-arguments))
(when (zero? (vector-length argv)) (set! argv (vector (read-line) (read-line))))

(define (number-of-words-containing c words)
  (count (lambda (word) (string-index word c)) words))
(define (string->seteqv s) (for/seteqv ((c (in-string s))) c))

(define word (vector-ref argv 0))
(define misses (vector-ref argv 1))
(define acceptable?
  (let ((r (regexp (format "^~a$" (if (string-null? misses) word (regexp-replace* "\\." word (format "[^~a]" misses)))))))
    (lambda (word) (regexp-match? r word))))
(define cur-words (filter acceptable? (file->lines word-list)))
(define full-alphabet (string->seteqv "abcdefghijklmnopqrstuvwxyz"))
(define cur-alphabet (set-subtract full-alphabet (string->seteqv word) (string->seteqv misses)))

(define (best alphabet words lives)
  (define (wins c)
    (define next-alphabet (set-remove alphabet c))
    (define rx (regexp (regexp-quote (string c))))
    (define h (make-hash))
    (for-each (lambda (word) (hash-update! h (regexp-match-positions* rx word) (lambda (v) (cons word v)) '())) words)
    (define (group-wins k v)
      (define next-lives (if (null? k) (- lives 1) lives))
      (let-values (((_ w) (best next-alphabet v next-lives))) w))
    (define (total-wins) (apply + (hash-map h group-wins)))
    (cond
      ((and (= (hash-count h) 1) (null? (car (hash-keys h))))
       0)
      (else
       (total-wins))))
  (define n (length words))
  (define in-all
    (do ((a alphabet (set-intersect a (string->seteqv (car ws))))
         (ws words (cdr ws)))
      ((or (set-empty? a) (null? ws)) a)))
  (cond
    ((zero? lives)
     (values #f 0))
    ((= n 0)
     (error "That's not possible."))
    ((= n 1)
     (define letters-left (set-intersect alphabet (string->seteqv (car words))))
     (if (set-empty? letters-left)
         (values #f 1)
         (values (car (set->list letters-left)) 1)))
    ((not (set-empty? in-all))
     (define c (car (set->list in-all)))
     (define c-wins (wins c))
     (values c c-wins))
    (else
     (define choices (set->list alphabet))
     (define sorted-choices (sort choices > #:key (lambda (c) (number-of-words-containing c words))))
     (define choices-to-examine (take sorted-choices search-width))
     (let loop ((l choices-to-examine) (best-c #f) (best-wins 0))
       (cond
         ((null? l)
          (values best-c best-wins))
         (else
          (define c (car l))
          (define c-wins (wins c))
          (cond
            ((= c-wins n)
             (values c c-wins))
            ((> c-wins best-wins)
             (loop (cdr l) c c-wins))
            (else
             (loop (cdr l) best-c best-wins)))))))))

(let-values (((c w) (best cur-alphabet cur-words (- lives (string-length misses)))))
  ; (printf "Best choice is \"~a\", winning ~a out of ~a times (~a%)\n" c w (length cur-words) (fl* (fl/ (exact->inexact w) (exact->inexact (length cur-words))) 100.))
  (printf "~a\n" c))

Name: Anonymous 2012-05-20 5:12

dubz. slick 'em, flick 'em, stick 'em, pick 'em, click 'em.

Name: Anonymous 2012-05-20 6:54

When a problem comes along you must whip it.

Name: VIPPER 2012-05-20 16:49

(use-modules (ice-9 rdelim))

(define Hang 0)
(define Len 1)
(define File "hangman.txt")
(define Char 0)

(define Handle
        (open-input-file File))
(define Word
        (read-line Handle)); For time reasons i put one word in here :/
; Thought of making it read lines in a loop and the test being a random number of some sort with the last read line being the word
(while (and (if (= Hang 6)
                (begin (display "LOST!\n") ; I wish i had time to make it draw a hanging man but i dont have it
                       #f))
            (not (= (string-length Word)
                    Len)))
       (set! Char
             (read-char (current-input-port)))
       (if (not (char=? Char #\nl)) ; Filter #\nl(\n) read when pressing enter
           (if (not (char=? Char (string-ref Word
                                             Len)))
               (set! Hang
                     (+ 1 Hang))
               (begin (set! Len
                            (+ 1 Len))
                      (display Char))))

A very simple and primitive implementation
Im made this sort of in a real rush, but i send it in anyway.
I wish i had the time to improve it.

Dont be hard on me since this is my first real scheme program.
It should work, i couldnt test i as much as i should.

Im glad OP took the time to make a programming contest, i missed those.

Name: Anonymous 2012-05-20 20:10

>>79
I hate to break this to you, VIPPER-kun, but that wasn't what was asked.

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