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-23 3:56

here have a mountain of scheme


(define (unordered-filter predicate lis)
  (letrec ((loop (lambda (lis acc)
                   (cond ((null? lis)
                          acc)
                         ((predicate (car lis))
                          (loop (cdr lis) (cons (car lis) acc)))
                         (else
                          (loop (cdr lis) acc))))))
   (loop lis '())))

(define (all? pred lis)
  (cond ((null? lis) #t)
        ((pred (car lis)) (all? pred (cdr lis)))
        (else #f)))

(define (range i j)
  (letrec ((loop (lambda (j acc)
                   (if (<= i j)
                     (loop (- j 1) (cons j acc))
                     acc))))
   (loop j '())))

(define (char-range a b)
  (map integer->char (range (char->integer a) (char->integer b))))

;; evaluates functions on a fixed input.
(define (evaluator x)
  (lambda (f)
    (f x)))

;; checks if a guess will match a fixed word.
(define (checker word)
  (lambda (guess)
    (equal? guess word)))

;; collects all indeces in which the letter matches a fixed word.
(define (matching-index-collector word)
  (lambda (letter)
    (letrec ((loop (lambda (i matches)
                     (cond ((>= i (string-length word))
                            matches)
                           ((equal? (string-ref word i) letter)
                            (loop (+ i 1) (cons i matches)))
                           (else
                            (loop (+ i 1) matches))))))
     (loop 0 '()))))

;; Sees how many times a letter will match a fixed word.
(define (scorer word)
  (lambda (letter)
    (letrec ((loop (lambda (i score)
                     (cond ((>= i (string-length word))
                            score)
                           ((equal? (string-ref word i) letter)
                            (loop (+ i 1) (+ score 1)))
                           (else
                            (loop (+ i 1) score))))))
     (loop 0 0))))

;; Sums the scores for a letter, recieved from a fixed list of scorers.
(define (sum-scorer scorers-list)
  (lambda (letter)
    (apply + (map (evaluator letter) scorers-list))))


;; evaluates to the element in domain-list that maximizes f.
(define (arg-max domain-list f)
  (letrec ((loop (lambda (current-arg-max current-max domain-list)
                   (if (null? domain-list)
                     current-arg-max
                     (let* ((next-arg (car domain-list))
                            (next-value (f next-arg)))
                       (if (> next-value current-max)
                         (loop next-arg next-value (cdr domain-list))
                         (loop current-arg-max current-max (cdr domain-list))))))))
   (loop (car domain-list) (f (car domain-list)) (cdr domain-list))))

(define (hang-man words-list goal-checker goal-matching-index-collector goal-length lives)
  (let ((chopped-words-list (unordered-filter (lambda (str) (= (string-length str) goal-length)) words-list)))))

;; returns (words-list missing-letters-list lives-count)
(define (hang-man-loop words-list goal-matching-index-collector missing-letters-list lives-count)
  (if (or (= lives-count 0) ;; dead
          (null? words-list) ;; word wasn't valid
          (null? missing-letters-list) ;; word contained unknown letters
          (null? (cdr words-list))) ;; got it down to one word!
    (list words-list missing-letters-list lives-count)
    (let* ((best-letter (arg-max missing-letters-list (sum-scorer (map scorer words-list))))
           (remaining-letters (unordered-filter (lambda (letter) (not (equal? letter best-letter)))
                                                missing-letters-list))
           (matching-indeces (goal-matching-index-collector best-letter)))
     (if (null? matching-indeces)
       (hang-man-loop words-list goal-matching-index-collector remaining-letters (- lives-count 1))
       (let ((remaining-words (unordered-filter (lambda (word)
                                                  (all? (lambda (index)
                                                          (and (< index (string-length word))
                                                               (equal? (string-ref word index) best-letter)))
                                                        matching-indeces))
                                                words-list)))
         (hang-man-loop remaining-words goal-matching-index-collector remaining-letters lives-count))))))


(define (hang-man words-list goal-matching-index-collector goal-string-length alphabet lives-count)
  (hang-man-loop (unordered-filter (lambda (str) (= (string-length str) goal-string-length)) words-list)
                 goal-matching-index-collector
                 alphabet
                 lives-count))


(define alphabet (map integer->char (range 0 255)))

(define goal-word (read))

(define words-list (read))

(define lives-count (read))

(define result (hang-man words-list (matching-index-collector goal-word) (string-length goal-word) alphabet lives-count))

(display result) (newline)


give it:

"goal word"
("dictionary" "list" ...)
lives-count

as input, and it'll:
1. pick an unchosen letter that would yield the largest sum of discoveries in all considerable words left in the dictionary.
2. find all positions where the letter matches the goal word.
3. filter the dictionary with words that match the chosen letter, in the same positions.

It seems alright, but consider it SLOW AS FUCK

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