2048 CLI in python

I’ve decided to code a simple CLI version of the 2048 game in python:

https://github.com/kunigami/blog-examples/blob/master/2014-05-13-2048-python/2048.py

Some details of the implementation:

* I’ve handled only the “up” move. For the others I do a rotation of either 90, 180 or 270 degrees the matrix representing the board.
* The core of the algorithm is apply “gravity”, perform the merge with adjacent element with same number and then apply the “gravity” again because my merge function can leave empty spaces.

[bash] Move and change directory (mv & cd)

In many cases when I move one or more files to a directory I want to change to that directory afterwards. I did a little search and found this discussion, which resulted in the following bash script function:

function mvcd () 
{ 
     num_of_files=$(($# - 1))
     for i in $(eval echo {1..$num_of_files})
     do
         eval mv \${$i} \${$#}
     done
     eval cd \${$#}
}

Example:

mkdir c
touch a.txt b.txt
mvcd a.txt b.txt c

[Linux] xclip command

Copying

The following command

echo "foo" | xclip -selection clipboard

will copy “foo” to you clipboard. Try pasting it elsewhere. The clipboard selection is the one used by Ctrl-C Ctrl-V commands. The default selection of xclip is the one used when you select text in linux (XA_PRIMARY).

A shorter version would be:

echo "foo" | xclip -sel c

Pasting

You can also send the contents of the current clipboard to the terminal (useful for piping).

xclip -sel c -out

Or

xclip -sel c -o