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