2048 game
This program was originally written on Windows 10 using Atom Text editor and run using Windows Powershell.
On Windows:
This code makes use of msvcrt module of python to implement getch type function. This feature is available only for Windows/DOS.
For UNIX based operating system:
1)First install pip3 or pip depending upon python version. To check python version, type python --version
. You should see something like this.
2)Then type sudo apt-get install pip3
or _sudo apt-get install pip_
.
3)For downloading getch function, pip3 install getch
. You can now run the code.
4)Other modules used in this code are “os”, “argparse”, “random”, “copy”, “math". If not available, they can be installed in similar manner.
How to run:
1)For windows, open command prompt or Windows Powershell and type python filename --n N --x X
, where N and X are board size and number required to win respectively and filename has “.py” extension. If you don’t want to specify any input, just write python filename
;
or to specify any one input, just write that respective argument only, else you’ll get error. Whenever not specified, default will set N to 5 and X to 2048. Ensure that X is a power of two; else you’ll never win.
2)For Unix based, type python3 filename --n N --x X
, or python2 depending upon python version. Rest is similar to windows.
How it works :
1)After you run the program, it will display the board of specified dimensions.
In this example board size is 3 and number required to win is 32.
2)Press w/a/s/d to shift numbers up/left/down/right respectively and e to quit.
3)If you enter any invalid move, i.e board doesn't change with a particular move played, it will ask you to try another move.
In the previous example, if we play "w" board doesn't change.
4)On entering any valid move, the board will change accordingly. This is an intermediate stage of same example.
5)When you get the required number to win(X) on your board, you win the game and the program stops. In this example, since we got the highlighted number(32), program gives a message game won
6)Other case, if you can’t play any more moves, you get a message game lost and program stops. It looks like this.
-Done by Kalyani Sainis.