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