Simple life
Name:
Anonymous
2007-09-08 12:44
ID:fuIqtYYp
import qualified Data.Map as M
type Location = (Int,Int)
main = renderloop [(1,2),(2,3),(3,1),(3,2),(3,3)]
where renderloop = mapM_ (\v -> render 20 v >> getLine) . iterate count
count :: [Location] -> [Location]
count locs = M.keys live ++ [v| v <-locs, M.member v still]
where neighborCount = foldl foldFun M.empty . concatMap allNeighbors
(live,still) = M.partition (== 3) . M.filter (`elem` [2,3]) . neighborCount $ locs
foldFun :: M.Map Location Int -> Location -> M.Map Location Int
foldFun map loc = M.insertWith (+) loc 1 map
allNeighbors :: Location -> [Location]
allNeighbors (x,y) = [(nx,ny) | nx <- [x+1,x-1,x], ny <- [y+1,y-1,y], (x,y) /= (nx,ny) ]
render size locs = mapM_ (putStrLn . makeRow locs size) (reverse [0..size])
makeRow locs wid r = map (\x -> if x `elem` inds then '#' else '.') [0..wid]
where inds = [j | (p,j) <- locs, p == r]
:)
Name:
Anonymous
2007-09-08 12:50
ID:6NTP7YTk
It withers out when it reaches the right border. Do something!
Name:
Anonymous
2007-09-08 12:57
ID:Heaven
THNK YOU ARE SMART DO U???????
Name:
Anonymous
2007-09-08 16:52
ID:rgB/27LD
(defun init-board (board)
(dolist (coord (quote ((1 . 2) (2 . 3) (3 . 1) (3 . 2) (3 . 3))) board)
(setf (aref board (car coord) (cdr coord)) (quote @))))
(defun count-life (board &aux (size (array-dimensions board)))
(let ((counts (make-array size :initial-element 0)))
(dotimes (y (first size) counts)
(dotimes (x (second size))
(dolist (c- (loop for xd from -1 to 1
nconc (loop for yd from -1 to 1
unless (= xd 0 yd)
collect (cons (+ x xd) (+ y yd)))))
(when (eql (quote @) (handler-case (aref board (car c-) (cdr c-)) (error)))
(incf (aref counts x y))))))))
(defun iterate-life (board &aux (size (array-dimensions board)) (counts (count-life board)))
(dotimes (y (first size) board)
(dotimes (x (second size))
(let ((count (aref counts x y)))
(setf (aref board x y)
(if (or (= 3 count) (and (eql (quote @) (aref board x y)) (= 2 count))) (quote @) (quote _)))))))
(defun main (&optional (board (init-board (make-array (quote (20 20)) :initial-element (quote _)))))
(print board) (sleep 0.1) (main (iterate-life board)))
Name:
Anonymous
2007-09-08 16:55
ID:ub/an7zK
Name:
Anonymous
2007-09-08 16:55
ID:Heaven
>>4
Let's rewrite a previous poster's code in some other language! My ``other language'' happens to be Common Lisp. Where are the Pythonfags?
Fixed.
Name:
Anonymous
2007-09-08 16:56
ID:Heaven
(
(
(
Name:
Anonymous
2007-09-08 16:59
ID:rgB/27LD
>>5
(CAR (CDR (LAMBDA (LAMBDA )))))))))))))))))))
>>6
um.. you didn't fix anything? lol, Provide your implementation of life in another language if you wish to participate in this
PROGRAMMING BOARD
Name:
Anonymous
2007-09-08 17:03
ID:Heaven
My Haskell implementation.
life = error "you fail :-("
Name:
Anonymous
2007-09-08 17:07
ID:Heaven
>>5
*** - HANDLER-CASE: illegal syntax of clause (ERROR)
Name:
Anonymous
2007-09-08 17:12
ID:Heaven
Name:
Anonymous
2007-09-08 17:20
ID:Heaven
>>11
(when (eql '@ (handler-case (aref board (car c-) (cdr c-)) (error)))
Should be
(when (eql '@ (handler-case (aref board (car c-) (cdr c-)) (error
())))
Name:
Anonymous
2007-09-08 17:24
ID:Heaven
>>5,10-12
One word, the illegal syntax of clause. Thread over.
Name:
Anonymous
2007-09-08 17:36
ID:rgB/27LD
Name:
Anonymous
2007-09-08 21:42
ID:rgB/27LD
moar
Name:
Anonymous
2007-09-08 21:44
ID:abeNituP
Haskell sucks
Name:
Anonymous
2007-09-08 21:48
ID:fuIqtYYp
post moar foo's
Name:
Anonymous
2007-09-08 21:53
ID:B2apljO6
Name:
Anonymous
2007-09-08 21:55
ID:B2apljO6
#!/usr/bin/perl6
use warnings;
use strict;
my $life = new life(20);
while(1)
{
$life.display();
}
class life
{
has Int $.count;
has Int $.dimension;
has Array of Int @.grid is dim ($.dimension, $.dimension);
method CREATE(Int $dimension)
{
$.count = 0;
$.dimension = $dimension;
loop (my $x = 0; $x < $dimenion; $x++)
{
loop (my $y = 0; $y < $dimension; $y++)
{
@.grid[$x][$y] = 0;
}
}
@.grid[$dimension / 2 - 1][$dimension / 2] = 1;
@.grid[$dimension / 2 - 1][$dimension / 2 + 1] = 1;
@.grid[$dimension / 2][$dimension / 2] = 1;
@.grid[$dimension / 2][$dimension / 2 - 1] = 1;
@.grid[$dimension / 2 + 1][$dimension / 2] = 1;
}
method calculate() is private
{
my @newgrid;
loop (my $x = 0; $x < .dimension; $x++)
{
loop (my $y = 0; $y < .dimension; $y++)
{
my $live = 0;
for ($x - 1, $y - 1, $x, $y - 1, $x + 1, $y - 1, $x - 1, $y, $
+x + 1,
$y, $x - 1, $y + 1, $x, $y + 1, $x + 1, $y + 1) -> ($nx, $ny
+)
{
next if 0 > $nx > .dimension || 0 > $ny > .dimension;
$live++ if @.grid[$nx][$ny] == 1;
}
$newgrid[$x][$y] = given @.grid[$x][$y]
{
when 0 { 1 if $live == 3};
when 1 { 1 if 1 < $live < 4 }:
} || 0;
}
}
@.grid = @newgrid;
}
method display
{
loop (my $x = 0; $x < $.dimension; $x++)
{
loop (my $y = 0; $y < $.dimension; $y++)
{
print $.grid[$x][$y] ? '+' : '.';
}
print "\n";
}
print "Turn $(++$.count), press enter for next turn, ctl-c to quit
+';
<STDIN>;
.calculate();
}
}
Name:
Anonymous
2007-09-08 21:56
ID:B2apljO6
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#define HMAX 500
#define VMAX 500
Display *d;
Window w;
GC gc;
int fg, bg;
char bitmap[2][HMAX][VMAX];
int hsize, vsize, psize, cbn;
void bgpixel_show(int i, int j)
{
XSetForeground(d, gc, bg);
XFillRectangle(d, w, gc,
(i-1)*psize, (j-1)*psize,
psize-1, psize-1);
}
void fgpixel_show(int i, int j)
{
XSetForeground(d, gc, fg);
XFillRectangle(d, w, gc,
(i-1)*psize, (j-1)*psize,
psize-1, psize-1);
}
void pixel(int c, int i, int j)
{
if (c<2 || c>3)
{
bitmap[1-cbn][i][j] = 0;
bgpixel_show(i,j);
return;
}
if (c==3)
{
bitmap[1-cbn][i][j] = 1;
fgpixel_show(i,j);
return;
}
bitmap[1-cbn][i][j] = bitmap[cbn][i][j];
}
bitmap_init()
{
int i,j;
for (i=1; i<=hsize; i++)
for(j=1; j<=vsize; j++)
if (bitmap[cbn][i][j] = rand() %2)
fgpixel_show(i,j);
else
bgpixel_show(i,j);
for (i=0; i<=hsize+1; i++)
{
bitmap[cbn][i][0] = 0;
bitmap[1-cbn][i][0] = 0;
bitmap[cbn][i][vsize+1] = 0;
bitmap[1-cbn][i][vsize+1] = 0;
}
for (j=0; j<=vsize+1; j++)
{
bitmap[cbn][0][j] = 0;
bitmap[1-cbn][0][j] = 0;
bitmap[cbn][hsize+1][j] = 0;
bitmap[1-cbn][hsize+1][j] = 0;
}
}
bitmap_next()
{
int i,j;
int c;
for (i=1; i<=hsize; i++)
for(j=1; j<=vsize; j++)
{
c = bitmap[cbn][i-1][j-1] + bitmap[cbn][i-1][j]
+ bitmap[cbn][i-1][j+1] + bitmap[cbn][i][j-1]
+ bitmap[cbn][i][j+1] + bitmap[cbn][i+1][j-1]
+ bitmap[cbn][i+1][j] + bitmap[cbn][i+1][j+1];
pixel(c,i,j);
}
cbn = 1-cbn;
}
void event_loop()
{
XEvent e;
while(1) {
XNextEvent(d, &e);
switch (e.type) {
case KeyPress:
while(1)
bitmap_next();
default:
break;
}
}
}
void window_init()
{
XGCValues gcv;
if((d=XOpenDisplay(NULL))==NULL)
{
exit(-1);
}
bg = WhitePixel(d,DefaultScreen(d));
fg = BlackPixel(d,DefaultScreen(d));
w = XCreateSimpleWindow(d, RootWindow(d,DefaultScreen(d)),
0, 0, psize * hsize, psize * hsize,
2, fg, bg);
gcv.function = GXcopy;
gcv.foreground = bg;
gcv.background = fg;
gc = XCreateGC(d, w,
GCFunction | GCForeground | GCBackground,
&gcv);
XMapWindow(d,w);
XSelectInput(d,w,
EnterWindowMask | LeaveWindowMask | ButtonPressMask |
OwnerGrabButtonMask | ExposureMask | StructureNotifyMask |
KeyPressMask);
}
void main()
{
hsize = 200;
vsize = 200;
psize = 4;
cbn = 0;
window_init();
bitmap_init();
event_loop();
}
Name:
Anonymous
2007-09-09 4:51
ID:F0p6yHkY
(((((((([/b]game-of-life))))))))
Name:
Anonymous
2007-09-09 7:41
ID:qofjCMY/
I am the Abelson and the Sussman,
the beginning and the end,
the first and the last.
Name:
Anonymous
2007-09-09 13:06
ID:+0NwdbJ8
pants
Name:
Anonymous
2007-09-09 15:26
ID:kPcEdU1G
>>19
OH GOD WHAT
has? Array of Int? method?
what did these fuckers do to MY Perl?
Name:
Anonymous
2007-09-09 20:12
ID:dGiz9hCK
ITT you all suck except
>>4 that posted original code.
Name:
Anonymous
2007-09-09 20:48
ID:RhOa47aK
Name:
Anonymous
2007-09-09 20:49
ID:SguFKrLo
WHERE'S MY PYTHON
Name:
Anonymous
2007-09-09 23:57
ID:op+H3Bf0
Name:
Anonymous
2007-09-10 3:14
ID:0L56hHW/
Loli Cross!
Name:
Anonymous
2010-12-17 1:38
FOLLOW THE
NEW GNAA TWITTER AT
http://twitter.com/Gary_Niger