|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "\n", |
| 8 | + "# 2048 Game<br>\n", |
| 9 | + "\n", |
| 10 | + "2048 is a game and you are expected to implement the move function for this game.<br>\n", |
| 11 | + "Arguments passed to the four functions is the matrix which we are using for 2048<br>\n", |
| 12 | + "The move function will be returning new matrix after moving the corresponding move.<br>\n", |
| 13 | + "Implement All The Four Moves Using These Functions -<br>\n", |
| 14 | + "1. move_up<br>\n", |
| 15 | + "2. move_down<br>\n", |
| 16 | + "3. move_left<br>\n", |
| 17 | + "4. move_right" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": 1, |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [ |
| 25 | + { |
| 26 | + "name": "stdout", |
| 27 | + "output_type": "stream", |
| 28 | + "text": [ |
| 29 | + "1 2 4 3 2 4 3 1 4\n", |
| 30 | + "[[4, 4, 2, 2], [0, 8, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]\n", |
| 31 | + "[[0, 0, 0, 0], [0, 0, 0, 0], [0, 4, 0, 0], [4, 8, 2, 2]]\n", |
| 32 | + "[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 4], [0, 4, 8, 4]]\n", |
| 33 | + "[[0, 0, 0, 0], [0, 0, 0, 0], [4, 0, 0, 0], [4, 8, 4, 0]]\n", |
| 34 | + "[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [8, 8, 4, 0]]\n", |
| 35 | + "[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 16, 4]]\n", |
| 36 | + "[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [16, 4, 0, 0]]\n", |
| 37 | + "[[16, 4, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]\n", |
| 38 | + "[[0, 0, 16, 4], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]\n" |
| 39 | + ] |
| 40 | + } |
| 41 | + ], |
| 42 | + "source": [ |
| 43 | + "import random\n", |
| 44 | + "def start_game():\n", |
| 45 | + " mat = []\n", |
| 46 | + " for i in range(4):\n", |
| 47 | + " mat.append([0]*4)\n", |
| 48 | + " return mat\n", |
| 49 | + "\n", |
| 50 | + "def compress(mat):\n", |
| 51 | + " new_mat = []\n", |
| 52 | + " for i in range(4):\n", |
| 53 | + " new_mat.append([0] * 4)\n", |
| 54 | + " for i in range(4):\n", |
| 55 | + " pos = 0\n", |
| 56 | + " for j in range(4):\n", |
| 57 | + " if mat[i][j] != 0:\n", |
| 58 | + " new_mat[i][pos] = mat[i][j]\n", |
| 59 | + " pos = pos + 1\n", |
| 60 | + " return new_mat\n", |
| 61 | + "\n", |
| 62 | + "def merge(mat):\n", |
| 63 | + " for i in range(4):\n", |
| 64 | + " for j in range(3):\n", |
| 65 | + " if mat[i][j] == mat[i][j + 1]:\n", |
| 66 | + " mat[i][j] = (mat[i][j]) * 2\n", |
| 67 | + " mat[i][j + 1] = 0\n", |
| 68 | + " return mat\n", |
| 69 | + "\n", |
| 70 | + "def reverse(mat):\n", |
| 71 | + " new_mat = []\n", |
| 72 | + " for i in range(4):\n", |
| 73 | + " new_mat.append(mat[i][::-1])\n", |
| 74 | + " return new_mat\n", |
| 75 | + "\n", |
| 76 | + "def transpose(mat):\n", |
| 77 | + " new_mat = []\n", |
| 78 | + " for i in range(4):\n", |
| 79 | + " new_mat.append([])\n", |
| 80 | + " for j in range(4):\n", |
| 81 | + " new_mat[i].append(mat[j][i])\n", |
| 82 | + " return new_mat\n", |
| 83 | + "\n", |
| 84 | + "def move_up(grid):\n", |
| 85 | + " transposed_grid = transpose(grid)\n", |
| 86 | + " new_grid = compress(transposed_grid)\n", |
| 87 | + " new_grid = merge(new_grid)\n", |
| 88 | + " new_grid = compress(new_grid)\n", |
| 89 | + " final_grid = transpose(new_grid)\n", |
| 90 | + " return final_grid\n", |
| 91 | + "\n", |
| 92 | + "def move_down(grid):\n", |
| 93 | + " transposed_grid = transpose(grid)\n", |
| 94 | + " reverse_grid = reverse(transposed_grid)\n", |
| 95 | + " new_grid = compress(reverse_grid)\n", |
| 96 | + " new_grid = merge(new_grid)\n", |
| 97 | + " new_grid = compress(new_grid)\n", |
| 98 | + " new_reverse = reverse(new_grid)\n", |
| 99 | + " final_grid = transpose(new_reverse)\n", |
| 100 | + " return final_grid\n", |
| 101 | + "\n", |
| 102 | + "def move_right(grid):\n", |
| 103 | + " reverse_grid = reverse(grid)\n", |
| 104 | + " new_grid = compress(reverse_grid)\n", |
| 105 | + " new_grid = merge(new_grid)\n", |
| 106 | + " new_grid = compress(new_grid)\n", |
| 107 | + " final_grid = reverse(new_grid)\n", |
| 108 | + " return final_grid\n", |
| 109 | + "\n", |
| 110 | + "def move_left(grid):\n", |
| 111 | + " new_grid = compress(grid)\n", |
| 112 | + " new_grid = merge(new_grid)\n", |
| 113 | + " new_grid = compress(new_grid)\n", |
| 114 | + " return new_grid\n", |
| 115 | + "\n", |
| 116 | + "\n", |
| 117 | + "mat = start_game()\n", |
| 118 | + "mat[1][3] = 2\n", |
| 119 | + "mat[2][2] = 2\n", |
| 120 | + "mat[3][0] = 4\n", |
| 121 | + "mat[3][1] = 8\n", |
| 122 | + "mat[2][1] = 4\n", |
| 123 | + "\n", |
| 124 | + "inputs = [int(ele) for ele in input().split()]\n", |
| 125 | + "for ele in inputs:\n", |
| 126 | + " if ele == 1:\n", |
| 127 | + " mat = move_up(mat)\n", |
| 128 | + " elif ele == 2:\n", |
| 129 | + " mat = move_down(mat)\n", |
| 130 | + " elif ele == 3:\n", |
| 131 | + " mat = move_left(mat)\n", |
| 132 | + " else:\n", |
| 133 | + " mat = move_right(mat)\n", |
| 134 | + " print(mat)" |
| 135 | + ] |
| 136 | + } |
| 137 | + ], |
| 138 | + "metadata": { |
| 139 | + "kernelspec": { |
| 140 | + "display_name": "Python 3", |
| 141 | + "language": "python", |
| 142 | + "name": "python3" |
| 143 | + }, |
| 144 | + "language_info": { |
| 145 | + "codemirror_mode": { |
| 146 | + "name": "ipython", |
| 147 | + "version": 3 |
| 148 | + }, |
| 149 | + "file_extension": ".py", |
| 150 | + "mimetype": "text/x-python", |
| 151 | + "name": "python", |
| 152 | + "nbconvert_exporter": "python", |
| 153 | + "pygments_lexer": "ipython3", |
| 154 | + "version": "3.8.6" |
| 155 | + } |
| 156 | + }, |
| 157 | + "nbformat": 4, |
| 158 | + "nbformat_minor": 4 |
| 159 | +} |
0 commit comments