News for hackers. Stuff that matters.
Re:Show HN: Simple JavaScript Tetris by jstimpfle on Wednesday November 02, 2016 @12:47PM (#12856742)
EDIT: Better URL https://github.com/jstimpfle/tetris-on-a-plane/

I had fun building this and experiencing the joyful side of JS (or at least the browser ecosystem). Boarding a plane I'd had the idea of finishing a simple tetris before arrival. The game was in a working state after ~2 hours, even though I had made a few bad decisions. (it was subsequently polished, investing another few hours).

It was made with a "get things done" mindset, and the experience from dabbling with purely functional programming in Haskell was very helpful (mostly the "pure", as in "non-destructive", part).

It's nice that in Javascript we have an environment where we can code a fun game in <400 lines, without any library dependencies (not even internet connection needed) or build systems. On the downside, if it had stronger dynamic types and errors (like Python) that would have saved maybe 25% of my debugging time, probably without coding overhead.

Re:Show HN: Simple JavaScript Tetris by zzguy on Wednesday November 02, 2016 @02:46PM (#12857975)
Had you made Tetris before? Or used an external resource to see any pseudocode? Reason I'm asking is, I was doing a similar thing with Snake and it took me a bit longer (probably 4 hours) because I messed up the basic mechanics in the beginning (I didn't think of how to structure the growing body as a linked list). I want to gauge how much I suck (at least at games programming) lol.
Re:Show HN: Simple JavaScript Tetris by jstimpfle on Wednesday November 02, 2016 @03:00PM (#12858143)
I once spoke about it with a friend who had to do it in C. However C is a dangerous trap here, I think it gets you in the wrong mindset.

Data representation is pretty universally one of the most important things (basically the "design" part to me). Four hours is pretty quick I guess. Snake might be more difficult (but an array or llist + snake direction + board map should be straightforward). I'd done chess that week and the representation is also doable unless thinking about AI or history or... which is when I quickly go bikeshedding. Multiple different representations for different tasks (and transforming in between) might be a way to go.

Re:Show HN: Simple JavaScript Tetris by pgt on Wednesday November 02, 2016 @05:43PM (#12859607)
I made Snake in 103 lines of ClojureScript for a talk once: https://github.com/pate/cljs-snake (LOC excludes whitespace and links to repo). Comes in under 100 LOC if you strip out the aesthetics.
Re:Show HN: Simple JavaScript Tetris by jstimpfle on Wednesday November 02, 2016 @06:55PM (#12860136)
Very nice!
Re:Show HN: Simple JavaScript Tetris by rbirkby on Wednesday November 02, 2016 @06:34PM (#12860018)
Check out the encoding of the tetrominoes in this implementation: https://github.com/jakesgordon/javascript-tetris

My subsequent port to C and Arduino was very natural: http://youtu.be/t3QOeQbEHVs

Re:Show HN: Simple JavaScript Tetris by electrum on Wednesday November 02, 2016 @08:16PM (#12860570)
Here's one I wrote a long time ago in Z80 assembly for the TI-86 graphing calculator: https://gist.github.com/electrum/76ac88de2e1d6c9254deaba003a...
Re:Show HN: Simple JavaScript Tetris by bbcbasic on Wednesday November 02, 2016 @08:43PM (#12860693)
The "Random" subroutine looks surprisingly simple
Re:Show HN: Simple JavaScript Tetris by agentultra on Wednesday November 02, 2016 @02:37PM (#12857889)
It is pretty cool. Feels very much like a smalltalk-ish environment. A WebGL context is a single HTML tag and a couple of lines of code away. However you can still get the productivity boosts from an ML-like language... there are options. And ES6 supports some patterns of FP out of the box (though it is made better with small library support).

Maybe checkout Bucklescript, Purescript, or Elm for the next one :)

One of my rituals of taking a long trip is to finish a small coding project before I land. Sudoku, minesweeper, tetris, forward-propagating constraint solvers... it's a nice practice. Thanks for sharing yours!

Re:Show HN: Simple JavaScript Tetris by g3 on Wednesday November 02, 2016 @07:58PM (#12860483)
And here is a concise JavaScript tetris (playable: http://copy.sh/tetris/):

    f=[];p=[1];g=h=2;s=0;t=500;for(i=0;23>i;i++)f[i]=j=-16380;f[23]=-1;onkeydown=e;e(e);function e(b,a){(k=b.which)?k-38?k&-3^37||(g+=a=k^37?-1:1):(z=p,v=p[2],c=p[1],p=[2*v&2|4*c&4,p[3]/2&1|v&2|2*c&4|4*p[0]&8,p[3]/4&1|v/2&2|c&4|2*p[0]&8,v/4&2|c/2&4]):s-j&&setTimeout(e,100+t,h++);m=p[0]<<g;l=p[1]<<g;_=p[2]<<g;o=p[3]<<g;f[h+=32==k]&m|f[1+h]&l|f[2+h]&_|f[3+h]&o?a?g-=a:k^38?(f[h]|=l,f[--h]|=m,f[2+h]|=_,f[3+h]|=o,h?(g=6,x=new Date%7*4,p=[h=0,2908739>>x&15,266758006>>x&15],t*=.97):s+=" Game Over!"):p=z:k^32||e(b);for(y=_=o="";24>++y;o+="\n")for(f[y]+4||(f.splice(y,1),f.unshift(j),s+=++_,o="",y=2),x=14;x;q.textContent=o+s)o+=" X"[(f[y]|p[y-h]<<g)>>x--&1]}
Re:Show HN: Simple JavaScript Tetris by geuis on Wednesday November 02, 2016 @11:01PM (#12861309)
Just because you minify and obfuscate this code doesn't somehow make it better or let you win the "contest".

I can walk through the submitter's code and understand what's happening clearly. It's fairly well structured and easy enough to read. This is 20x more valuable than any code that is produced using uncommented edge features for a perceived 5% gain. (Note that I'm making up these numbers to illustrate a point.)

Re:Show HN: Simple JavaScript Tetris by bronson on Thursday November 03, 2016 @01:59AM (#12861893)
Wait, where did the OP say it was better or won some contest?
Re:Show HN: Simple JavaScript Tetris by CardenB on Thursday November 03, 2016 @02:06AM (#12861914)
I think you're taking this too seriously
Re:Show HN: Simple JavaScript Tetris by wmichelin on Wednesday November 02, 2016 @04:27PM (#12858914)
Re:Show HN: Simple JavaScript Tetris by Sophistifunk on Wednesday November 02, 2016 @05:16PM (#12859368)
"Clever" code. shudder It's confusing to read for anybody not used to it, particularly as you need to set the "initial" value to the "actual initial value"+1.
Re:Show HN: Simple JavaScript Tetris by proto-n on Wednesday November 02, 2016 @04:37PM (#12859002)
Re:Show HN: Simple JavaScript Tetris by Cpoll on Wednesday November 02, 2016 @05:00PM (#12859225)
In other words, it's a crappy way to write `i-- > 0`

Note that it's not logically equivalent to `for (var i = 5; i > 0; i--)` because in this case the `i--` operation is applied after the body of the loop.

Try it in your browser: for (var i = 5; i-- > 0;) { console.log(i); } //4,3,2,1 for (var i = 5; i > 0; i--) { console.log(i); } //5,4,3,2,1

Re:Show HN: Simple JavaScript Tetris by jstimpfle on Wednesday November 02, 2016 @06:31PM (#12859991)
This is why I actually like this syntax (and get fun out of provoking "shudder" comments).

    - Can use the same limits as in ascending iteration
    - Can do decrementing in for loop (no extra line)
    - The arrow unambiguously signals that the iteration is correct :-)
Re:Show HN: Simple JavaScript Tetris by bbcbasic on Wednesday November 02, 2016 @08:45PM (#12860704)
!!"It's the JS way"
Re:Show HN: Simple JavaScript Tetris by jstimpfle on Wednesday November 02, 2016 @06:05PM (#12859819)
Link broke with new commit, here's a stable one: https://github.com/jstimpfle/tetris-on-a-plane/blob/d5d4d821...

This is a test to see how quickly someone brings up the stackoverflow link ;-)

Re:Show HN: Simple JavaScript Tetris by TheBiv on Wednesday November 02, 2016 @02:29PM (#12857814)
Playable version of this code located here http://jstimpfle.de/projects/tetris-on-a-plane/tetris.html
Re:Show HN: Simple JavaScript Tetris by jibreel on Wednesday November 02, 2016 @03:34PM (#12858498)
nice, as a web developer i wanted to try my hands on making a game. which turned out to be much harder than i thought, and also see if i could do any thing with c lang, never wrote a program more than ~15 lines of code in c, so i build a tetris game, using SDL lib.

funny thing is i also represented shapes like you did as 0 or 1 on plane, but then changed to x,y list of blocks.

here it is. https://gist.github.com/spaceexperiment/4d6b116ef577a2971259...

Re:Show HN: Simple JavaScript Tetris by mattnewton on Wednesday November 02, 2016 @03:41PM (#12858554)
Broken on Safari 10, and I can't figure out why. None of the keyboard keys seem to fire any events.
Re:Show HN: Simple JavaScript Tetris by dhritzkiv on Wednesday November 02, 2016 @06:01PM (#12859778)
It seems the code uses `event.key` (which returns a human-readable string, e.g. "ArrowLeft"). `event.key` is not supported in WebKit.
Re:Show HN: Simple JavaScript Tetris by jstimpfle on Wednesday November 02, 2016 @06:18PM (#12859919)
Thanks to you both. I converted to using ev.keyCode. Hope it works everywhere.
Re:Show HN: Simple JavaScript Tetris by alfanick on Wednesday November 02, 2016 @04:48PM (#12859117)
yup I can confirm that, uBlock disabled
Re:Show HN: Simple JavaScript Tetris by nthcolumn on Wednesday November 02, 2016 @04:16PM (#12858824)
Awesome! I made a fiddle https://jsfiddle.net/wt309enw/1/
Re:Show HN: Simple JavaScript Tetris by pryelluw on Wednesday November 02, 2016 @04:28PM (#12858930)
This is pretty fun to see on a fiddle. :)
Re:Show HN: Simple JavaScript Tetris by skanga on Wednesday November 02, 2016 @04:03PM (#12858726)
Very nice. Agree with danso that keys should be case insensitive. Also - the UP arrow should rotate in some (any) direction.
Re:Show HN: Simple JavaScript Tetris by tluyben2 on Thursday November 03, 2016 @11:12AM (#12864601)
Nice, I like writing things under constraint; especially without internet and manuals. Seems to take away a lot of stress and open up creativity, at least it does for me [0]. I am flying to AUS soon which will have me without internet for around 21 hours again and I will try another constraint game at that time.

[0] http://brainfisheatfishbrain.com/2015/03/writing-a-game-with...

Re:Show HN: Simple JavaScript Tetris by jnpatel on Wednesday November 02, 2016 @04:55PM (#12859187)
I like that that this implementation lets you shift right/left for a split second after you let the piece fall.

Not all Tetris versions I've seen do that. I'm not sure what official behavior, though.

Re:Show HN: Simple JavaScript Tetris by 0x0 on Wednesday November 02, 2016 @05:05PM (#12859256)
There's quite a few details about the official authentic tetris guidelines here, looks like it's probably reverse-engineered. https://tetris.wiki/Tetris_Guideline
Re:Show HN: Simple JavaScript Tetris by acconrad on Wednesday November 02, 2016 @02:57PM (#12858099)
I think you could optimize quite a bit of this. If you made your shape objects more robust, even just {x,y,on}, instead of doing all of those nested for loops, you could start leveraging array methods like filter to only select the ones where on === 1, then do some sort of matrix transform to swap out that x,y coord in the 4x4 with the shape you're replacing.

Either way, cool game! Pretty neat you could do that in sub 400 lines of code.

Re:Show HN: Simple JavaScript Tetris by jstimpfle on Wednesday November 02, 2016 @03:05PM (#12858206)
The reason why I'm (sort of) proud of this is exactly that I managed to not get hung up with such considerations and just do the obvious things. It's not a problem to scan through 200 blocks (of which often 50% will be habitated) once every 100ms or so).

If there is any need for optimization, then it would probably be to avoid DOM inefficiencies and opting for canvas or another environment.

Re:Show HN: Simple JavaScript Tetris by danso on Wednesday November 02, 2016 @03:32PM (#12858466)
Very neat. I was about to report a bizarre bug -- `a/d` for rotating pieces worked on my laptop keyboard but not my USB-connected keyboard -- but then I realized caps-lock was turned-on my connected keyboard. Still probably wouldn't hurt to make the conditional case-insensitive
Re:Show HN: Simple JavaScript Tetris by etaioinshrdlu on Wednesday November 02, 2016 @10:06PM (#12861039)
My iteration of the same thing

This one generates a wide array of pieces upon start. And has buttons for use on mobile

http://kevinusbaragon.us/jtris/jtris.html

Re:Show HN: Simple JavaScript Tetris by hauxir on Thursday November 03, 2016 @11:57AM (#12865078)
Here's one i made in Haskell and NCurses https://github.com/hauxir/haskell-tetris
Re:Show HN: Simple JavaScript Tetris by smnscu on Wednesday November 02, 2016 @04:59PM (#12859215)
Similar, in Python, using rudimentary CLI controls (it was a take-home test). https://github.com/andreis/bchw
Re:Show HN: Simple JavaScript Tetris by sp332 on Wednesday November 02, 2016 @05:12PM (#12859328)
I think the rotations feel wrong. If I get a Z piece, rotating twice should get me back where I started. Instead it's shifted sideways one space. I have to shift 4 times to get back.
Re:Show HN: Simple JavaScript Tetris by jstimpfle on Wednesday November 02, 2016 @06:33PM (#12860005)
The scheme used here is a 4x4 block (the minimum required to accomodate all pieces) and rotations by 90 degrees. You can rotate both cw and ccw, this works for undoing rotation of all pieces.
Re:Show HN: Simple JavaScript Tetris by pryelluw on Wednesday November 02, 2016 @04:29PM (#12858940)
@OP License?

Can I use it as a teaching tool? :)

Re:Show HN: Simple JavaScript Tetris by jstimpfle on Wednesday November 02, 2016 @06:23PM (#12859947)
Sure!
Re:Show HN: Simple JavaScript Tetris by pryelluw on Wednesday November 02, 2016 @09:37PM (#12860935)
Thanks. Please add a license file to repo of you haven't done so already. :)
Re:Show HN: Simple JavaScript Tetris by Scarbutt on Wednesday November 02, 2016 @04:14PM (#12858811)
no babel/webpack?
Re:Show HN: Simple JavaScript Tetris by nkg on Wednesday November 02, 2016 @04:18PM (#12858838)
XD