Log In  

Cart #silicon14_1klife-0 | 2022-01-29 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

(Warning: If you are sensitive to flashing lights, I recommend you don't run this)

(Note: Original made for PICO-1K Jam)

This is Conway's Game of life (https://en.wikipedia.org/wiki/Conway's_Game_of_Life) in under 400 characters (and here is the proof):

g={}for y=0,128 do g[y]={} for x=0,128 do if((flr(rnd(2))+1)==1)g[y][x]=true else g[y][x]=false end end

::★:: cls() ng={} for y=0,128 do ng[y]={} for x=0,128 do nc=0 for dy=-1,1 do for dx=-1,1 do if not (dy==0and dx==0)and g[y+dy]and g[y+dy][x+dx] then nc+=1  end end end
ng[y][x]=nc==3or(g[y][x]and nc==2) if(g[y][x])pset(x,y,7) end end
g=ng flip() goto ★
P#105907 2022-01-29 17:03 ( Edited 2022-01-29 17:03)

1

Good LIFE program, @outcastinteractive. And yeah, it's times like this, @zep, I wish there was an obscure POKE you could access that would let you run your code flat out.

poke(nnn),vv

  • vv=0 (default)
  • vv=1-100, 1=slowest, 100=use entire CPU to speed up the execution to run at 30fps or 60fps no matter what other code is around it.

Cart #silicon14_1klife_mod-1 | 2022-01-29 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

Here I optimized it with no black page-flipping and a bit smaller at 324-chars instead of 357-chars above.

I think this can still be optimized further. Takers ?

P#105908 2022-01-29 17:35 ( Edited 2022-01-29 18:51)
1

ok, I did bite:

p={[3]=1,[12]=1,[13]=1}r=8192f=r*3pal(1,7,1)for t=f,r*4-1do
poke(t,rnd(256)&17)end::_::for w=0,16383do
x,y=w>>7,w&127c=9*pget(x,y)for k=0,8 do c+=pget(x+k%3-1,y+k/3-1)end
sset(x,y,p[c])end memcpy(f,0,r)goto _

208 chars
I've used the screen buffer as a source and the sprite sheet as destination, cell presence coded as color 0/1
each gen takes ~25 frames, sprite sheet used as a back buffer memcpied in one go, so no flip() nor holdframe() needed

hadn't done a tweetcart in a while, fun stuff! thanks guys :)

P#105958 2022-01-30 11:34

Superb work, @ultrabrite ! I'm still reading what you did here ...

P#105988 2022-01-30 18:19

@ultrabrite Nice! Sorry if this wasn't very optimize, this was my first time trying to compress code.

P#106011 2022-01-30 23:00

NP, @outcastinteractive. That's the thing - when I first arrived in the Pico-8 scene and started to write code, I had a whole bunch of people show me how I could improve my coding. Many I followed, in some I kept my old ways.

It's all up to you. :)

P#106019 2022-01-31 04:17

@ultrabrite ,can give me tips/resources on how to condense code? Compressing code, data, and manipulating Pico-8's memory are not my strong suites.

P#106088 2022-02-01 02:34

Sorry I intended to post some more explanation but got sidetracked
Here we go:

p={[3]=1,[12]=1,[13]=1} -- from count to color, more on this below

-- shortcuts, r and f are used twice:
r=8192 -- "r=8192"+"r"+"r" -> 8 chars instead of "8192"+"8192" -> 8 chars too. no gain...
f=r*3  -- but!, "f=r*3"+"f"+"f" -> 7 chars instead of "24576"+"24576" -> 10 chars 
-- overall, 3 chars less

-- color 1 (dark blue) will end up 7 (white) (on screen only, still 1 in mem):
pal(1,7,1)

-- init screen buffer with random fill, as bytes (0x00,0x01,0x10,0x11)
for t=f,r*4-1do poke(t,rnd(32)&17)end

::_::

  -- loop on all screen pixels
  for w=0,16383do

    x=w>>7y=w&127 -- current cell at (x,y)

    c=9*pget(x,y) -- init count with current cell 0/1 * (10 - 1)

    -- add up nine cells around (cell presence -> +1 else +0)
    for k=0,8do
       c+=pget(x+k%3-1,y+k/3-1)
    end

    -- here we have the 8 neighbors cell count (sum from 9 cells, minus the center one),
    -- plus 10 times the center one
    -- c = (center cell presence)*10+(#neightbors). 03,12,13 => cell is alive
    -- set sprite sheet pixel according to lookup table p: 
    --    03,12,13 -> 1, other values -> nil (meaning 0 for sset)
    sset(x,y,p[c])

  end

  memcpy(f,0,r) -- copy whole sprite sheet to screen buffer

goto _ -- next gen

^ shaved 3 chars while writing this, quite sure there's more ;)

I don't have an actual list of tricks and I'm not the best around at it but, off the top of my head:

  • check unecessary spaces (I missed one above)
  • rework parts in a line underneath:

    for x=0,127do for y=0,127do ... end end
    for w=0,16383do x=w>>7y=w&127 ... end     <- 2chars!
    
    pget() ... pget()
    p=pget p() ... p() <- might not be worth it
    
    pget() ... pget() ... pget()
    p=pget p() ... p() ... p() <- most likely worth it
  • rework other people tweetcarts with proper indentation. the math is often undecipherable, but you might find a new trick here or there
    https://twitter.com/hashtag/tweetcart?src=hashtag_click&f=live
    also check out ye olde tweetjam thread that started it all (most of it at 140chars):
    https://www.lexaloffle.com/bbs/?page=8&tid=3726
  • find your own pace and don't forget to have fun ;)
P#106158 2022-02-01 21:50

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 15:29:18 | 0.078s | Q:33