Create a simple 2048 game clone 2048 is an incredibly fun and addictive game in which the goal is to create a tile with 2048 on it.

Here's a short description of the game:



Pressing an arrow key will slide all the blocks in the stage in that direction. For example, if x represents a block, and you pressed the up arrow in this case:

...x
.x..
..x.
xx..


Then the board would become

xxxx
.x..
....
....


Furthermore, the blocks are numbered, starting at 2. If two of the same numbered blocks are moved together, they will merge into the next number. For example, pressing "up" on this board:

.2..
..22
.2..
....


Would create this:

.422
....
....
....


And then after pressing "right," it would become ..44, and therefore pressing right again would create an "8" block, and so on.

Each turn, a new "2" block is created on a random open square. (It's actually not always a "2," but for simplicity let's keep it at that.) If there are no possible moves left (i.e., the board is full and you can't merge anything), the game is lost, and if a 2048 block is created, you win!



Your challenge is to recreate this game, golfed!


For edge-cases, such as pressing "right" on this board:

....
.222
....
....


you must merge the tiles closest to the edge of the arrow key pressed. For example, this would become ..24, because the second and third "2"s are the closest to the right edge.
If the player presses an invalid move (such as "up" on a board like 2.2. / .... / .... / ....), you must ignore the move.
Moves can be accepted in any way, such as clicking, arrow keys, ULRD, etc.
The tiles must all be the same shape—a "1024" tile should be the same size as a "2" tile.
The tiles must be separated in some way. For example, |1024|   2|1024|1024| is a valid example of a row (assuming that the tiles are squares), while 1024   210241024 is not.
You must output "you win" if the player creates a 2048 tile, or "you lose" if there are no valid moves left.
This is code-golf, so the shortest code in bytes will win!
GolfScript, 323 characters
1,16*{16,{1$=!},.,rand=)/()2++\[]*+4/{.['-----']4*'+'*'++'1/*n+:^\{'|''     ':X'|'+4*n++:Y.@{X\`+-5&gt;'|'\+}%'|'+n+Y+Y+^+}%+puts}:P~&quot;wasd&quot;:Q{1$.@{Q\?[{zip}{}{-1%zip-1%}{{-1%}%}]=\1$~{0-[{.2$={+)}*}*0...]4&lt;{1|(}%}%\~}:^~=!},:F{0{;'&quot;#{'9.?rand';STDIN.getc()}&quot;'++~.F\?0&lt;}do^[]*.2048?0&lt;.!{'you win'@@}*}{'you lose'@@*0}if}do 4/P;

The keys you have to use are wasd but you can change the constant in the code to whatever keys you like. It gracefully ignores any unknown key or illegal move. Unfortunately you have to press enter after each key.

256 character solution
1,16*{16,{1$=!},.,rand=)/())+\[]*+4/{.{{.9&gt;7*+48+}%n+}%puts}:P~&quot;wasd&quot;:Q{1$.@{Q\?[{zip}{}{-1%zip-1%}{{-1%}%}]=\1$~{0-[{.2$={;21+}*}*0...]4&lt;{20%}%}%\~}:^~=!},:F{0{;'&quot;#{'9.?rand';STDIN.getc()}&quot;'++~.F\?0&lt;}do^[]*.10?0&lt;.!{'you win'@@}*}{'you lose'@@*0}if}do 4/P;

Here the output is hexadecimal logarithmic, i.e. 0 stands for ., 1 for 2, 2 for 4, 3 for 8 ... B for 2048. Since we all know hexadecimals and the powers of two this shouldn't be a limitation at all.
Example run (first moves):
0000
0000
0010
0000
w
0011
0000
0000
0000
a
2000
0000
0001
0000
s
0000
0000
0100
2001
d
0000
0000
1001
0021
w
1022
0000
0001
0000
a
1301
0000
1000
0000
s
0000
1000
0000
2301
d
0000
1001
0000
0231