Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;Bridge Score Card
- ;;;;;;;;;;;;;;;;;;;;;;
- ;;Simon Murdoch
- ;;September 27, 2016
- ;;;;;;;;;;;;;;;;;;;;;;
- ;;a)
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;Purpose:
- ;;Helper function that consumes a number of "tricks", and a suit
- ;; and outputs the score recieved in a game of bridge
- ;; before applying "double" or "redouble"
- ;;Contract:
- ;;contract-points: Num Sym -> Num
- ;; Num is a value 1-7, Sym are one of 'heart, diamond, 'spade, 'club, 'no-trump
- ;;Examples:
- (check-expect (before-double 3 'no-trump) 100)
- (check-expect (before-double 1 'heart) 30)
- ;;Body:
- (define (before-double tricks suit)
- (cond
- [(symbol=? 'no-trump suit)
- (cond
- [(< 1 tricks) (+ (* (- tricks 1) 30) 40)]
- [else 40])]
- [(or (symbol=? 'heart suit) (symbol=? 'spade suit)) (* 30 tricks)]
- [else (* 20 tricks)]))
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;Purpose:
- ;;Consumes a number of "tricks", a suit, and 2 booleans for doubled/redoubled
- ;; and outputs the score recieved in a game of bridge. Uses above helper
- ;;Contract:
- ;;contract-points: Num Sym Bool Bool -> Num
- ;; Num is a value 1-7, Sym are one of 'heart, diamond, 'spade, 'club, 'no-trump
- ;;Examples:
- (check-expect (contract-points 3 'no-trump true true) 400)
- (check-expect (contract-points 1 'heart true false) 60)
- ;;Body:
- (define (contract-points tricks suit double? redouble?)
- (cond
- [(not (or double? redouble?)) (before-double tricks suit)]
- [(and double? redouble?) (* 4 (before-double tricks suit))]
- [else (* 2 (before-double tricks suit))]))
- ;;Tests:
- (check-expect (contract-points 1 'no-trump true true) 160)
- (check-expect (contract-points -5 'heart true false) -300)
- (check-expect (contract-points 4 'club false true) 160)
- (check-expect (contract-points 10 'diamond false false) 200)
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;b)
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;Purpose:
- ;;Consumes a number of "undertricks" and 3 booleans for vunerable/doubled/redoubled
- ;; and outputs the score penalty recieved in a game of bridge.
- ;;Contract:
- ;;contract-points: Num Bool Bool Bool -> Num
- ;; Num is a value 1-7, Bool represent states of vunerable/double/redouble
- ;; if first Bool is true, Vunerable.
- ;;Examples:
- (check-expect (penalty-points 3 true true true) -1600)
- (check-expect (penalty-points 5 false true false) -1100)
- ;;Body:
- (define (penalty-points undertricks vunerable? double? redouble?)
- (cond
- [vunerable?
- (cond
- [(not (or double? redouble?)) (* -100 undertricks)]
- [(and double? (not redouble?))
- (cond
- [(< undertricks 2) -200]
- [else (+ (* (- undertricks 1) -300) -200)])]
- [(and double? redouble?)
- (cond
- [(< undertricks 2) -400]
- [else (+ (* (- undertricks 1) -600) -400)])]
- [else "Invalid input"])]
- [(not vunerable?)
- (cond
- [(not (or double? redouble?)) (* -50 undertricks)]
- [(and double? (not redouble?))
- (cond
- [(< undertricks 2) -100]
- [(< undertricks 4) (+ (* (- undertricks 1) -200) -100)]
- [(>= undertricks 4) (+ (* (- undertricks 3) -300) -500)])]
- [(and double? redouble?)
- (cond
- [(< undertricks 2) -200]
- [(< undertricks 4) (+ (* (- undertricks 1) -400) -200)]
- [(>= undertricks 4) (+ (* (- undertricks 3) -600) -1000)])]
- [else "Invalid input"])]))
- ;;Tests:
- (check-expect (penalty-points -5 true true true) -400)
- (check-expect (penalty-points 10 true true false) -2900)
- (check-expect (penalty-points 1 true false false) -100)
- (check-expect (penalty-points 0 true false true) "Invalid input")
- (check-expect (penalty-points 4 false true true) -1600)
- (check-expect (penalty-points 7 false true false) -1700)
- (check-expect (penalty-points 0.1 false false true) "Invalid input")
- (check-expect (penalty-points 2/4 false false false) -25)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement