December 5, 2014
This is a simple classic hangman. The basic mechanic of the game is that the computer will randomly selected a word from our word database ("word_list.rkt"). The user will then try to guess what the word is. As the user makes correct, or incorrect guesses, the hangman or word will be updated. If the user makes 7 wrong guesses, the racket man will die of asphyxiation.
- Word Dictionary is used to store the random words for the computer to pick.
- The concept of Global Variables were used. Several of our lists and counters are global.
- We used a lot of recursion for traversing and storing letters in a list. Both iterative and true recursive.
- Several lambdas are used in both the GUI and logic portions of the code.
- Filter is used with an index to pick our random word.
External Technology and Libraries
The only external libraries that we use are the canvas and the text-field box, which exist in the racket/gui/base. Both of these libraries were used exclusively in the GUI portion of the code.
The way we integrated these 2 components with the logic is:
- Started out by drawing a the world with an empty platform using canvas. - We then attach text-field to the main frame of the project. - The text-field listens for the "Enter" key to be pressed. It will grab the character in the text-field when this happens. - The character in the text-field will then be passed on to the "guess" function. This is where the logic side comes in. - The "guess" function will then check the guess, update the game state accordingly, and request the canvas redraw with the new state.
Favorite Lines of Code
Each partner should identify a favorite line of code, Scheme expression, or entire procedure written by himself / herself in the entire project, and explain what it does. E.g.:
- Vibol: @@(define guess-box (new text-field% (label "Guess Box")
(parent frame) (callback (lambda (tf ce) (if (equal? (send ce get-event-type) 'text-field-enter) (begin (update-user-guess (send (send guess-box get-editor) get-text)) (when (not (eq? "" (send (send guess-box get-editor) get-text))) (guess (send (send guess-box get-editor) get-text))) (send (send guess-box get-editor) erase) ) (set! wrong-count wrong-count) )))))@@
This is the function for the text field which listen for an "enter key" press event, and get the string from that text-field and send it through the logic (guess function). It will eventually redraw the canvas based on the outcome of the guess function.
(define (guess user-guess)
(let ((correct? #f)) (define (guess-loop char-list char-index) (if (null? char-list) (if (not correct?) (begin (displayln "WRONG!") (update-game-state-guesses) ;draw a piece of the hangman (draw (cadr game-state) (- 7 (car game-state))) ) (displayln "List updated") ) (if (string=? (car char-list) user-guess) (begin (displayln "Correct!") (update-game-state-correct-list user-guess char-index) (draw (cadr game-state) (- 7 (car game-state))) ;draw the character (set! correct? #t) (guess-loop (cdr char-list) (+ 1 char-index)) ) (guess-loop (cdr char-list) (+ 1 char-index)) ) ) )(guess-loop (get-global-characters) 0) (cadr game-state) )
This is the main guess driver. It takes an argument of a single character, checks to see if the character appears one or more times in the word. If it does, it will update the users correct guess list and the number of correct guesses and have the canvas redraw with the new list. If not, it will decrement the remaining guesses, and notify the canvas to draw an additional portion of the hangman.
Technology Used Block Diagram
Below are the diagrams of the game and the external components that were used.