1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
;;; CrapsSim.lsp
"""
The main purpose of this program was to implement a Craps game, using a language that we have just
learned. Also, it was written in a functional style with almost no reliance on the assignment
operation. Only one local variable called THROW was used.
"""
;;; ====================================================================================================== ;;;
;;; ======================================= CRAPS SIMULATION ============================================= ;;;
;;; ====================================================================================================== ;;;
;;; ** This function takes no parameters as input and returns a random number between 1 and 6. **
(DEFUN THROW-DIE ()
(+ (RANDOM 6) 1) ;;; get a random number between 0 and 5 and then add 1
)
;;; ====================================================================================================== ;;;
;;; ** This function takes no parameters as input and returns a LIST with two numbers between 1 and 6. **
(DEFUN THROW-DICE ()
(LIST (THROW-DIE) (THROW-DIE)) ;;; create a list with two random numbers
)
;;; ====================================================================================================== ;;;
;;; ** This function takes two numbers as parameters for input and returns T or Nil. T is returned if both
;;; numbers are equal to 6. Nil is returned otherwise. **
(DEFUN BOXCARS-P (A B)
(AND (EQUAL '6 A)
(EQUAL '6 B)
)
)
;;; ====================================================================================================== ;;;
;;; ** This function takes two numbers as parameters for input and returns T or Nil. T is returned if both
;;; numbers are equal to 1. Nil is returned otherwise. **
(DEFUN SNAKE-EYES-P (A B)
(AND (EQUAL '1 A)
(EQUAL '1 B)
)
)
;;; ====================================================================================================== ;;;
;;; ** This function takes two numbers as parameters for input and returns T or Nil. T is returned if the
;;; sum of both numbers is equal to a 7 or 11. Nil is returned otherwise. **
(DEFUN INSTANT-WIN-P (A B)
(OR (EQUAL '7 (+ A B))
(EQUAL '11 (+ A B))
)
)
;;; ====================================================================================================== ;;;
;;; ** This function takes two numbers as parameters for input and returns T or Nil. T is returned if the
;;; sum of both numbers is equal to a 2, 3 or 12. Nil is returned otherwise. **
(DEFUN INSTANT-LOSS-P (A B)
(OR (EQUAL '2 (+ A B))
(EQUAL '3 (+ A B))
(EQUAL '12 (+ A B))
)
)
;;; ====================================================================================================== ;;;
;;; ** This function takes two numbers as parameters for input and returns a string. If function BOXCARS_P
;;; returns T, then the returned string equals BOXCARS. If function SNAKE_EYES_P returns T, then the
;;; returned string equals SNAKE_EYES. The string contains Nil otherwise. **
(DEFUN SAY-THROW (A B)
(COND ((BOXCARS-P A B) 'BOXCARS) ;;; make use of function BOXCARS_P
((SNAKE-EYES-P A B) 'SNAKE-EYES) ;;; make use of function SNAKE_EYES_P
)
)
;;; ====================================================================================================== ;;;
;;; ** This is the main function used to simulate the game of craps. Variable THROW contains a LIST of two
;;; numbers between 1 and 6. The numbers located in THROW, are used as parameters for the other functions.
;;; The several pieces used for output are listed together and then the LIST is returned from this
;;; function.
(DEFUN CRAPS ()
(LET THROW (THROW-DICE)) ;;; get initial roll of the dice
;;; if roll is a win, then LIST the appropriate output
(COND ((INSTANT-WIN-P (FIRST THROW) (SECOND THROW))
(LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- (+ (FIRST THROW) (SECOND THROW)) '-- 'YOU 'WIN))
;;; if roll is a loss, then check for BOXCARS or SNAKE-EYES
((INSTANT-LOSS-P (FIRST THROW) (SECOND THROW))
(IF (EQUAL 'NIL (SAY-THROW (FIRST THROW) (SECOND THROW))) ;;; if Nil then LIST appropriate output
(LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- (+ (FIRST THROW) (SECOND THROW)) '-- 'YOU 'LOSE)
;;; else include the BOXCARS or SNAKE-EYES string in the output
(LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- (SAY-THROW (FIRST THROW) (SECOND THROW))
'-- 'YOU 'LOSE)))
;;; if roll is not instant win or loss then output sum of dice
(T (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- 'YOUR 'POINT 'IS (+ (FIRST THROW)
(SECOND THROW))))
) ;;; end COND
) ;;; end LET
)
;;; ======================================== END OF PROGRAM CRAPS ======================================== ;;;
|