DESIGN RECIPE TWO
Conditional Program
   With Symbolic Data

PRIOR KNOWLEDGE
DESIGN RECIPES:
  • Program Definition
  • Conditional Program with Intervals
  • DATA ANALYSIS
    CONTRACT, PURPOSE, HEADER - Name of program, Input / Output
    ;;Constant definition for the list of SUITS in a deck of cards
    ;;Included so player can match the correct symbol for the game
    (define SUITS (cons 'spade (cons 'heart (cons 'diamond (cons 'club empty)))))
    
    ;;Target: number -> symbol
    ;;Randomly return a SUIT from the list
    (define (Target key)
    
    EXAMPLES - Function calls (for EACH situation AND end points) with estimated answers. ;;(Target KEY1) = One of four SUITS picked randomly
    ;;(Target KEY2) = One of four SUITS picked randomly
    BODY
    SKELETON/TEMPLATE: For EACH situation there is ONE COND-CLAUSE
    (define (Target key)
      (cond
        [     ]
        [     ]
        [     ]
        [     ]))
    

    QUESTIONS: Numbered Conditions
    1. (= key 1)
    2. (= key 2)
    3. (= key 3)
    4. (= key 4)
    ANSWERS:
    1. 'spade
    2. 'heart
    3. 'diamond
    4. 'club
    TEST - Execute the program. Compare the computer answerS to your estimateS. Check logic.
    > SUITS
    (cons 'spade (cons 'heart (cons 'diamond (cons 'club empty))))
    > (Target KEY1)
    'diamond
    > (Target KEY2)
    'heart
    

    DATA ANALYSIS
    CONTRACT, PURPOSE, HEADER - Name of program, Input / Output
    ;;Constant definitions to randomly assign a target number
    (define KEY1 (+ 1(random 4)))
    (define KEY2 (+ 1(random 4)))
    
    ;;Constant definitions to randomly assign a SUIT
    (define T1 (Target KEY1))
    (define T2 (Target KEY2))
    
    ;;cardGame: symbol symbol --> string
    ;;Determine is if the suits guessed match the targets to win the game
    (define (cardGame g1 g2)
    
    EXAMPLES - Function calls (for EACH situation AND end points) with estimated answers.
    ;;Random Targets are:  'diamond 'heart
    ;;Both match and in correct place  (cardGame 'diamond 'heart) = 'You Win!!
    ;;One matches and in correct place (cardGame 'diamond 'spade) = 'OneSuitAllRight!
    ;;Both match and in wrong place    (cardGame 'heart 'diamond) = 'SuitsAllCorrect-WrongPlaces
    ;;One matches and in wrong place   (cardGame 'club 'diamond)  = 'OneCorrectSuit-WrongPlace
    ;;Nothing correct                  (cardGame 'club 'club)     = 'AllWrong
    
    BODY
    Skeleton / Template: For EACH situation there is ONE COND-CLAUSE
    (define (cardGame g1 g2)
      (cond
        [   ]
        [   ]
        [   ]
        [   ]
        [   ]))
    

    QUESTIONS: Numbered Conditions
    1. (and (eq? g1 T1) (eq? g2 T2))
    2. (or (eq? g1 T1) (eq? g2 T2))
    3. (and (eq? g1 T2) (eq? g2 T1))
    4. (or (eq? g1 T2) (eq? g2 T1))
    5. (not(and (eq? g1 T1)(eq? g2 T2)))
    ANSWERS:
    1. 'YouWin!!
    2. 'OneSuitAllRight!
    3. 'SuitsAllCorrect-WrongPlaces
    4. 'OneCorrectSuit-WrongPlace
    5. 'AllWrong
    SIMPLIFY Since each cond-line is evaluated sequentially, last statement can be else.
    TEST - Execute the program. Compare the computer answerS to your estimateS. Check logic.

    > (cardGame 'heart 'diamond)
    'AllWrong
    > (cardGame 'club 'spade)
    'YouWin!!
    

    CARD GAME PROGRAM

    ;;Constant definition for the list of SUITS in a deck of cards
    ;;Included so player can match the correct symbol for the game
    (define SUITS (cons 'spade (cons 'heart (cons 'diamond (cons 'club empty)))))
    
    ;;Constant definitions to randomly assign a target number
    (define KEY1 (+ 1(random 4)))
    (define KEY2 (+ 1(random 4)))
    
    ;;Target: number -> symbol
    ;;Randomly return a SUIT from the list
    (define (Target key)
      (cond
        [(= key 1) 'spade]
        [(= key 2) 'heart]
        [(= key 3) 'diamond]
        [else 'club]))
    
    ;;(Target KEY1) = One of four SUITS
    ;;(Target KEY2) = One of four SUITS
    
    ;;Constant definitions to randomly assign a SUIT
    (define T1 (Target KEY1))
    (define T2 (Target KEY2))
    
    ;;cardGame: symbol symbol --> string
    ;;Determine if the suits guessed match the targets to win the game
    (define (cardGame g1 g2)
      (cond
        [(and (eq? g1 T1)(eq? g2 T2)) 'YouWin!!]
        [(or (eq? g1 T1)(eq? g2 T2)) 'OneSuitAllRight!]
        [(and (eq? g1 T2) (eq? g2 T1)) 'SuitsAllCorrect-WrongPlaces]
        [(or (eq? g1 T2) (eq? g2 T1)) 'OneCorrectSuit-WrongPlace]
        [(not(and (eq? g1 T1)(eq? g2 T2))) 'AllWrong]))
    
    ;;Random Targets are:  'diamond 'heart
    ;;Both match and are in correct location (cardGame 'diamond 'heart) = 'You Win!!
    ;;One matches and is in correct location (cardGame 'diamond 'spade) = 'OneSuitAllRight!
    ;;Both match and are in wrong position   (cardGame 'heart 'diamond) = 'SuitsAllCorrect-WrongPlaces
    ;;One matches and is in wrong position   (cardGame 'club 'diamond) = 'OneCorrectSuit-WrongPlace
    ;;Neither are in correct position        (cardGame 'club 'club) = 'AllWrong
    
    (define ACE 1)
    (define JACK 11)
    (define QUEEN 12)
    (define KING 13)
    (define KEYVALUE (+ 1 (random 13)))
    
    ;;cardValue: number -> symbol
    ;;Determine if card value is correct
    (define (cardValue value)
      (cond
        ((< value KEYVALUE) 'CardValueTooLow)
        ((> value KEYVALUE) 'CardValueTooHigh)
        ((= value KEYVALUE) 'CardValueCorrect)))
    
    ;;Random target is JACK (value 11)
    ;;(cardValue 7) = 'CardValueTooLow
    ;;(cardValue KING) = 'CardValueTooHigh
    ;;(cardValue JACK) = 'CardValueCorrect
    
    The CardValue function can be combined with the Suit function to create your own games. Using those helper functions, try using the design recipe for new programs. The Card Value template below is like the number guessing game.

    DATA ANALYSIS
    CONTRACT, PURPOSE, HEADER - Name of program, Input / Output
    (define ACE 1)
    (define JACK 11)
    (define QUEEN 12)
    (define KING 13)
    (define KEYVALUE (+ 1 (random 13)))
    
    ;;cardValue: number -> symbol
    ;;Determine if card value
    (define (cardValue value)
    
    EXAMPLES - Function calls (for EACH situation AND end points) with estimated answers.
    ;;Random target is JACK (value 11)
    ;;(cardValue 7) = 'CardValueTooLow
    ;;(cardValue KING) = 'CardValueTooHigh
    ;;(cardValue JACK) = 'CardValueCorrect
    
    BODY
    SKELTON/TEMPLATE For EACH situation there is ONE COND-CLAUSE
    (define (cardValue value)
      (cond
        [   ]
        [   ]
        [   ]))
    

    QUESTIONS: Boolean Expressions
    1. (< value KEYVALUE)
    2. (> value KEYVALUE)
    3. (= value KEYVALUE)
    ANSWERS:
    1. 'CardValueTooLow
    2. 'CardValueTooHigh
    3. 'CardValueCorrect
    TEST - Execute the program. Compare the computer answerS to your estimateS. Check logic.
    > (cardValue QUEEN)
    'CardValueTooHigh
    > (cardValue 7)
    'CardValueTooLow
    > (cardValue 10)
    'CardValueTooLow
    > (cardValue JACK)
    'CardValueCorrect
    


    Home
    E-mail
    Lessons