3 ;;; The main purpose of this program was to implement a Craps game, using a language that we have just
4 ;;; learned. Also, it was written in a functional style with almost no reliance on the assignment
5 ;;; operation. Only one local variable called THROW was used.
8 ;;; ====================================================================================================== ;;;
9 ;;; ======================================= CRAPS SIMULATION ============================================= ;;;
10 ;;; ====================================================================================================== ;;;
13 ;;; ** This function takes no parameters as input and returns a random number between 1 and 6. **
16 (+ (RANDOM 6) 1) ;;; get a random number between 0 and 5 and then add 1
19 ;;; ====================================================================================================== ;;;
21 ;;; ** This function takes no parameters as input and returns a LIST with two numbers between 1 and 6. **
26 (LIST (THROW-DIE) (THROW-DIE)) ;;; create a list with two random numbers
30 ;;; ====================================================================================================== ;;;
32 ;;; ** This function takes two numbers as parameters for input and returns T or Nil. T is returned if both
33 ;;; numbers are equal to 6. Nil is returned otherwise. **
35 (DEFUN BOXCARS-P (A B)
42 ;;; ====================================================================================================== ;;;
44 ;;; ** This function takes two numbers as parameters for input and returns T or Nil. T is returned if both
45 ;;; numbers are equal to 1. Nil is returned otherwise. **
47 (DEFUN SNAKE-EYES-P (A B)
54 ;;; ====================================================================================================== ;;;
56 ;;; ** This function takes two numbers as parameters for input and returns T or Nil. T is returned if the
57 ;;; sum of both numbers is equal to a 7 or 11. Nil is returned otherwise. **
59 (DEFUN INSTANT-WIN-P (A B)
60 (OR (EQUAL '7 (+ A B))
66 ;;; ====================================================================================================== ;;;
68 ;;; ** This function takes two numbers as parameters for input and returns T or Nil. T is returned if the
69 ;;; sum of both numbers is equal to a 2, 3 or 12. Nil is returned otherwise. **
71 (DEFUN INSTANT-LOSS-P (A B)
72 (OR (EQUAL '2 (+ A B))
79 ;;; ====================================================================================================== ;;;
81 ;;; ** This function takes two numbers as parameters for input and returns a string. If function BOXCARS_P
82 ;;; returns T, then the returned string equals BOXCARS. If function SNAKE_EYES_P returns T, then the
83 ;;; returned string equals SNAKE_EYES. The string contains Nil otherwise. **
85 (DEFUN SAY-THROW (A B)
86 (COND ((BOXCARS-P A B) 'BOXCARS) ;;; make use of function BOXCARS_P
87 ((SNAKE-EYES-P A B) 'SNAKE-EYES) ;;; make use of function SNAKE_EYES_P
92 ;;; ====================================================================================================== ;;;
94 ;;; ** This is the main function used to simulate the game of craps. Variable THROW contains a LIST of two
95 ;;; numbers between 1 and 6. The numbers located in THROW, are used as parameters for the other functions.
96 ;;; The several pieces used for output are listed together and then the LIST is returned from this
101 (LET THROW (THROW-DICE)) ;;; get initial roll of the dice
103 ;;; if roll is a win, then LIST the appropriate output
105 (COND ((INSTANT-WIN-P (FIRST THROW) (SECOND THROW))
106 (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- (+ (FIRST THROW) (SECOND THROW)) '-- 'YOU 'WIN))
108 ;;; if roll is a loss, then check for BOXCARS or SNAKE-EYES
110 ((INSTANT-LOSS-P (FIRST THROW) (SECOND THROW))
112 (IF (EQUAL 'NIL (SAY-THROW (FIRST THROW) (SECOND THROW))) ;;; if Nil then LIST appropriate output
114 (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- (+ (FIRST THROW) (SECOND THROW)) '-- 'YOU 'LOSE)
116 ;;; else include the BOXCARS or SNAKE-EYES string in the output
118 (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- (SAY-THROW (FIRST THROW) (SECOND THROW))
121 ;;; if roll is not instant win or loss then output sum of dice
123 (T (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- 'YOUR 'POINT 'IS (+ (FIRST THROW)
133 ;;; ======================================== END OF PROGRAM CRAPS ======================================== ;;;