I wanted to show off some things I've learned about HPPL, so I made a Game of Life program today.
I'm still testing on whether or not making the grid larger via CONCAT or adding a 'is-off-grid' test condition in UPDATE is faster.
Without further ado, here's a bare-bones Conway's Game of Life simulator:
Code:
EDIT: Version 2, now with 100% less CONCAT. I got a 5% increase in speed out of it:
Code:
I'm still testing on whether or not making the grid larger via CONCAT or adding a 'is-off-grid' test condition in UPDATE is faster.
Without further ado, here's a bare-bones Conway's Game of Life simulator:
Code:
maxX = 64;
maxY = 64;
imageScale = 3;
onColor = RGB(0,0,0);
offColor = RGB(255,255,255);
grid;
UPDATE(X, Y)
BEGIN
LOCAL v = grid[X+1, Y+1];
PIXON_P(G1, X, Y, IFTE(v, onColor, offColor));
LOCAL s = grid[X+2, Y+1] + grid[X+2, Y+2] + grid[X+1, Y+2] + grid[X, Y+2] + grid[X, Y+1] + grid[X, Y] + grid[X+1, Y] + grid[X+2, Y];
RETURN IFTE(v, s==2 OR s==3, s==3);
END;
EXPORT GAMELIFE()
BEGIN
grid := CONCAT({MAKELIST(0,Y,0,maxY+1)},MAKELIST(CONCAT(0,MAKELIST(RANDINT(1),Y,1,maxY), 0),X,1,maxX), {MAKELIST(0,Y,0,maxY+1)});
DIMGROB_P(G1,maxX,maxY);
RECT(offColor);
RECT(G1,offColor);
WHILE NOT ISKEYDOWN(4) DO
grid := CONCAT({MAKELIST(0,Y,0,maxY+1)}, MAKELIST(CONCAT(0,MAKELIST(UPDATE(X, Y),Y,1,maxY), 0),X,1,maxX),{MAKELIST(0,Y,0,maxY+1)});
BLIT_P(G0, 0, 0, maxX*imageScale, maxY*imageScale, G1);
END;
END;
EDIT: Version 2, now with 100% less CONCAT. I got a 5% increase in speed out of it:
Code:
maxX = 64;
maxY = 64;
imageScale = 3;
onColor = RGB(0,0,0);
offColor = RGB(255,255,255);
grid;
UPDATE(X, Y)
BEGIN
IF X==1 OR X==maxX OR Y==1 OR Y==maxY THEN
RETURN 0;
END;
LOCAL v = grid[X, Y];
PIXON_P(G1, X, Y, IFTE(v, onColor, offColor));
LOCAL s = grid[X+1, Y] + grid[X+1, Y+1] + grid[X, Y+1] + grid[X-1, Y+1] + grid[X-1, Y] + grid[X-1, Y-1] + grid[X, Y-1] + grid[X+1, Y-1];
RETURN IFTE(v, s==2 OR s==3, s==3);
END;
EXPORT GAMELIFE()
BEGIN
grid := MAKELIST(MAKELIST(RANDINT(1),Y,1,maxY),X,1,maxX);
DIMGROB_P(G1,maxX,maxY);
RECT(offColor);
RECT(G1,offColor);
WHILE NOT ISKEYDOWN(4) DO
LOCAL T = TICKS();
grid := MAKELIST(MAKELIST(UPDATE(X, Y),Y,1,maxY),X,1,maxX);
RECT(offColor);
BLIT_P(G0, 0, 0, maxX*imageScale, maxY*imageScale, G1);
TEXTOUT_P(TICKS()-T, G0, 200, 40, 0, onColor);
END;
END;