|
| 1 | +# logic.py to be |
| 2 | +# imported in the 2048.py file |
| 3 | + |
| 4 | +# importing random package |
| 5 | +# for methods to generate random |
| 6 | +# numbers. |
| 7 | +import random |
| 8 | + |
| 9 | +# function to initialize game / grid |
| 10 | +# at the start |
| 11 | +def start_game(): |
| 12 | + |
| 13 | + # declaring an empty list then |
| 14 | + # appending 4 list each with four |
| 15 | + # elements as 0. |
| 16 | + mat =[] |
| 17 | + for i in range(4): |
| 18 | + mat.append([0] * 4) |
| 19 | + |
| 20 | + # printing controls for user |
| 21 | + print("Commands are as follows : ") |
| 22 | + print("'W' or 'w' : Move Up") |
| 23 | + print("'S' or 's' : Move Down") |
| 24 | + print("'A' or 'a' : Move Left") |
| 25 | + print("'D' or 'd' : Move Right") |
| 26 | + |
| 27 | + # calling the function to add |
| 28 | + # a new 2 in grid after every step |
| 29 | + add_new_2(mat) |
| 30 | + return mat |
| 31 | + |
| 32 | +# function to add a new 2 in |
| 33 | +# grid at any random empty cell |
| 34 | +def add_new_2(mat): |
| 35 | + |
| 36 | +# choosing a random index for |
| 37 | +# row and column. |
| 38 | + r = random.randint(0, 3) |
| 39 | + c = random.randint(0, 3) |
| 40 | + |
| 41 | + # while loop will break as the |
| 42 | + # random cell chosen will be empty |
| 43 | + # (or contains zero) |
| 44 | + while(mat[r] != 0): |
| 45 | + r = random.randint(0, 3) |
| 46 | + c = random.randint(0, 3) |
| 47 | + |
| 48 | + # we will place a 2 at that empty |
| 49 | + # random cell. |
| 50 | + mat[r] = 2 |
| 51 | + |
| 52 | +# function to get the current |
| 53 | +# state of game |
| 54 | +def get_current_state(mat): |
| 55 | + |
| 56 | + # if any cell contains |
| 57 | + # 2048 we have won |
| 58 | + for i in range(4): |
| 59 | + for j in range(4): |
| 60 | + if(mat[i][j]== 2048): |
| 61 | + return 'WON' |
| 62 | + |
| 63 | + # if we are still left with |
| 64 | + # atleast one empty cell |
| 65 | + # game is not yet over |
| 66 | + for i in range(4): |
| 67 | + for j in range(4): |
| 68 | + if(mat[i][j]== 0): |
| 69 | + return 'GAME NOT OVER' |
| 70 | + |
| 71 | + # or if no cell is empty now |
| 72 | + # but if after any move left, right, |
| 73 | + # up or down, if any two cells |
| 74 | + # gets merged and create an empty |
| 75 | + # cell then also game is not yet over |
| 76 | + for i in range(3): |
| 77 | + for j in range(3): |
| 78 | + if(mat[i][j]== mat[i + 1][j] or mat[i][j]== mat[i][j + 1]): |
| 79 | + return 'GAME NOT OVER' |
| 80 | + |
| 81 | + for j in range(3): |
| 82 | + if(mat[3][j]== mat[3][j + 1]): |
| 83 | + return 'GAME NOT OVER' |
| 84 | + |
| 85 | + for i in range(3): |
| 86 | + if(mat[i][3]== mat[i + 1][3]): |
| 87 | + return 'GAME NOT OVER' |
| 88 | + |
| 89 | + # else we have lost the game |
| 90 | + return 'LOST' |
| 91 | + |
| 92 | +# all the functions defined below |
| 93 | +# are for left swap initially. |
| 94 | + |
| 95 | +# function to compress the grid |
| 96 | +# after every step before and |
| 97 | +# after merging cells. |
| 98 | +def compress(mat): |
| 99 | + |
| 100 | + # bool variable to determine |
| 101 | + # any change happened or not |
| 102 | + changed = False |
| 103 | + |
| 104 | + # empty grid |
| 105 | + new_mat = [] |
| 106 | + |
| 107 | + # with all cells empty |
| 108 | + for i in range(4): |
| 109 | + new_mat.append([0] * 4) |
| 110 | + |
| 111 | + # here we will shift entries |
| 112 | + # of each cell to it's extreme |
| 113 | + # left row by row |
| 114 | + # loop to traverse rows |
| 115 | + for i in range(4): |
| 116 | + pos = 0 |
| 117 | + |
| 118 | + # loop to traverse each column |
| 119 | + # in respective row |
| 120 | + for j in range(4): |
| 121 | + if(mat[i][j] != 0): |
| 122 | + |
| 123 | + # if cell is non empty then |
| 124 | + # we will shift it's number to |
| 125 | + # previous empty cell in that row |
| 126 | + # denoted by pos variable |
| 127 | + new_mat[i][pos] = mat[i][j] |
| 128 | + |
| 129 | + if(j != pos): |
| 130 | + changed = True |
| 131 | + pos += 1 |
| 132 | + |
| 133 | + # returning new compressed matrix |
| 134 | + # and the flag variable. |
| 135 | + return new_mat, changed |
| 136 | + |
| 137 | +# function to merge the cells |
| 138 | +# in matrix after compressing |
| 139 | +def merge(mat): |
| 140 | + |
| 141 | + changed = False |
| 142 | + |
| 143 | + for i in range(4): |
| 144 | + for j in range(3): |
| 145 | + |
| 146 | + # if current cell has same value as |
| 147 | + # next cell in the row and they |
| 148 | + # are non empty then |
| 149 | + if(mat[i][j] == mat[i][j + 1] and mat[i][j] != 0): |
| 150 | + |
| 151 | + # double current cell value and |
| 152 | + # empty the next cell |
| 153 | + mat[i][j] = mat[i][j] * 2 |
| 154 | + mat[i][j + 1] = 0 |
| 155 | + |
| 156 | + # make bool variable True indicating |
| 157 | + # the new grid after merging is |
| 158 | + # different. |
| 159 | + changed = True |
| 160 | + |
| 161 | + return mat, changed |
| 162 | + |
| 163 | +# function to reverse the matrix |
| 164 | +# means reversing the content of |
| 165 | +# each row (reversing the sequence) |
| 166 | +def reverse(mat): |
| 167 | + new_mat =[] |
| 168 | + for i in range(4): |
| 169 | + new_mat.append([]) |
| 170 | + for j in range(4): |
| 171 | + new_mat[i].append(mat[i][3 - j]) |
| 172 | + return new_mat |
| 173 | + |
| 174 | +# function to get the transpose |
| 175 | +# of matrix means interchanging |
| 176 | +# rows and column |
| 177 | +def transpose(mat): |
| 178 | + new_mat = [] |
| 179 | + for i in range(4): |
| 180 | + new_mat.append([]) |
| 181 | + for j in range(4): |
| 182 | + new_mat[i].append(mat[j][i]) |
| 183 | + return new_mat |
| 184 | + |
| 185 | +# function to update the matrix |
| 186 | +# if we move / swipe left |
| 187 | +def move_left(grid): |
| 188 | + |
| 189 | + # first compress the grid |
| 190 | + new_grid, changed1 = compress(grid) |
| 191 | + |
| 192 | + # then merge the cells. |
| 193 | + new_grid, changed2 = merge(new_grid) |
| 194 | + |
| 195 | + changed = changed1 or changed2 |
| 196 | + |
| 197 | + # again compress after merging. |
| 198 | + new_grid, temp = compress(new_grid) |
| 199 | + |
| 200 | + # return new matrix and bool changed |
| 201 | + # telling whether the grid is same |
| 202 | + # or different |
| 203 | + return new_grid, changed |
| 204 | + |
| 205 | +# function to update the matrix |
| 206 | +# if we move / swipe right |
| 207 | +def move_right(grid): |
| 208 | + |
| 209 | + # to move right we just reverse |
| 210 | + # the matrix |
| 211 | + new_grid = reverse(grid) |
| 212 | + |
| 213 | + # then move left |
| 214 | + new_grid, changed = move_left(new_grid) |
| 215 | + |
| 216 | + # then again reverse matrix will |
| 217 | + # give us desired result |
| 218 | + new_grid = reverse(new_grid) |
| 219 | + return new_grid, changed |
| 220 | + |
| 221 | +# function to update the matrix |
| 222 | +# if we move / swipe up |
| 223 | +def move_up(grid): |
| 224 | + |
| 225 | + # to move up we just take |
| 226 | + # transpose of matrix |
| 227 | + new_grid = transpose(grid) |
| 228 | + |
| 229 | + # then move left (calling all |
| 230 | + # included functions) then |
| 231 | + new_grid, changed = move_left(new_grid) |
| 232 | + |
| 233 | + # again take transpose will give |
| 234 | + # desired results |
| 235 | + new_grid = transpose(new_grid) |
| 236 | + return new_grid, changed |
| 237 | + |
| 238 | +# function to update the matrix |
| 239 | +# if we move / swipe down |
| 240 | +def move_down(grid): |
| 241 | + |
| 242 | + # to move down we take transpose |
| 243 | + new_grid = transpose(grid) |
| 244 | + |
| 245 | + # move right and then again |
| 246 | + new_grid, changed = move_right(new_grid) |
| 247 | + |
| 248 | + # take transpose will give desired |
| 249 | + # results. |
| 250 | + new_grid = transpose(new_grid) |
| 251 | + return new_grid, changed |
| 252 | + |
| 253 | +# this file only contains all the logic |
| 254 | +# functions to be called in main function |
| 255 | +# present in the other file |
0 commit comments