Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. ;;Bridge Score Card
  2.  
  3. ;;;;;;;;;;;;;;;;;;;;;;
  4. ;;Simon Murdoch
  5. ;;September 27, 2016
  6. ;;;;;;;;;;;;;;;;;;;;;;
  7. ;;a)
  8. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  9. ;;Purpose:
  10. ;;Helper function that consumes a number of "tricks", and a suit
  11. ;; and outputs the score recieved in a game of bridge
  12. ;; before applying "double" or "redouble"
  13.  
  14. ;;Contract:
  15. ;;contract-points: Num Sym -> Num
  16. ;; Num is a value 1-7, Sym are one of 'heart, diamond, 'spade, 'club, 'no-trump
  17.  
  18. ;;Examples:
  19. (check-expect (before-double 3 'no-trump) 100)
  20. (check-expect (before-double 1 'heart) 30)
  21.  
  22. ;;Body:
  23. (define (before-double tricks suit)
  24. (cond
  25. [(symbol=? 'no-trump suit)
  26. (cond
  27. [(< 1 tricks) (+ (* (- tricks 1) 30) 40)]
  28. [else 40])]
  29. [(or (symbol=? 'heart suit) (symbol=? 'spade suit)) (* 30 tricks)]
  30. [else (* 20 tricks)]))
  31.  
  32. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  33.  
  34. ;;Purpose:
  35. ;;Consumes a number of "tricks", a suit, and 2 booleans for doubled/redoubled
  36. ;; and outputs the score recieved in a game of bridge. Uses above helper
  37.  
  38. ;;Contract:
  39. ;;contract-points: Num Sym Bool Bool -> Num
  40. ;; Num is a value 1-7, Sym are one of 'heart, diamond, 'spade, 'club, 'no-trump
  41.  
  42. ;;Examples:
  43. (check-expect (contract-points 3 'no-trump true true) 400)
  44. (check-expect (contract-points 1 'heart true false) 60)
  45.  
  46. ;;Body:
  47. (define (contract-points tricks suit double? redouble?)
  48. (cond
  49. [(not (or double? redouble?)) (before-double tricks suit)]
  50. [(and double? redouble?) (* 4 (before-double tricks suit))]
  51. [else (* 2 (before-double tricks suit))]))
  52.  
  53.  
  54. ;;Tests:
  55. (check-expect (contract-points 1 'no-trump true true) 160)
  56. (check-expect (contract-points -5 'heart true false) -300)
  57. (check-expect (contract-points 4 'club false true) 160)
  58. (check-expect (contract-points 10 'diamond false false) 200)
  59.  
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. ;;b)
  62. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  63.  
  64. ;;Purpose:
  65. ;;Consumes a number of "undertricks" and 3 booleans for vunerable/doubled/redoubled
  66. ;; and outputs the score penalty recieved in a game of bridge.
  67.  
  68. ;;Contract:
  69. ;;contract-points: Num Bool Bool Bool -> Num
  70. ;; Num is a value 1-7, Bool represent states of vunerable/double/redouble
  71. ;; if first Bool is true, Vunerable.
  72.  
  73. ;;Examples:
  74. (check-expect (penalty-points 3 true true true) -1600)
  75. (check-expect (penalty-points 5 false true false) -1100)
  76.  
  77. ;;Body:
  78. (define (penalty-points undertricks vunerable? double? redouble?)
  79. (cond
  80. [vunerable?
  81. (cond
  82. [(not (or double? redouble?)) (* -100 undertricks)]
  83. [(and double? (not redouble?))
  84. (cond
  85. [(< undertricks 2) -200]
  86. [else (+ (* (- undertricks 1) -300) -200)])]
  87. [(and double? redouble?)
  88. (cond
  89. [(< undertricks 2) -400]
  90. [else (+ (* (- undertricks 1) -600) -400)])]
  91. [else "Invalid input"])]
  92. [(not vunerable?)
  93. (cond
  94. [(not (or double? redouble?)) (* -50 undertricks)]
  95. [(and double? (not redouble?))
  96. (cond
  97. [(< undertricks 2) -100]
  98. [(< undertricks 4) (+ (* (- undertricks 1) -200) -100)]
  99. [(>= undertricks 4) (+ (* (- undertricks 3) -300) -500)])]
  100. [(and double? redouble?)
  101. (cond
  102. [(< undertricks 2) -200]
  103. [(< undertricks 4) (+ (* (- undertricks 1) -400) -200)]
  104. [(>= undertricks 4) (+ (* (- undertricks 3) -600) -1000)])]
  105. [else "Invalid input"])]))
  106.  
  107.  
  108. ;;Tests:
  109. (check-expect (penalty-points -5 true true true) -400)
  110. (check-expect (penalty-points 10 true true false) -2900)
  111. (check-expect (penalty-points 1 true false false) -100)
  112. (check-expect (penalty-points 0 true false true) "Invalid input")
  113. (check-expect (penalty-points 4 false true true) -1600)
  114. (check-expect (penalty-points 7 false true false) -1700)
  115. (check-expect (penalty-points 0.1 false false true) "Invalid input")
  116. (check-expect (penalty-points 2/4 false false false) -25)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement