Fork of https://github.com/google/lisp-koans so that I could go through them. THIS CONTAINS ANSWERS.

dice-project.lsp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ;; Copyright 2013 Google Inc.
  2. ;;
  3. ;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;; you may not use this file except in compliance with the License.
  5. ;; You may obtain a copy of the License at
  6. ;;
  7. ;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;
  9. ;; Unless required by applicable law or agreed to in writing, software
  10. ;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;; See the License for the specific language governing permissions and
  13. ;; limitations under the License.
  14. ; based on about_dice_project.rb
  15. ;; In this project we are going to build a CLOS class representing
  16. ;; a simple set of dice. There are only two operations on the dice,
  17. ;; reading the values, and re-rolling.
  18. ;; YOU WRITE THIS PART:
  19. (defclass dice-set ()
  20. () ;; WRITE DICE-SET CLASS BODY HERE
  21. )
  22. (defmethod get-values ((object dice-set))
  23. ;; WRITE GET-VALUES METHOD DEFINITION HERE
  24. )
  25. (defmethod roll (how-many (object dice-set))
  26. ;; WRITE ROLL METHOD DEFINITION HERE
  27. )
  28. (define-test test-create-dice-set
  29. ;; tests making an instance of the dice-set
  30. (let ((dice (make-instance 'dice-set)))
  31. (assert-true dice)))
  32. (define-test test-rolling-the-dice-returns-a-set-of-integers-between-1-and-6
  33. ;; tests rolling the dice
  34. (let ((dice (make-instance 'dice-set)))
  35. (roll 5 dice)
  36. (assert-true (typep (get-values dice) 'list))
  37. (assert-equal 5 (length (get-values dice)))
  38. (dolist (x (get-values dice))
  39. (assert-true (and (>= x 1)
  40. (<= x 6)
  41. (typep x 'integer))))))
  42. (define-test test-dice-values-do-not-change-unless-explicitly-rolled
  43. ;; tests that dice don't change just by looking at them
  44. (let ((dice (make-instance 'dice-set)))
  45. (roll 100 dice)
  46. (let ((first-time (get-values dice))
  47. (second-time (get-values dice)))
  48. (assert-equal first-time second-time))))
  49. (define-test test-dice-values-should-change-between-rolls
  50. ;; tests that rolling the dice DOES change the values.
  51. (let ((dice (make-instance 'dice-set))
  52. (first-time nil)
  53. (second-time nil))
  54. (roll 100 dice)
  55. (setf first-time (get-values dice))
  56. (roll 100 dice)
  57. (setf second-time (get-values dice))
  58. (assert-false (equal first-time second-time))))
  59. (define-test test-you-can-roll-different-numbers-of-dice
  60. ;; tests count parameter of how many dice to roll
  61. (let ((dice (make-instance 'dice-set)))
  62. (assert-equal 5 (length (roll 5 dice)))
  63. (assert-equal 100 (length (roll 100 dice)))
  64. (assert-equal 1 (length (roll 1 dice)))))