forked from tarunsinghofficial/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A normal 2048 game
- Loading branch information
Showing
3 changed files
with
394 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"SIZE = 400\n", | ||
"GRID_LEN = 4\n", | ||
"GRID_PADDING = 10\n", | ||
"\n", | ||
"BACKGROUND_COLOR_GAME = \"#92877d\"\n", | ||
"BACKGROUND_COLOR_CELL_EMPTY = \"#9e948a\"\n", | ||
"\n", | ||
"BACKGROUND_COLOR_DICT = {2: \"#eee4da\", 4: \"#ede0c8\", 8: \"#f2b179\",\n", | ||
" 16: \"#f59563\", 32: \"#f67c5f\", 64: \"#f65e3b\",\n", | ||
" 128: \"#edcf72\", 256: \"#edcc61\", 512: \"#edc850\",\n", | ||
" 1024: \"#edc53f\", 2048: \"#edc22e\",\n", | ||
"\n", | ||
" 4096: \"#eee4da\", 8192: \"#edc22e\", 16384: \"#f2b179\",\n", | ||
" 32768: \"#f59563\", 65536: \"#f67c5f\", }\n", | ||
"\n", | ||
"CELL_COLOR_DICT = {2: \"#776e65\", 4: \"#776e65\", 8: \"#f9f6f2\", 16: \"#f9f6f2\",\n", | ||
" 32: \"#f9f6f2\", 64: \"#f9f6f2\", 128: \"#f9f6f2\",\n", | ||
" 256: \"#f9f6f2\", 512: \"#f9f6f2\", 1024: \"#f9f6f2\",\n", | ||
" 2048: \"#f9f6f2\",\n", | ||
"\n", | ||
" 4096: \"#776e65\", 8192: \"#f9f6f2\", 16384: \"#776e65\",\n", | ||
" 32768: \"#776e65\", 65536: \"#f9f6f2\", }\n", | ||
"\n", | ||
"FONT = (\"Verdana\", 40, \"bold\")\n", | ||
"\n", | ||
"\n", | ||
"KEY_UP_ALT = \"\\'\\\\uf700\\'\"\n", | ||
"KEY_DOWN_ALT = \"\\'\\\\uf701\\'\"\n", | ||
"KEY_LEFT_ALT = \"\\'\\\\uf702\\'\"\n", | ||
"KEY_RIGHT_ALT = \"\\'\\\\uf703\\'\"\n", | ||
"\n", | ||
"KEY_UP = \"'w'\"\n", | ||
"KEY_DOWN = \"'s'\"\n", | ||
"KEY_LEFT = \"'a'\"\n", | ||
"KEY_RIGHT = \"'d'\"\n", | ||
"KEY_BACK = \"'b'\"\n", | ||
"\n", | ||
"KEY_J = \"'j'\"\n", | ||
"KEY_K = \"'k'\"\n", | ||
"KEY_L = \"'l'\"\n", | ||
"KEY_H = \"'h'\"" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"importing Jupyter notebook from logic.ipynb\n", | ||
"importing Jupyter notebook from CONSTANTS.ipynb\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import random\n", | ||
"from tkinter import Frame, Label, CENTER\n", | ||
"\n", | ||
"import import_ipynb\n", | ||
"\n", | ||
"import logic\n", | ||
"\n", | ||
"import CONSTANTS as c\n", | ||
"\n", | ||
"\n", | ||
"class Game2048(Frame):\n", | ||
" def __init__(self):\n", | ||
" Frame.__init__(self)\n", | ||
"\n", | ||
" self.grid()\n", | ||
" self.master.title('2048')\n", | ||
" self.master.bind(\"<Key>\", self.key_down)\n", | ||
"\n", | ||
" # self.gamelogics = gamelogics\n", | ||
" self.commands = {c.KEY_UP: logic.move_up, c.KEY_DOWN: logic.move_down,\n", | ||
" c.KEY_LEFT: logic.move_left, c.KEY_RIGHT: logic.move_right,\n", | ||
" c.KEY_UP_ALT: logic.move_up, c.KEY_DOWN_ALT: logic.move_down,\n", | ||
" c.KEY_LEFT_ALT: logic.move_left, c.KEY_RIGHT_ALT: logic.move_right,\n", | ||
" c.KEY_H: logic.move_left, c.KEY_L: logic.move_right,\n", | ||
" c.KEY_K: logic.move_up, c.KEY_J: logic.move_down}\n", | ||
" \n", | ||
" self.grid_cells = []\n", | ||
" self.init_grid()\n", | ||
" self.init_matrix()\n", | ||
" self.update_grid_cells()\n", | ||
"\n", | ||
" self.mainloop()\n", | ||
"\n", | ||
" def init_grid(self):\n", | ||
" background = Frame(self, bg=c.BACKGROUND_COLOR_GAME,\n", | ||
" width=c.SIZE, height=c.SIZE)\n", | ||
" background.grid()\n", | ||
"\n", | ||
" for i in range(c.GRID_LEN):\n", | ||
" grid_row = []\n", | ||
" for j in range(c.GRID_LEN):\n", | ||
" cell = Frame(background, bg=c.BACKGROUND_COLOR_CELL_EMPTY,\n", | ||
" width=c.SIZE / c.GRID_LEN,\n", | ||
" height=c.SIZE / c.GRID_LEN)\n", | ||
" cell.grid(row=i, column=j, padx=c.GRID_PADDING,\n", | ||
" pady=c.GRID_PADDING)\n", | ||
" t = Label(master=cell, text=\"\",\n", | ||
" bg=c.BACKGROUND_COLOR_CELL_EMPTY,\n", | ||
" justify=CENTER, font=c.FONT, width=5, height=2)\n", | ||
" t.grid()\n", | ||
" grid_row.append(t)\n", | ||
"\n", | ||
" self.grid_cells.append(grid_row)\n", | ||
" \n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
" def init_matrix(self):\n", | ||
" self.matrix = logic.start_game()\n", | ||
" logic.add_new_2(self.matrix)\n", | ||
" logic.add_new_2(self.matrix)\n", | ||
" \n", | ||
" def update_grid_cells(self):\n", | ||
" for i in range(c.GRID_LEN):\n", | ||
" for j in range(c.GRID_LEN):\n", | ||
" new_number = self.matrix[i][j]\n", | ||
" if new_number == 0:\n", | ||
" self.grid_cells[i][j].configure(\n", | ||
" text=\"\", bg=c.BACKGROUND_COLOR_CELL_EMPTY)\n", | ||
" else:\n", | ||
" self.grid_cells[i][j].configure(text=str(\n", | ||
" new_number), bg=c.BACKGROUND_COLOR_DICT[new_number],\n", | ||
" fg=c.CELL_COLOR_DICT[new_number])\n", | ||
" self.update_idletasks()\n", | ||
"\n", | ||
" def key_down(self, event):\n", | ||
" key = repr(event.char)\n", | ||
" if key in self.commands:\n", | ||
" self.matrix, changed = self.commands[repr(event.char)](self.matrix)\n", | ||
" if changed:\n", | ||
" logic.add_new_2(self.matrix)\n", | ||
" self.update_grid_cells()\n", | ||
" changed = False\n", | ||
" if logic.get_current_state(self.matrix) == 'WON':\n", | ||
" self.grid_cells[1][1].configure(\n", | ||
" text=\"You\", bg=c.BACKGROUND_COLOR_CELL_EMPTY)\n", | ||
" self.grid_cells[1][2].configure(\n", | ||
" text=\"Win!\", bg=c.BACKGROUND_COLOR_CELL_EMPTY)\n", | ||
" if logic.get_current_state(self.matrix) == 'LOST':\n", | ||
" self.grid_cells[1][1].configure(\n", | ||
" text=\"You\", bg=c.BACKGROUND_COLOR_CELL_EMPTY)\n", | ||
" self.grid_cells[1][2].configure(\n", | ||
" text=\"Lose!\", bg=c.BACKGROUND_COLOR_CELL_EMPTY)\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"gamegrid = Game2048()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import random\n", | ||
"\n", | ||
"### 4* 4 matrix with 0(empty pos) in it\n", | ||
"\n", | ||
"def start_game():\n", | ||
" mat=[]\n", | ||
" for i in range(4):\n", | ||
" mat.append([0]*4)\n", | ||
" return mat \n", | ||
"\n", | ||
"### add random 2 at position which is empty or 0 \n", | ||
"\n", | ||
"def add_new_2(mat):\n", | ||
" r=random.randint(0,3) ##random row\n", | ||
" c=random.randint(0,3) ##radnom col\n", | ||
" while (mat[r][c]!=0): ### produce random row and col till we dont find an empty space\n", | ||
" r=random.randint(0,3)\n", | ||
" c=random.randint(0,3) \n", | ||
" mat[r][c]=2\n", | ||
" \n", | ||
"### current state of game , we need to check at every point what are possibilities\n", | ||
"### like we won , lost , game still can be played\n", | ||
"\n", | ||
"def get_current_state(mat): ### returns 3 things won ,lost ,still play\n", | ||
" for i in range(4):\n", | ||
" for j in range(4):\n", | ||
" if mat[i][j]==2048:\n", | ||
" return 'WON'\n", | ||
" ## if any o present\n", | ||
" for i in range(4):\n", | ||
" for j in range(4):\n", | ||
" if mat[i][j]==0:\n", | ||
" return 'GAME NOT OVER'\n", | ||
" ### evn if any adj cell has same no we are still in game\n", | ||
" ### every row and col except last row and column\n", | ||
" for i in range(3):\n", | ||
" for j in range(3):\n", | ||
" if (mat[i][j]==mat[i+1][j] or mat[i][j]==mat[i][j+1]):\n", | ||
" return 'GAME NOT OVER'\n", | ||
" ### last row\n", | ||
" for j in range(3):\n", | ||
" if mat[3][j]==mat[3][j+1]:\n", | ||
" return 'GAME NOT OVER'\n", | ||
" ### last col\n", | ||
" for i in range(3):\n", | ||
" if mat[i][3]==mat[i+1][3]:\n", | ||
" return 'GAME NOT OVER'\n", | ||
" ### lost\n", | ||
" return 'LOST'\n", | ||
"\n", | ||
"\n", | ||
"### COMPRESS FUNC--- ALL NON ZERO NO MOVES TO THE MOVEMENT SIDE\n", | ||
"def compress(mat):\n", | ||
" changed=False\n", | ||
" new_mat=[]\n", | ||
" for i in range(4):\n", | ||
" new_mat.append([0]*4)\n", | ||
" for i in range(4):\n", | ||
" pos=0\n", | ||
" for j in range(4):\n", | ||
" if mat[i][j]!=0:\n", | ||
" changed=True\n", | ||
" new_mat[i][pos]=mat[i][j]\n", | ||
" if j!=pos:\n", | ||
" changed=True\n", | ||
" pos+=1\n", | ||
" return new_mat,changed\n", | ||
" \n", | ||
"### merging two same consecutive/adj no\n", | ||
"def merge(mat):\n", | ||
" changed=False\n", | ||
" for i in range(4):\n", | ||
" for j in range(3):\n", | ||
" if mat[i][j]==mat[i][j+1] and mat[i][j]!=0:\n", | ||
" changed=True\n", | ||
" mat[i][j]=mat[i][j]*2\n", | ||
" mat[i][j+1]=0 ###vvi\n", | ||
" return mat,changed\n", | ||
" \n", | ||
" \n", | ||
"\n", | ||
"### replaing a matrix\n", | ||
"def reverse(mat):\n", | ||
" new_mat=[]\n", | ||
" for i in range(4):\n", | ||
" new_mat.append([])\n", | ||
" for j in range(4):\n", | ||
" new_mat[i].append(mat[i][4-j-1])\n", | ||
" \n", | ||
" return new_mat\n", | ||
"\n", | ||
"###transpose\n", | ||
"def transpose(mat):\n", | ||
" new_mat=[]\n", | ||
" for i in range(4):\n", | ||
" new_mat.append([])\n", | ||
" for j in range(4):\n", | ||
" new_mat[i].append(mat[j][i])\n", | ||
" return new_mat\n", | ||
"\n", | ||
"\n", | ||
"### FOCUS ON LEFT ONLY ADN FOR OTHER MOVEMENTS WE WILL GENERALIZE\n", | ||
"### left---- comp,merge,compree\n", | ||
"### right--- rev ,comp,merge,compree,rev\n", | ||
"### up-- tanspose, comp,merge,compree,transpose\n", | ||
"### down -- tanspose,rev, comp,merge,compree,rev,transpose\n", | ||
"\n", | ||
"\n", | ||
"### IF ANY CHANGE OR MOVEMENT OR COMPRESSION OR MERGING HAPPENED THEN ONLY NEW RANDOM IS ADDED\n", | ||
"### SO WITH RETURNING MAT WE ALSO NEED TO RETURN IF SOME CHANGE HAPPEND OR NOT\n", | ||
" \n", | ||
"def move_up(grid):\n", | ||
" transpose_grid=transpose(grid)\n", | ||
" new_grid,changed1=compress(transpose_grid)\n", | ||
" new_grid,changed2=merge(new_grid)\n", | ||
" changed=changed1 or changed2\n", | ||
" new_grid,temp=compress(new_grid)\n", | ||
" final_grid=transpose(new_grid)\n", | ||
" return final_grid,changed\n", | ||
" \n", | ||
"def move_down(grid):\n", | ||
" transpose_grid=transpose(grid)\n", | ||
" reverse_grid=reverse(transpose_grid)\n", | ||
" new_grid,changed1=compress(reverse_grid)\n", | ||
" new_grid,changed2=merge(new_grid)\n", | ||
" changed=changed1 or changed2\n", | ||
" new_grid,temp=compress(new_grid)\n", | ||
" new_grid=reverse(new_grid)\n", | ||
" final_grid=transpose(new_grid)\n", | ||
" return final_grid,changed\n", | ||
"\n", | ||
"def move_left(grid):\n", | ||
" new_grid,changed1=compress(grid)\n", | ||
" new_grid,changed2=merge(new_grid)\n", | ||
" changed=changed1 or changed2\n", | ||
" new_grid,temp=compress(new_grid)\n", | ||
" return new_grid,changed\n", | ||
"\n", | ||
"def move_right(grid):\n", | ||
" reverse_grid=reverse(grid)\n", | ||
" new_grid,changed1=compress(reverse_grid)\n", | ||
" new_grid,changed2=merge(new_grid)\n", | ||
" changed=changed1 or changed2\n", | ||
" new_grid,temp=compress(new_grid)\n", | ||
" final_grid=reverse(new_grid)\n", | ||
" return final_grid,changed\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |