CS 1102 Homework 7: Script Position

Due: October 12 (Tuesday) at 11:59pm via turnin (assignment name hwk7).

Assignment Goals


Exercises

To test your solutions, keep the originals in your file, copy the define-script macro from the posted class code, and make sure you get the same answer from both the original and converted code.

Note that much of the code here consists of helper functions so you can test your results. You only need to convert two functions on each problem.

  1. The following code provides the core of a mastermind program that you want to release on the web. Mastermind is a game in which the player tries to guess a hidden sequence of five colors. After each guess, the game replies with the number of colors in the sequence that are in the right position (the number of white pegs) and the number of colors that are in the sequence but in the wrong position (the number of black pegs). The game continues until the player has guessed the entire sequence of colors.

    Convert both prompt-guess and mastermind to scripts (treat all other helper functions as built-in primitives, meaning you should leave them as defines and not convert them or move calls to them).

    ;; prompts users for a guess at mastermind
    ;; enter list using parens at prompt
    (define (prompt-guess)
      (begin (printf "Enter a list of colors ")
             (read)))
    
    ;; guess-correct? : list[symbol] list[symbol] -> boolean
    ;; determines whether two lists are identical in contents (including order)w
    (define (guess-correct? newguess target)
      (andmap symbol=? newguess target))
    
    ;; calc-white : list[symbol] list[symbol] -> number
    ;; count number of positions that match exactly between two lists
    (define (calc-white tlist glist)
      (length (filter (lambda (p) (symbol=? (first p) (rest p)))
                      (map cons tlist glist))))
    
    ;; calc-white : list[symbol] list[symbol] -> number
    ;; count number of positions from first list that do match exactly but do appear in
    ;;   second list [ASSUMES that colors in both lists are unique]
    (define (calc-black tlist glist) 
      (let ([unmatched (filter (lambda (p) (not (symbol=? (first p) (rest p))))
                               (map cons tlist glist))])
        (let ([gum (map first unmatched)]
              [tum (map rest unmatched)])
          (length (filter (lambda (g) (memq g tum)) gum)))))
    
    ;; mastermind : list[symbol] list[symbol] number -> number
    ;; plays mastermind, returning number of tries needed to get right answer
    (define (mastermind target guess tries)
      (cond [(guess-correct? guess target) tries]
            [else (begin
                    (printf "You get ~a white and ~a black pegs~n"
                            (calc-white target guess)
                            (calc-black target guess))
                    (mastermind target (prompt-guess) (+ 1 tries)))]))
    
    (mastermind (list 'red 'blue 'yellow) (prompt-guess) 1)
    
  2. The following code provides the core of a blackjack game program that you want to release on the web (blackjack is a card game: the goal is to get as close as possible to 21 points without going over, where your points are the sum of numbers on your cards).

    Convert both prompt-card and blackjack to scripts (treat all other helper functions as built-in primitives, meaning you should leave them as defines and not convert them or move calls to them).

    ;; prompt-card : number -> symbol
    ;; prompts user for whether they want to draw a new card
    (define (prompt-card curr-points)
      (begin
        (printf "You have ~a points. Do you want another card? " curr-points)
        (read)))
     
    ;; draw-new-card : -> number
    ;; returns random number between 0 and 9
    (define (draw-new-card) (random 10))
    
    ;; points : list[num] -> number
    ;; sums the numbers in the list
    (define (points cards) (foldr + 0 cards))
    
    ;; check-if-win : list[num] -> string
    ;; print result of game based on number of points
    (define (check-if-win points) (cond [(> points 18) "You win"]
                                        [else "Sorry, you lose"]))
    
    ;; blackjack : list[number] -> string
    ;; plays blackjack and returns string with final results
    (define (blackjack cards)
      (cond [(> (points cards) 21) "Sorry, you lose"]
            [else (cond [(symbol=? (prompt-card (points cards)) 'y)
                         (blackjack (cons (draw-new-card) cards))]
                        [else (check-if-win (points cards))])]))
    
    (blackjack empty)
    

What to Turn In

Turn in a single file hwk7.ss (or hwk7.scm) containing all code and documentation for this assignment. Make sure that both students' names are in a comment at the top of the file.


Hints and Guidelines


Back to the Assignments page