1 lisp comment ;;; CrapsSim.lsp
4 lisp comment The main purpose of this program was to implement a Craps game, using a language that we have just
5 lisp comment learned. Also, it was written in a functional style with almost no reliance on the assignment
6 lisp comment operation. Only one local variable called THROW was used.
10 lisp comment ;;; ====================================================================================================== ;;;
11 lisp comment ;;; ======================================= CRAPS SIMULATION ============================================= ;;;
12 lisp comment ;;; ====================================================================================================== ;;;
15 lisp comment ;;; ** This function takes no parameters as input and returns a random number between 1 and 6. **
17 lisp code (DEFUN THROW-DIE ()
18 lisp code (+ (RANDOM 6) 1) ;;; get a random number between 0 and 5 and then add 1
21 lisp comment ;;; ====================================================================================================== ;;;
23 lisp comment ;;; ** This function takes no parameters as input and returns a LIST with two numbers between 1 and 6. **
26 lisp code (DEFUN THROW-DICE ()
28 lisp code (LIST (THROW-DIE) (THROW-DIE)) ;;; create a list with two random numbers
32 lisp comment ;;; ====================================================================================================== ;;;
34 lisp comment ;;; ** This function takes two numbers as parameters for input and returns T or Nil. T is returned if both
35 lisp comment ;;; numbers are equal to 6. Nil is returned otherwise. **
37 lisp code (DEFUN BOXCARS-P (A B)
38 lisp code (AND (EQUAL '6 A)
39 lisp code (EQUAL '6 B)
44 lisp comment ;;; ====================================================================================================== ;;;
46 lisp comment ;;; ** This function takes two numbers as parameters for input and returns T or Nil. T is returned if both
47 lisp comment ;;; numbers are equal to 1. Nil is returned otherwise. **
49 lisp code (DEFUN SNAKE-EYES-P (A B)
50 lisp code (AND (EQUAL '1 A)
51 lisp code (EQUAL '1 B)
56 lisp comment ;;; ====================================================================================================== ;;;
58 lisp comment ;;; ** This function takes two numbers as parameters for input and returns T or Nil. T is returned if the
59 lisp comment ;;; sum of both numbers is equal to a 7 or 11. Nil is returned otherwise. **
61 lisp code (DEFUN INSTANT-WIN-P (A B)
62 lisp code (OR (EQUAL '7 (+ A B))
63 lisp code (EQUAL '11 (+ A B))
68 lisp comment ;;; ====================================================================================================== ;;;
70 lisp comment ;;; ** This function takes two numbers as parameters for input and returns T or Nil. T is returned if the
71 lisp comment ;;; sum of both numbers is equal to a 2, 3 or 12. Nil is returned otherwise. **
73 lisp code (DEFUN INSTANT-LOSS-P (A B)
74 lisp code (OR (EQUAL '2 (+ A B))
75 lisp code (EQUAL '3 (+ A B))
76 lisp code (EQUAL '12 (+ A B))
81 lisp comment ;;; ====================================================================================================== ;;;
83 lisp comment ;;; ** This function takes two numbers as parameters for input and returns a string. If function BOXCARS_P
84 lisp comment ;;; returns T, then the returned string equals BOXCARS. If function SNAKE_EYES_P returns T, then the
85 lisp comment ;;; returned string equals SNAKE_EYES. The string contains Nil otherwise. **
87 lisp code (DEFUN SAY-THROW (A B)
88 lisp code (COND ((BOXCARS-P A B) 'BOXCARS) ;;; make use of function BOXCARS_P
89 lisp code ((SNAKE-EYES-P A B) 'SNAKE-EYES) ;;; make use of function SNAKE_EYES_P
94 lisp comment ;;; ====================================================================================================== ;;;
96 lisp comment ;;; ** This is the main function used to simulate the game of craps. Variable THROW contains a LIST of two
97 lisp comment ;;; numbers between 1 and 6. The numbers located in THROW, are used as parameters for the other functions.
98 lisp comment ;;; The several pieces used for output are listed together and then the LIST is returned from this
99 lisp comment ;;; function.
102 lisp code (DEFUN CRAPS ()
103 lisp code (LET THROW (THROW-DICE)) ;;; get initial roll of the dice
105 lisp comment ;;; if roll is a win, then LIST the appropriate output
107 lisp code (COND ((INSTANT-WIN-P (FIRST THROW) (SECOND THROW))
108 lisp code (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- (+ (FIRST THROW) (SECOND THROW)) '-- 'YOU 'WIN))
110 lisp comment ;;; if roll is a loss, then check for BOXCARS or SNAKE-EYES
112 lisp code ((INSTANT-LOSS-P (FIRST THROW) (SECOND THROW))
114 lisp code (IF (EQUAL 'NIL (SAY-THROW (FIRST THROW) (SECOND THROW))) ;;; if Nil then LIST appropriate output
116 lisp code (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- (+ (FIRST THROW) (SECOND THROW)) '-- 'YOU 'LOSE)
118 lisp comment ;;; else include the BOXCARS or SNAKE-EYES string in the output
120 lisp code (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- (SAY-THROW (FIRST THROW) (SECOND THROW))
121 lisp code '-- 'YOU 'LOSE)))
123 lisp comment ;;; if roll is not instant win or loss then output sum of dice
125 lisp code (T (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- 'YOUR 'POINT 'IS (+ (FIRST THROW)
126 lisp code (SECOND THROW))))
127 lisp code ) ;;; end COND
129 lisp code ) ;;; end LET
135 lisp comment ;;; ======================================== END OF PROGRAM CRAPS ======================================== ;;;