Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Why this no work?

Name: Anonymous 2011-04-21 11:41

;___;



(defmacro get-pos (board x y)
  `(nth ,y (nth ,x ,board)))

(defun cell-times (lst &optional (acc 0))
  (if lst
      (cond ((eq (car lst) nil)
         (cell-times (cdr lst) acc))
        ((numberp (car lst))
         (cell-times (cdr lst) acc))
        (t (cell-times (cdr lst) (1+ acc))))
      acc))
 
(defun get-if-possible (board x y) ;X och Y felet ligger här!?
  (if (not
       (and (>= x (length board))
        (>= y (length (nth 0 board)))))
      (if
       (not (< x 0))
       (if (not (< y 0))
       (get-pos board x y)))))

(defun get-neighbours (board x y)
  (let ((output '() ))
    (push (get-if-possible board (1+ x) y) output)
    (push (get-if-possible board (1- x) y) output)

    (push (get-if-possible board x (1+ y)) output)
    (push (get-if-possible board x (1- y)) output)

    (push (get-if-possible board (1+ x) (1+ y)) output)
    (push (get-if-possible board (1- x) (1- y)) output)

    (push (get-if-possible board (1+ x) (1- y)) output)
    (push (get-if-possible board (1- x) (1+ y)) output)
    output))

(defun next-gen (board) ;Den ändrar board, men hur ><?!
  (let ((output board))
  (loop for x-num from 0 to (1- (length output))
     for x in output do
       (loop for y in x
        for y-num from 0 to (1- (length x)) do
        (let ((neigh-val (cell-times (get-neighbours output x-num y-num))))
          (cond
        ((< neigh-val 2) (setf (get-pos output x-num y-num) neigh-val))
        ((= neigh-val 3) (setf (get-pos output x-num y-num) 'x))
        ((> neigh-val 3) (setf (get-pos output x-num y-num) neigh-val))))))
  output))

Name: Anonymous 2011-04-21 14:04

lol I was doing the same thing yesterday because I saw some interesting thread on Reddit. May I ask if your motivation is the same as mine?

btw, here's the code in HASKAL

import Data.Array

neighbs :: (Int, Int) -> Array (Int, Int) Bool -> Int
neighbs (i,j) arr
  | (iOK i) && (jOK j)  = count i j 1 1 1 1
  | i==mMax && (jOK j)  = count i j 1 0 1 1
  | i==mMin && (jOK j)  = count i j 0 1 1 1
  | (iOK i) && j==nMax  = count i j 1 1 1 0
  | (iOK i) && j==nMin  = count i j 1 1 0 1
  | (i,j)==(mMin, nMin) = count i j 0 1 0 1
  | (i,j)==(mMin, nMax) = count i j 0 1 1 0
  | (i,j)==(mMax, nMin) = count i j 1 0 0 1
  | (i,j)==(mMax, nMax) = count i j 1 0 1 0
  | otherwise           = 0
  where  ((mMin, nMin), (mMax, nMax)) = bounds arr
         iOK i = (i > mMin) && (i < mMax)
     jOK j = (j > nMin) && (j < nMax)
     count i j di ui dj uj = length.filter id $ [arr!(k,l) | k<-[i-di..i+ui], l<-[j-dj..j+uj], (k,l)/=(i,j)]

updateGrid :: Array (Int, Int) Bool -> Array (Int, Int) Bool
updateGrid arr = arr // [((i,j), updateCell i j) | i<-[mMin..mMax], j<-[nMin..nMax]]
  where ((mMin, nMin), (mMax, nMax)) = bounds arr
        updateCell i j
      | arr!(i,j) && (aliveN < 2)        = False
      | arr!(i,j) && (aliveN > 3)        = False
      | arr!(i,j)                        = True
      | (not$arr!(i,j)) && (aliveN == 3) = True
      | otherwise                        = False
      where
        aliveN = neighbs (i,j) arr

grid2String :: Array (Int, Int) Bool -> String
grid2String arr = strGrid mMin nMin arr
  where ((mMin, nMin), (mMax, nMax)) = bounds arr
        strGrid i j arr
      | j>nMax    = []
      | i>mMax    = '\n':strGrid mMin (j+1) arr
      | otherwise = showCell (arr!(i,j)) : (strGrid (i+1) j arr)
    showCell b = if b then '*' else ' '

readString :: String -> [(Int, Bool)]
readString s = zip [0,1..] (map readCell s)
  where readCell ' ' = False
        readCell '*' = True

readLinesGrid :: [String] -> [((Int, Int), Bool)]
readLinesGrid ss = concat $ map (\(j, xs) ->
                          map (\(i, b) -> ((i,j),b)) xs) $ zip [0,1..] (map readString ss)

readGrid :: String -> Array (Int, Int) Bool
readGrid str =
  let gridList = readLinesGrid $ lines str
      minBound = (0,0)
      (maxBound, _) = last gridList
  in  array (minBound, maxBound) gridList

nCycles :: Int -> Array (Int, Int) Bool -> Array (Int, Int) Bool
nCycles n arr = doNCycles 1 arr
  where doNCycles i arr
          | i>n = arr
      | otherwise = updateGrid $ doNCycles (i+1) arr

diff :: Array (Int, Int) Bool -> Array (Int, Int) Bool -> Int
diff arr1 arr2
  | mMin==mMin2 && nMin==nMin2 = sum [if (arr1!(k,l)) == (arr2!(k,l)) then 0 else 1 | k<-[mMin..mMax], l<-[nMin..nMax]]
  | otherwise                  = undefined
  where ((mMin, nMin), (mMax, nMax))     = bounds arr1
        ((mMin2, nMin2), (mMax2, nMax2)) = bounds arr2


has some utility shit to read the initial board from text file too

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List