Name:
Anonymous
2011-04-12 10:28
Lets suppose that we have an array which looks like this:
#(#(O O O O)
#(O O X O)
#(O O O O))
How would I go about to find the neighbours of 'X
without using loads of if-else ?
Name:
Anonymous
2011-04-13 8:10
#lang racket
;; Conway's Game of Life
(define (lives? currently-alive neighbors)
(if currently-alive
(case neighbors [(2 3) #t] [else #f])
(= neighbors 3)))
(define (how-many-living cells)
(length (filter (lambda (cell) cell) cells)))
(define (my-vector-ref vector n)
(vector-ref vector (let ([i (vector-length vector)])
(cond
[(< n 0) (+ i n)]
[(= i n) 0]
[else n]))))
(define (next-generation game)
(let ([y (vector-length game)]
[x (vector-length (vector-ref game 0))])
(list->vector
(let loop ([j (- y 1)]
[new-game '()])
(if (< j 0) new-game
(loop (- j 1)
(cons (list->vector
(let loop ([i (- x 1)]
[row '()])
(if (< i 0) row
(loop (- i 1)
(cons (lives?
(vector-ref (vector-ref game j) i)
(how-many-living
`(,(my-vector-ref (my-vector-ref game (- j 1)) (- i 1))
,(my-vector-ref (my-vector-ref game (- j 1)) i)
,(my-vector-ref (my-vector-ref game (- j 1)) (+ i 1))
,(my-vector-ref (my-vector-ref game j) (- i 1))
,(my-vector-ref (my-vector-ref game j) (+ i 1))
,(my-vector-ref (my-vector-ref game (+ j 1)) (- i 1))
,(my-vector-ref (my-vector-ref game (+ j 1)) i)
,(my-vector-ref (my-vector-ref game (+ j 1)) (+ i 1)))))
row)))))
new-game)))))))
(define (play game generations)
(if (<= generations 0)
(displayln "Fuck you, ``faggot''!!!" (current-error-port))
(let loop ([g 1]
[game (next-generation game)])
(displayln game)
(unless (= g generations)
(loop (+ g 1) (next-generation game))))))
;;
;; Now let's try 'er out:
;;
(play #(#(#t #f #t #f #t)
#(#f #t #f #f #f)
#(#f #t #f #t #t)
#(#f #t #t #f #f)
#(#t #f #f #f #f))
25)
;;
;; Would output:
;;
#(#(#t #f #f #f #t) #(#f #t #f #f #f) #(#f #t #f #t #f) #(#f #t #t #t #t) #(#t #f #t #t #t))
#(#(#f #f #t #f #f) #(#f #t #t #f #t) #(#f #t #f #t #t) #(#f #f #f #f #f) #(#f #f #f #f #f))
#(#(#f #t #t #t #f) #(#f #t #f #f #t) #(#f #t #f #t #t) #(#f #f #f #f #f) #(#f #f #f #f #f))
#(#(#t #t #t #t #f) #(#f #t #f #f #t) #(#f #f #t #t #t) #(#f #f #f #f #f) #(#f #f #t #f #f))
#(#(#t #f #f #t #t) #(#f #f #f #f #f) #(#t #f #t #t #t) #(#f #f #t #f #f) #(#f #f #t #t #f))
#(#(#f #f #t #t #t) #(#f #t #t #f #f) #(#f #t #t #t #t) #(#f #f #f #f #f) #(#f #t #t #f #f))
#(#(#t #f #f #f #f) #(#f #f #f #f #f) #(#t #t #f #t #f) #(#t #f #f #f #f) #(#f #t #t #f #f))
#(#(#f #t #f #f #f) #(#t #t #f #f #t) #(#t #t #f #f #t) #(#t #f #f #f #t) #(#t #t #f #f #f))
#(#(#f #f #t #f #t) #(#f #f #t #f #t) #(#f #f #f #t #f) #(#f #f #f #f #f) #(#f #t #f #f #t))
#(#(#f #t #t #f #t) #(#f #f #t #f #t) #(#f #f #f #t #f) #(#f #f #f #f #f) #(#t #f #f #t #f))
#(#(#f #t #t #f #t) #(#t #t #t #f #t) #(#f #f #f #t #f) #(#f #f #f #f #t) #(#t #t #t #t #t))
#(#(#f #f #f #f #f) #(#f #f #f #f #t) #(#f #t #t #t #f) #(#f #t #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #f #f #f) #(#f #f #t #t #f) #(#t #t #t #t #f) #(#f #t #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #f #f #f) #(#f #f #f #t #t) #(#t #f #f #t #t) #(#t #t #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #f #f #f) #(#t #f #f #t #f) #(#f #t #t #t #f) #(#t #t #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #f #f #f) #(#f #t #f #t #t) #(#f #f #f #t #f) #(#t #t #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #f #f #f) #(#f #f #t #t #t) #(#f #t #f #t #f) #(#f #f #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #f #t #f) #(#f #f #t #t #t) #(#f #f #f #t #t) #(#f #f #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #t #t #t) #(#f #f #t #f #f) #(#f #f #t #f #t) #(#f #f #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #t #t #f) #(#f #t #t #f #t) #(#f #f #f #t #f) #(#f #f #f #f #f) #(#f #f #f #t #f))
#(#(#f #t #f #f #t) #(#f #t #f #f #t) #(#f #f #t #t #f) #(#f #f #f #f #f) #(#f #f #t #t #f))
#(#(#f #t #f #f #t) #(#f #t #f #f #t) #(#f #f #t #t #f) #(#f #f #f #f #f) #(#f #f #t #t #f))
#(#(#f #t #f #f #t) #(#f #t #f #f #t) #(#f #f #t #t #f) #(#f #f #f #f #f) #(#f #f #t #t #f))
#(#(#f #t #f #f #t) #(#f #t #f #f #t) #(#f #f #t #t #f) #(#f #f #f #f #f) #(#f #f #t #t #f))
#(#(#f #t #f #f #t) #(#f #t #f #f #t) #(#f #f #t #t #f) #(#f #f #f #f #f) #(#f #f #t #t #f))
;;
;; Observation: Entropy change survives for 21 generations in the particular test game above.
;; The 21st generation's state, namely
;; #(#(#f #t #f #f #t) #(#f #t #f #f #t) #(#f #f #t #t #f) #(#f #f #f #f #f) #(#f #f #t #t #f))
;; does not change.
;;