Created
January 18, 2018 09:51
-
-
Save soleshka/ca485de6a6f6715454dd49c12fe4f288 to your computer and use it in GitHub Desktop.
2048 generic game
This file contains hidden or 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
| """ | |
| Clone of 2048 game. | |
| To be used at : codeskulptor.org | |
| Change board size at :poc_2048_gui.run_gui(TwentyFortyEight(2, 2)) | |
| """ | |
| import poc_2048_gui | |
| import random | |
| # Directions, DO NOT MODIFY | |
| UP = 1 | |
| DOWN = 2 | |
| LEFT = 3 | |
| RIGHT = 4 | |
| # Offsets for computing tile indices in each direction. | |
| # DO NOT MODIFY this dictionary. | |
| OFFSETS = {UP: (1, 0), | |
| DOWN: (-1, 0), | |
| LEFT: (0, 1), | |
| RIGHT: (0, -1)} | |
| def first_indx_non_zero(list_to_check): | |
| """Find first index value for which is not zero""" | |
| for dummy_x,dummy_y in enumerate(list_to_check): | |
| if list_to_check[dummy_x] != 0 : | |
| return dummy_x | |
| def merge(line): | |
| """ | |
| merges line recursively | |
| x_left is the index of left merge_two_value_line | |
| x_right is the index of right value | |
| """ | |
| #copy list , not changing the original | |
| line1 = list(line) | |
| #check size bigger than 1 | |
| done = False | |
| x_left = 0 | |
| if len(line1)>1: | |
| x_right = 1 | |
| else: | |
| done = True | |
| #until fully merged : done | |
| while not done: | |
| if x_right+1 <= len(line1): #if not end of line(x_right) | |
| #case left value differs zero | |
| if line1[x_left]!=0: #2024 0,1 , 2204 , 0,1 , 4004 , 1,2 , 4400 | |
| #right value is zero | |
| if line1[x_right] == 0: | |
| indx = first_indx_non_zero(line1[x_right:]) | |
| if not indx: | |
| done = True | |
| else: | |
| line1[x_right], line1[x_right+indx] = line1[indx+x_right],\ | |
| line1[x_right]#change_places(x_right,new_place) | |
| #right value is not zero and different | |
| elif line1[x_right] != line1[x_left]: | |
| x_right += 1 | |
| x_left += 1 | |
| #right value non zero and same | |
| elif line1[x_right] == line1[x_left]: | |
| line1[x_left] *= 2 | |
| line1[x_right] = 0 | |
| x_left += 1 | |
| x_right += 1 | |
| #case left value is zero | |
| elif line1[x_left] == 0: | |
| #find first non zero and exchange | |
| indx = first_indx_non_zero(line1[x_left:]) | |
| if not indx: | |
| done = True | |
| else: | |
| line1[x_left], line1[x_left+indx] = line1[x_left+indx], line1[x_left]#change_places(x_right,new_place) | |
| #print(line) | |
| else: | |
| done = True | |
| return line1 | |
| class TwentyFortyEight: | |
| """ | |
| Class to run the game logic. | |
| """ | |
| def __init__(self, grid_height, grid_width): | |
| # replace with your code | |
| self._gridheight1_ = grid_height | |
| self._gridwidth1_ = grid_width | |
| #list to follow up empty spaces | |
| self._empty_ = [] | |
| for col in range(self._gridwidth1_): | |
| for row in range(self._gridheight1_): | |
| self._empty_.append((row,col)) | |
| self.reset() | |
| self._initile1_ = { UP:[], | |
| DOWN:[], | |
| LEFT:[], | |
| RIGHT:[]} | |
| self._traverse_length_ = { UP:self._gridheight1_, | |
| DOWN:self._gridheight1_, | |
| LEFT:self._gridwidth1_, | |
| RIGHT:self._gridwidth1_} | |
| for col_ptr in range(self._gridwidth1_): | |
| self._initile1_[UP].append((0,col_ptr)) | |
| self._initile1_[DOWN].append((self._gridheight1_-1,col_ptr)) | |
| for row_ptr in range(self._gridheight1_): | |
| self._initile1_[LEFT].append((row_ptr,0)) | |
| self._initile1_[RIGHT].append((row_ptr,self._gridwidth1_-1)) | |
| print self._initile1_ | |
| def reset(self): | |
| """ | |
| Reset the game so the grid is empty except for two | |
| initial tiles. | |
| """ | |
| # replace with your code | |
| self._grid1_ = [[0 for dummy_col in range(self._gridwidth1_)] | |
| for dummy_row in range(self._gridheight1_)] | |
| self.new_tile() | |
| self.new_tile() | |
| def __str__(self): | |
| """ | |
| Return a string representation of the grid for debugging. | |
| """ | |
| str_val = "" | |
| for row_grid in range(self._gridheight1_): | |
| str_val += "\n"+'_'.join(str(x) for x in self._grid1_[row_grid]) | |
| return str_val | |
| def get_grid_height(self): | |
| """ | |
| Get the height of the board. | |
| """ | |
| # replace with your code | |
| return self._gridheight1_ | |
| def get_grid_width(self): | |
| """ | |
| Get the width of the board. | |
| """ | |
| # replace with your code | |
| return self._gridwidth1_ | |
| def move(self, direction): | |
| """ | |
| Move all tiles in the given direction and add | |
| a new tile if any tiles moved. | |
| """ | |
| add_new_tile=False | |
| #tile_n: (0,1) | |
| #print "self.initile1[direction]",self.initile1[direction] | |
| for tile_n in self._initile1_[direction]: | |
| line_x = list() | |
| #next_indx : 0,1,2,3 | |
| #geting line for spesific verticl / diagonal line | |
| for next_indx in range(self._traverse_length_[direction]): | |
| next_row = tile_n[0]+next_indx*OFFSETS[direction][0] | |
| next_col = tile_n[1]+next_indx*OFFSETS[direction][1] | |
| line_x.append(self._grid1_[next_row][next_col]) | |
| #print "line_x: ",line_x | |
| #merging line | |
| line_x_merged = merge(line_x) | |
| #print "line_x_merged: ",line_x_merged | |
| for next_indx in range(self._traverse_length_[direction]): | |
| next_row = tile_n[0]+next_indx*OFFSETS[direction][0] | |
| next_col = tile_n[1]+next_indx*OFFSETS[direction][1] | |
| self._grid1_[next_row][next_col] = line_x_merged[next_indx] | |
| if line_x_merged[next_indx] != line_x[next_indx]: | |
| add_new_tile = True | |
| #print self.grid1 | |
| #add new tile if structure changed | |
| if add_new_tile: | |
| self.new_tile() | |
| def new_tile(self): | |
| """ | |
| Create a new tile in a randomly selected empty | |
| square. The tile should be 2 90% of the time and | |
| 4 10% of the time. | |
| """ | |
| val_list = [2,2,2,2,2,2,2,2,2,4] | |
| indx = random.randrange(len(val_list)) | |
| self._empty_ = [] | |
| for col in range(self._gridwidth1_): | |
| for row in range(self._gridheight1_): | |
| if self._grid1_[row][col] == 0: | |
| self._empty_.append((row,col)) | |
| print "Empty places:",self._empty_ | |
| print "val indx:",indx | |
| if not len(self._empty_): | |
| #no more place | |
| #return empty flag | |
| print "There is no space left" | |
| else: | |
| #randomize which tile to occupy | |
| place = random.randrange(len(self._empty_)) | |
| print "place:\t" , place | |
| self._grid1_[self._empty_[place][0]][self._empty_[place][1]]=val_list[indx] | |
| self._empty_.pop(place) | |
| print "grid after:",self._grid1_ | |
| print "\n" | |
| def set_tile(self, row, col, value): | |
| """ | |
| Set the tile at position row, col to have the given value. | |
| """ | |
| # sanity check | |
| if row >= 0 and col >= 0: | |
| self._grid1_[row][col] = value | |
| else: | |
| print "Row or Col out of range",row,col | |
| def get_tile(self, row, col): | |
| """ | |
| Return the value of the tile at position row, col. | |
| """ | |
| if row >= 0 and col >= 0: | |
| return self._grid1_[row][col] | |
| else: | |
| print "Row or Col out of range",row,col | |
| return None | |
| poc_2048_gui.run_gui(TwentyFortyEight(2, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment