Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: 2048.cpp – Play 2048 in directly your terminal (github.com/plibither8)
135 points by plibither8 on Sept 2, 2018 | hide | past | favorite | 45 comments



Have we really come so far? This has to be full circle, right?

First we had a wave of “see what can be done in a browser!” kind of demos.

Which at the time was massively impressing, because nobody thought you could seriously use the browser for other things than documents and very slow and clicky applications.

These days impressive browser-based experiences are the norm and native software is often accused of being visually “boring” and inanimate.

And here we’re back at the beginning: a tech demo for a native app, in the terminal, trying to reproduce what was once a tech demo of what you could do in a browser, as if that’s somehow novel.

Just 30 (or more?) years after nethack.

I’m honestly not sure how to feel about that ;)


If it helps, I wrote an implementation for Emacs: https://bitbucket.org/zck/2048.el


The game that 2048 is based on is a native mobile app...


Also, neither game was successful because it was an impressive graphically... it was just a fun game. This whole discussion is irrelevant but happens all the time on HN. Why?


This is much better than Electron apps for simple things that eat 1 GB of RAM (more or less as a rule) and are horrendously laggy.


> trying to reproduce what was once a tech demo of what you could do in a browser

2048 was never that, it was a shitty knockoff of Threes and remains so.


Maybe it's a knockoff, but I'd call it improved. I never liked number 3, and for me the additions in Threes look completely arbitrary. Whereas 2048 features nice, round (base 2) numbers.

And I'm only half-joking. I've been working with base 2 for two thirds of my life now, so powers of two are really more pleasant emotionally to me than multiples of 3.


I get that, I still feel a little weird my work computer has 12GB of RAM.


Threes! is inherently more complicated, dare I say topographically so.

The two games are obviously very similar, and you can list their rules pretty easily, and how those rules differ. I'm going to attempt to write them out below for my benefit more than anything else, but my point is mostly about rule number (4):

In Threes! tiles combine if they are 3 or greater and the same, or if they add to 3; in 2048 they combine only if they are the same.

This means that the 'base level' tiles, 1 and 2, cannot combine with copies of themselves. Coupled with (7) this means that there is a lot of planning to make sure that new tiles can be combined with the correct type of tile.

This mechanic is not possible using powers of 2, unless you had two 'types' of 2-tile that cannot combine with themselves. For example, a black 2 and a white 2, that combine to make a grey 4. That would be interesting, but less clean than using 1+2=3, I think.

The other differences in the rules are important but have less to do with multiples of 3 (where the multipliers are powers of 2) vs powers of 2.

While both games have randomness inherent in them, Threes! allows forward planning a lot more. You can keep track of which tiles are left in the deck, and as tiles only move one grid square at a time they are somewhat more manoeuvrable. Tiles only combine if you squeeze them together, while in 2048 they feel a lot more 'magnetic', jumping together as soon as they can. This gives you more control over when tiles combine, which is really important for positioning moves. It also means that Threes! has more tiles on it and feels more crowded as the game goes on, because many moves will not combine any tiles at all, while in 2048 almost any move will combine some tiles.

==== Rules ====

Start with a grid that has tiles on it, each tile taking one space and containing a number. A turn consists of pushing all tiles in one direction, either up, down, left, or right.

Tiles will either stay where they are or slide in the direction pushed, and may combine with tiles next to them (in the direction of pushing). When tiles combine they create a new tile with number equal to the sum of the two that combine, and this new tile replaces the other two.

We will say a tile is next to another tile only if they are adjacent in the direction of the push.

In Threes!

1. the grid is 4x4

2. a tile stays where it is if it is next to a wall, or next to a tile that is staying where it is and that it cannot combine with

3. a tile that moves will only ever move one tile

4. two tiles can combine if their sum is 3, or they are 3 or greater and the same

5. two tiles that can combine will combine if they are next to each other and both cannot move

6. after each move a new tile will appear next to the opposite wall, on a line where at least one tile moved

7. new tiles are drawn from a shuffled 'deck' of twelve tiles - four 1s, four 2s, and four 3s. Occasionally a large 'wild' tile will be picked instead of the next tile from the deck

8. to start, the grid has 9 new tiles placed randomly on it

In 2048

1. the grid is 4x4

2. a tile stays where it is if it is next to a wall, or next to a tile that is staying where it is and that it cannot combine with

3. a tile will move as far as possible in the direction it is pushed

4. two tiles can combine if they are the same

5. two tiles that can combine will always combine if there are no other tiles between them

6. after each move a new tile will appear in a random empty square

7. new tiles will be either a 2 (90%) or 4 (10%)

8. to start, the grid has 1 new tile placed randomly on it


The problem with 2048 is that it’s not a very good game. You can solve it algorithmicly.

Repeat swipe left then down down. If the board gets stuck swipe right then down and repeat.


Chess can also be solved algorithmically (the first algorithms for solving games were specifically for the solving of chess games). I don't think a game being solvable by an algorithm detracts from its quality as a game (though 2048 might be a bad game for other reasons).

I actually am struggling to think of a game that cannot be played by an algorithm (aside from obvious games like tennis). Even Jeopardy could be played by a computer.


> The problem with 2048 is that it’s not a very good game. You can solve it algorithmicly.

Every game is susceptible to algorithms by definition, by itself, it doesn't make a game more or less good, or more or less fun.

For me, 2048 hits a perfect spot where I can play it during meetings/conference calls/regular phone calls, to give that 20% of my mind that gets bored something to do, while not impacting my ability to focus on the discussion itself.


I believe that that strategy is decent but not optimal.


It's not really even decent, but I agree that it helped me improve my win rate quote a bit, mostly through avoiding movements up unless necessary, and by trying to stick and keep the largest number in one of the lower corners.


Most (if not all) games can be played algorithmically, some can be solved (ie. have a known best move for every state). As far as I'm aware 2048 is far from being solved, however expectimax implementations fare quite well.

From playing the game with your algorithm, you won't have a high win rate.


Wasn't 2048 based on a game call Three on iOS? It was a blatant copy over to Android. Or maybe I got my wires crossed.


Indirectly, yes. The developer of the version of 2048 everyone has heard of (Gabriele Cirulli) had apparently never heard of or seen Threes, but the games he was copying/inspired were in turn inspired by/copied from Threes, if you dig back far enough.

Note that Threes has different, more complex rules. Some prefer it, but apparently, more people don't. :)


Type

    telnet telehack.com

    2048
in a terminal


For other telnet games, there is milek7.pl with tetris, breakout and multiplayer pong.


Tetrinet for CLI [1]. Not sure if it still works.

Also, Tron over SSH [2]

[1] http://tetrinet.or.cz/

[2] $ ssh sshtron.zachlatta.com


oh, I miss the backgammon games over telnet.

Does anyone knows of any servers still online?


There's always fibs :)

telnet fibs.com 4321


Another, in Python: https://github.com/darius/sturm/blob/master/2048.py This one animates the sliding of the pieces.

The terminal input-handling might not work in your OS -- I gave up trying to find a nice cross-platform approach to that.


> The terminal input-handling might not work in your OS -- I gave up trying to find a nice cross-platform approach to that.

I wrote a terminal version of 2048 back in 2014 [1] and the input-handling works on all platforms I know of. You may want to look at the code: https://github.com/bfontaine/term2048/blob/master/term2048/k...

[1]: https://github.com/bfontaine/term2048


Thanks. I'm not sure yet if that will help with the trouble I was having (I wanted the library to support extra features not needed for 2048), but it's great to see small code to learn from.


I wrote it in c as a first project in intro CS in college:

https://github.com/iRapha/2048linux


I did a similar thing with 1010 a while ago: https://github.com/trosh/1010

These projects are probably useless but very fun to make and contribute to if you feel like practising. Some stranger actually contributed to my rushed 1010 clone, it was the best feeling ever.


A few years ago I wrote a terminal version of threes (the imho better predecessor to 2048): https://github.com/mwylde/Threes.scala, including ANSI graphics for the gameboard.


Very cool!

I noticed Wikipedia references another app for terminal in C: https://github.com/Tiehuis/2048-cli

Might be interesting to compare.


There is version written in sed, too.



You rock!

EDIT: Not only because of the technical achievement, but because this little has allowed me to waste some time playing a fun game, while looking very busy to passers-by to whom anything being done in a terminal looks like black magic from The Matrix. ;-) That game can be seriously addictive.


Did a similar thing a while ago that should work on all POSIX systems. https://github.com/vmednis/2048


Wrote it in C++ too a while back (5 years ago ) https://github.com/tlextrait/2048


Shameless self-plug: Play 2048 in the kernel - https://github.com/Mic92/2048.ko



nice! how did you record the commandline as a gif?


I will throw another option, very lightweight: licecap, works on win and osx, from the original dev of winamp!

https://www.cockos.com/licecap/


Had no idea and I've been pushing licecap for years.


Personally, I'm fond of https://asciinema.org/


Not sure how OP did it, but I use peek. Pretty handy for readmes, even for showing clients stuff without having to deploy.


If you’re on OS X, gifox is nice.[0]

[0] https://gifox.io


Ok, now I'm tempted to rewrite Twenty to work in the terminal. It's already c++!


Huh. Seems to misalign the right side of the grid in my terminal. Still pretty cool though.


At first I thought this was going to be one of those template preprocessor games.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: