# publiczot/Leisure

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Older
100644 304 lines (277 sloc) 16.205 kb
  679d7e8d » bill  2011-05-30 refactored evaluator.html into lc.js for embedding 1 2 25 26 47 48 148  f6167440 » bill  2011-06-07 expanded grid from 9x9 to 19x19 149  a188539f » bill  2011-06-06 allow lambda characters in programs 150  2227de09 » bill  2011-06-07 cleaned up page (code on right) 151
 679d7e8d » bill  2011-05-30 refactored evaluator.html into lc.js for embedding 152  f6167440 » bill  2011-06-07 expanded grid from 9x9 to 19x19 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172  2227de09 » bill  2011-06-07 cleaned up page (code on right) 173

 a19463bf » bill  2011-06-07 doc 174 Here's my simplistic version of "space invaders". It's not as deluxe as the real one, but the point is to show how you might make a video game with Lambda Calculus.

 2e624eba » bill  2011-06-07 doc 175  a19463bf » bill  2011-06-07 doc 176 The only thing that works right now is the left and right arrow keys :).

 2e624eba » bill  2011-06-07 doc 177 178 Below is all of the Lambda Calulus code. JavaScript provides the key event (or an empty key event) and the game state to a Lambda Calculus function to calculate the next state of the game. Then, it uses other Lambda Calculus functions to inspect the current state so it can display it.

 4e201c9d » bill  2011-06-07 docs 179 180 To advance the state, JavaScript calls (state eventFunc). State starts out equal to (start). The eventFuncs are: (moveLeft), (moveRight), (stay), and (fire).

181 182 The screen is a grid of values, with a row of values underneath. A row has 19 values in it, so it's a function which applies its argument to its 19 values. The grid is really a "row of rows". You can use (left) and (right) to rotate the values and (get1) to get the first value. The base values are numbers from 0 to 4, represented with 5 argument functions that return the Nth argument, depending on which number the function represents.

183  2227de09 » bill  2011-06-07 cleaned up page (code on right) 184
186 187
Code
 a188539f » bill  2011-06-06 allow lambda characters in programs 188  2227de09 » bill  2011-06-07 cleaned up page (code on right) 214
                   a188539f                   »                   bill                                                   2011-06-06                                    allow lambda characters in programs                                  215  # Booleans
                   2227de09                   »                   bill                                                   2011-06-07                                    cleaned up page (code on right)                                      216  t = \x y . x
217  f = \x y . y
218  not = \a . a f t
                   679d7e8d                   »                   bill                                                   2011-05-30                                    refactored evaluator.html into lc.js for embedding                   219
220  # 10 x 9 grid (a 9x9 grid for missiles and aliens and a 9x1 row for the ship)
221  # a grid row applies a function of 9 arguments to its values
                   221ceb18                   »                   bill                                                   2011-06-01                                    cleaned up LC code                                                   222  # a grid cell represents a value from 0 to 4 (empty, ship, alien1, alien2, missile)
                   679d7e8d                   »                   bill                                                   2011-05-30                                    refactored evaluator.html into lc.js for embedding                   223  #   higher values 'beat' lower values
                   221ceb18                   »                   bill                                                   2011-06-01                                    cleaned up LC code                                                   224  #   cells are functions of 5 arguments and they indicate their value by returning the nth argument (like booleans)
225
226  # cell functions
                   96c4975c                   »                   bill                                                   2011-06-08                                    Throw error when someone tries to use a free variable as a function  227  e      = \1 2 3 4 5 . 1
228  s      = \1 2 3 4 5 . 2
229  a1     = \1 2 3 4 5 . 3
230  a2     = \1 2 3 4 5 . 4
231  m      = \1 2 3 4 5 . 5
232  cell   = \n f . f n
233  isE    = \n . n t f f f f
234  kills? = \n1 n2 . n1 n2 (n2 n1 n1 n2 n2 n2) (n2 n1 n1 n1 n2 n2) (n2 n1 n1 n1 n1 n2) n1
                   221ceb18                   »                   bill                                                   2011-06-01                                    cleaned up LC code                                                   235
236  # row functions
                   f6167440                   »                   bill                                                   2011-06-07                                    expanded grid from 9x9 to 19x19                                      237  row   = \1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 g . g 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
238  left  = \1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 g . g 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1
239  right = \1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 g . g 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
240  cellA = \1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . cell 1
241  cellZ = \1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . cell 19
242  get1  = \1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . 1
                   2227de09                   »                   bill                                                   2011-06-07                                    cleaned up page (code on right)                                      243  # return a new row with each element applied to a function
244  row-to-func   = \g 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 r . r (1 g) (2 g) (3 g) (4 g) (5 g) (6 g) (7 g) (8 g) (9 g) (10 g) (11 g) (12 g) (13 g) (14 g) (15 g) (16 g) (17 g) (18 g) (19 g)
245  # return a new row with a function applied to each element
246  func-to-row = \g 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 r . r (g 1) (g 2) (g 3) (g 4) (g 5) (g 6) (g 7) (g 8) (g 9) (g 10) (g 11) (g 12) (g 13) (g 14) (g 15) (g 16) (g 17) (g 18) (g 19)
247  # to calculate the value:
248  # 1) get the first result by applying the first cell to the function 'first'
249  # 2) get the next result for every other element by applying previous result to the function 'f' and applying the result of that to the row element
250  # 3) get the final value by applying the final result to the function 'last'
251  rowReduce = \first g last 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . 1 first g 2 g 3 g 4 g 5 g 6 g 7 g 8 g 9 g 10 g 11 g 12 g 13 g 14 g 15 g 16 g 17 g 18 g 19 last
                   f6167440                   »                   bill                                                   2011-06-07                                    expanded grid from 9x9 to 19x19                                      252  empty     = row e e e e e e e e e e e e e e e e e e e
253  arow1     = row e e e e e a1 e a1 e a1 e a1 e a1 e e e e e
254  gridStart = row arow1 empty arow1 empty arow1 empty arow1 empty empty empty empty empty empty empty empty empty empty empty empty
255  shipStart = row e e e e e e e e e s e e e e e e e e e
                   221ceb18                   »                   bill                                                   2011-06-01                                    cleaned up LC code                                                   256
257  # grid functions
258  identity  = \x . x
259  ignore    = \x . row
                   f6167440                   »                   bill                                                   2011-06-07                                    expanded grid from 9x9 to 19x19                                      260  onFirstEmpty    = \1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . 1 identity ignore ignore ignore ignore
                   2227de09                   »                   bill                                                   2011-06-07                                    cleaned up page (code on right)                                      261  # compare a cell with the first row element.  If they're both e, return a cell with e otherwise return one of the non-e values
                   f6167440                   »                   bill                                                   2011-06-07                                    expanded grid from 9x9 to 19x19                                      262  check1    = \n r . r \m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 m15 m16 m17 m18 m19 . cell (n m1 n n n n)
263  check9    = \n r . r \m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 m15 m16 m17 m18 m19 . cell (n m19 n n n n)
                   2227de09                   »                   bill                                                   2011-06-07                                    cleaned up page (code on right)                                      264  isAllFirstEmpty = rowReduce cellA check1 isE
265  isAllLastEmpty  = rowReduce cellZ check9 isE
266  allLeft   = row-to-func left
267  allRight  = row-to-func right
                   221ceb18                   »                   bill                                                   2011-06-01                                    cleaned up LC code                                                   268
                   d98332ba                   »                   bill                                                   2011-06-06                                    better counter (12 values, divisible by 2, 3, 4, and 6)              269  # counter
270  counter = \1 2 3 4 5 6 7 8 9 10 11 12 c . c 1 2 3 4 5 6 7 8 9 10 11 12
271  cNext   = \1 2 3 4 5 6 7 8 9 10 11 12 c . c 2 3 4 5 6 7 8 9 10 11 12 1
272  cTrue   = \1 2 3 4 5 6 7 8 9 10 11 12 . 1
273  slowest = counter t f f f f f f f f f f f
274  slower  = counter t f f f f f t f f f f f
275  slow    = counter t f f f t f f f t f f f
276  fast    = counter t f f t f f t f f t f f
277  faster  = counter t f t f t f t f t f t f
278  fastest = counter t t t t t t t t t t t t
279
                   221ceb18                   »                   bill                                                   2011-06-01                                    cleaned up LC code                                                   280  # game functions
                   2227de09                   »                   bill                                                   2011-06-07                                    cleaned up page (code on right)                                      281  cflip    = \n . n e s a2 a1 m
282  flip     = func-to-row cflip
283  allFlip  = row-to-func flip
284  start    = \statef . statef gridStart shipStart t slowest
285  checkDir = \grid left? . left? (grid isAllFirstEmpty) (not (grid isAllLastEmpty))
286  next     = \grid ship left? ctr statef . (\dir . statef (ctr cTrue (grid (dir allLeft allRight) allFlip) grid) ship dir (ctr cNext)) (checkDir grid left?)
                   a27c4c0f                   »                   bill                                                   2011-06-03                                    checkpoint                                                           287
                   786994ab                   »                   bill                                                   2011-05-30                                    more work                                                            288  #events
                   a188539f                   »                   bill                                                   2011-06-06                                    allow lambda characters in programs                                  289  moveLeft  = \grid ship left? ctr . next grid (ship (ship onFirstEmpty left)) left? ctr
290  moveRight = \grid ship left? ctr . next grid (ship (ship right onFirstEmpty right)) left? ctr
291  stay      = \grid ship left? ctr . next grid ship left? ctr
292  fire      = \grid ship left? ctr . next (missile grid ship) ship left? ctr
                   786994ab                   »                   bill                                                   2011-05-30                                    more work                                                            293
294  # accessors
                   f31f06cf                   »                   bill                                                   2011-06-06                                    fixed generated code comments                                        295  first = \a b c d . a
296  second = \a b c d . b
                   a188539f                   »                   bill                                                   2011-06-06                                    allow lambda characters in programs                                  297  num   = \n 0 1 2 3 4 . n 0 1 2 3 4
                   679d7e8d                   »                   bill                                                   2011-05-30                                    refactored evaluator.html into lc.js for embedding                   298
 a188539f » bill  2011-06-06 allow lambda characters in programs 299
 2227de09 » bill  2011-06-07 cleaned up page (code on right) 300
301
185  679d7e8d » bill  2011-05-30 refactored evaluator.html into lc.js for embedding 302 303
Something went wrong with that request. Please try again.