Initial Revision
[ohcount] / test / src_dir / lsp1.lsp
1 ;;; CrapsSim.lsp
2
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.
6
7
8 ;;; ====================================================================================================== ;;;
9 ;;; ======================================= CRAPS SIMULATION ============================================= ;;;
10 ;;; ====================================================================================================== ;;;
11
12
13 ;;; ** This function takes no parameters as input and returns a random number between 1 and 6. **
14
15 (DEFUN THROW-DIE ()
16                          (+ (RANDOM 6) 1)          ;;; get a random number between 0 and 5 and then add 1
17                          )
18
19 ;;; ====================================================================================================== ;;;
20
21 ;;; ** This function takes no parameters as input and returns a LIST with two numbers between 1 and 6. **
22
23
24 (DEFUN THROW-DICE ()       
25
26                          (LIST (THROW-DIE) (THROW-DIE))              ;;; create a list with two random numbers
27
28                          )
29
30 ;;; ====================================================================================================== ;;;
31
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. **
34
35 (DEFUN BOXCARS-P (A B)
36                          (AND (EQUAL '6 A)                      
37                                                 (EQUAL '6 B)
38                                                 )
39
40                          )
41
42 ;;; ====================================================================================================== ;;;
43
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. **
46
47 (DEFUN SNAKE-EYES-P (A B)
48                          (AND (EQUAL '1 A)                       
49                                                 (EQUAL '1 B)
50                                                 )
51
52                          )
53
54 ;;; ====================================================================================================== ;;;
55
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. **
58
59 (DEFUN INSTANT-WIN-P (A B)
60                          (OR (EQUAL '7 (+ A B))                  
61                                          (EQUAL '11 (+ A B))
62                                          )
63
64                          )
65
66 ;;; ====================================================================================================== ;;;
67
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. **
70
71 (DEFUN INSTANT-LOSS-P (A B)
72                          (OR (EQUAL '2 (+ A B))
73                                          (EQUAL '3 (+ A B))
74                                          (EQUAL '12 (+ A B))
75                                          )
76
77                          )
78
79 ;;; ====================================================================================================== ;;;
80
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. **
84
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
88
89                                                  )
90                          )
91
92 ;;; ====================================================================================================== ;;;
93
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 
97 ;;;    function.
98
99
100 (DEFUN CRAPS ()
101                          (LET THROW (THROW-DICE))                        ;;; get initial roll of the dice
102
103                          ;;; if roll is a win, then LIST the appropriate output
104
105                          (COND ((INSTANT-WIN-P (FIRST THROW) (SECOND THROW)) 
106                                                         (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- (+ (FIRST THROW) (SECOND THROW)) '-- 'YOU 'WIN))
107
108                                                  ;;; if roll is a loss, then check for BOXCARS or SNAKE-EYES
109
110                                                  ((INSTANT-LOSS-P (FIRST THROW) (SECOND THROW))
111
112                                                         (IF (EQUAL 'NIL (SAY-THROW (FIRST THROW) (SECOND THROW)))   ;;; if Nil then LIST appropriate output
113
114                                                                         (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- (+ (FIRST THROW) (SECOND THROW)) '-- 'YOU 'LOSE)
115
116                                                                         ;;; else include the BOXCARS or SNAKE-EYES string in the output
117
118                                                                         (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- (SAY-THROW (FIRST THROW) (SECOND THROW)) 
119                                                                                                 '-- 'YOU 'LOSE)))
120
121                                                  ;;; if roll is not instant win or loss then output sum of dice
122
123                                                  (T (LIST 'THROW (FIRST THROW) 'AND (SECOND THROW) '-- 'YOUR 'POINT 'IS (+ (FIRST THROW) 
124                                                                                                                                                                                                                                                                                                                                                          (SECOND THROW))))
125                                                  )        ;;; end COND
126
127                          )           ;;; end LET
128
129
130 )
131
132
133 ;;; ======================================== END OF PROGRAM CRAPS ======================================== ;;;