""" 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. """ values = [2, 2, 2, 2, 2, 2, 2, 2, 2, 4] rand_row = random.randint(0, self.get_grid_height() - 1) rand_col = random.randint(0, self.get_grid_width() - 1) while self._grid[rand_row][rand_col] != 0: rand_row = random.randint(0, self.get_grid_height() - 1) rand_col = random.randint(0, self.get_grid_width() - 1) self._grid[rand_row][rand_col] = random.choice(values) def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ self._grid[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ return self._grid[row][col] M_G9_ = TwentyFortyEight(2, 3) poc_2048_gui.run_gui(M_G9_) print str(M_G9_)
""" 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. """ rand_tile_choice = randint(1, 10) if rand_tile_choice == 1: tile = 4 else: tile = 2 rand_row_index = randint(0, self._grid_height-1) rand_col_index = randint(0, self._grid_width-1) if self._board[rand_row_index][rand_col_index] == 0: self._board[rand_row_index][rand_col_index] = tile else: self.new_tile() def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ self._board[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ return self._board[row][col] poc_2048_gui.run_gui(TwentyFortyEight(6, 6))
self.set_tile(x_pos, y_pos, tile_value) def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ self.grid[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ return self.grid[row][col] poc_2048_gui.run_gui(TwentyFortyEight(3, 3)) ##user34_cMJVLVBRqj_5.run_test(merge) # merge test #my_board = TwentyFortyEight(4,5) #my_board.grid = [] #my_board.grid.insert(0, [8, 16, 8, 16, 8]) #my_board.grid.insert(1, [16, 8, 16, 8, 16]) #my_board.grid.insert(2, [8, 16, 8, 16, 8]) #my_board.grid.insert(3, [16, 8, 16, 8, 16]) ##my_board.grid.insert(0, [2,2,2,2]) ##my_board.grid.insert(1, [2,0,2,2]) ##my_board.grid.insert(2, [2,4,4,2]) ##my_board.grid.insert(0, [0,0,2,2]) #print my_board #my_board.move(LEFT) #print my_board
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. """ zero_cells = [] for row in range(self.height): for col in range(self.width): if self.grid[row][col] == 0: zero_cells.append((row, col)) random_cell = random.choice(zero_cells) row, col = random_cell if int(random.random() * 10) == 0: self.grid[row][col] = 4 else: self.grid[row][col] = 2 def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ self.grid[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ return self.grid[row][col] a_grid = TwentyFortyEight(4, 4) poc_2048_gui.run_gui(a_grid) print(a_grid)
text = "Moves: %i" % GAME_ENGINE.get_moves() canvas.draw_text( text, ( 151, 32 ), 24, "black" ) canvas.draw_text( text, ( 150, 31 ), 24, "lime" ) text = "Max tile: %i" % GAME_ENGINE.get_max_tile() canvas.draw_text( text, ( 301, 32 ), 24, "black" ) canvas.draw_text( text, ( 300, 31 ), 24, "white" ) if GAME_ENGINE.is_game_over(): text = "<<<Game Over>>>" canvas.draw_text( text, ( 52, 202 ), 48, "black" ) canvas.draw_text( text, ( 50, 200 ), 48, "#ee5" ) # overwrite painter function poc_2048_gui.GUI.draw = draw ###################################################### ## start game gui ###################################################### poc_2048_gui.run_gui( GAME_ENGINE ) #Math notes: # n = WIDTH * HEIGHT # - max possible tile = 2^(n+1) # GUI #http://www.codeskulptor.org/#poc_2048_gui.py
col = random.randrange(0, self.get_grid_width()) # if there's no zero anywhere on the grid AND # no adjacent equal tiles: #then game over... if self.get_tile(row, col) == 0: if random.random() <= 0.9: self._tile = 2 self.set_tile(row, col, self._tile) else: self._tile = 4 self.set_tile(row, col, self._tile) else: self.new_tile() #This might not be the best method? #Maybe set a MAX recursion depth limit of (Height x Width)? def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ self._grid[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ return self._grid[row][col] RICE = TwentyFortyEight(4, 4) poc_2048_gui.run_gui(RICE)
""" # Select the value of the tile new_tile_in_play = random.choice([2,2,2,2,2,2,2,2,4]) # Search all the empty tiles available zero_pos_list = [] for row in range(self.get_grid_height()): for col in range(self.get_grid_width()): if self.tiles[row][col] == 0: zero_pos_list.append((row, col)) # Select a random tile from the tuples list selected_zero_pos = random.choice(zero_pos_list) # Set the random value self.set_tile(selected_zero_pos[0], selected_zero_pos[1], new_tile_in_play) def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ self.tiles[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ return self.tiles[row][col] poc_2048_gui.run_gui(TwentyFortyEight(5, 6))
c_somthing = random.randint(0, self.grid_width - 1) r_somthing = random.randint(0, self.grid_height - 1) check_repeat.append((r_somthing, c_somthing)) if len(check_repeat) >= self.grid_width * self.grid_height: stop = 1 break if stop == 0: self.cells[r_somthing][c_somthing] = random.choice([2] * 9 + [4] * 1) elif stop == 1: return def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ # replace with your code self.cells[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ # replace with your code self.value = self.cells[row][col] return self.value poc_2048_gui.run_gui(TwentyFortyEight(3, 3))
number = random.choice(numbers) column = random.randrange(0, self._grid_width) row = random.randrange(0, self._grid_height) if 0 in [ num for elem in TwentyFortyEight.EXAMPLE_GRID for num in elem ]: if (TwentyFortyEight.EXAMPLE_GRID[row][column] == 0): TwentyFortyEight.EXAMPLE_GRID[row][column] = number else: self.new_tile() else: pass def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ # replace with your code TwentyFortyEight.EXAMPLE_GRID[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ # replace with your code return TwentyFortyEight.EXAMPLE_GRID[row][col] poc_2048_gui.run_gui(TwentyFortyEight(5, 5))
if self.initial_board[new_tile_row][new_tile_column] == 0: self.initial_board[new_tile_row][new_tile_column] = new_tile else: self.new_tile() def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ self.initial_board[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ return self.initial_board[row][col] board = TwentyFortyEight(4, 4) poc_2048_gui.run_gui(board) print board print board.indicies_dict initial_tiles = board.indicies_dict[3] print len(initial_tiles) print board.indicies_dict[1][1][0]
rand_row = randint(0, self._height-1) rand_col = randint(0, self._width-1) if self._grid[rand_row][rand_col] != 0: continue else: check = True print rand_col, rand_row, rand_seed if rand_seed < 10: self._grid[rand_row][rand_col] = 2 else: self._grid[rand_row][rand_col] = 4 #print self def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ # replace with your code self._grid[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ # replace with your code return self._grid[row][col] poc_2048_gui.run_gui(TwentyFortyEight(2, 2))
obj.set_tile(0, 1, 4) obj.set_tile(0, 2, 8) obj.set_tile(0, 3, 16) obj.set_tile(1, 0, 16) obj.set_tile(1, 1, 8) obj.set_tile(1, 2, 4) obj.set_tile(1, 3, 2) obj.set_tile(2, 0, 0) obj.set_tile(2, 1, 0) obj.set_tile(2, 2, 8) obj.set_tile(2, 3, 16) obj.set_tile(3, 0, 0) obj.set_tile(3, 1, 0) obj.set_tile(3, 2, 4) obj.set_tile(3, 3, 2) obj.move(UP) alist = [[2, 4, 8, 16], [16, 8, 4, 2], [0, 0, 8, 16], [0, 0, 4, 2]] if cmp_list(obj._grid, alist): print "Passed " else: print obj print "Expected: \n" + str([row for row in alist]).replace("],", "]\n") sys.exit(1) run_first_test(5, 4, 20) run_second_test() TFE = TwentyFortyEight(4, 4) poc_2048_gui.run_gui(TFE)
random.randrange(0, self.height), random.randrange(0, self.width) ] if self.grid[rand_square[0]][rand_square[1]] == 0: break #set that square to either 2 or 4 seed = random.randint(1, 10) if seed == 1: tile_value = 4 else: tile_value = 2 self.grid[rand_square[0]][rand_square[1]] = tile_value def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ # replace with your code self.grid[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ return self.grid[row][col] game = TwentyFortyEight(4, 5) poc_2048_gui.run_gui(game) print game
def main(): new_game = TwentyFortyEight(4,4) poc_2048_gui.run_gui(new_game)
if self._grid[row][col] == 0: zeros.append((row, col)) return zeros def merge_all(self, direction): """ Call merge for all gird """ if direction == UP or direction == DOWN: step = self.get_grid_height() else: step = self.get_grid_width() #print('step' ,step) for val in self._initial_val[direction]: temp_lst = [] #print('val : ',val) row, col = OFFSETS[direction] for num in range(step): #print(val[0]+x,val[1]+x) temp_lst.append( self.get_tile(val[0] + (num * row), val[1] + (num * col))) #print("BF : ",temp_lst) temp_lst = merge(temp_lst) #print("AF : ",temp_lst) for num in range(step): self.set_tile(val[0] + (row * num), val[1] + (col * num), temp_lst[num]) poc_2048_gui.run_gui(TwentyFortyEight(ROWS, COLS))
if self._grid1_[row][col] == 0: self._grid1_[row][col] = new_value tile_found = True trials += 1 if trials > timeout: tile_found = True print "GAME OVER!" def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ # replace with your code self._grid1_[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ # replace with your code return self._grid1_[row][col] __repr__ = __str__ poc_2048_gui.run_gui(TwentyFortyEight(10, 10))
def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ return self._grid[row][col] #RICE = TwentyFortyEight(4, 4) #poc_2048_gui.run_gui(RICE) TEST1 = TwentyFortyEight(4, 6) print TEST1 poc_2048_gui.run_gui(TEST1) ##print TEST1 ##print str(TEST1) ##print TEST1.reset() ##print TEST1.__str__() ##print #Test_Suite_2048.run_suite(TEST1) ##print TEST1 ##print str(TEST1) ##print TEST1.reset() ##print TEST1.__str__() ##print
twoOrFour = [] index = 0 for index in range(0, 100): if (index < 90): twoOrFour.insert(index, 2) else: twoOrFour.insert(index, 4) randomTwoOrFour = random.randint(0, 99) valueOfNewTiles = twoOrFour[randomTwoOrFour] rowOfNewTile = coordinatesOfZero[selectedZeroIndex][0] columnOfNewTile = coordinatesOfZero[selectedZeroIndex][1] self.set_tile(rowOfNewTile, columnOfNewTile, valueOfNewTiles) def set_tile(self, row, col, value): self.EXAMPLE_GRID[row][col] = value def get_tile(self, row, col): return self.EXAMPLE_GRID[row][col] def set_tile(self, row, col, value): self.EXAMPLE_GRID[row][col] = value def get_tile(self, row, col): return self.EXAMPLE_GRID[row][col] main = TwentyFortyEight(4, 6) poc_2048_gui.run_gui(main)
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. available_positions = [] for row in range(self.grid_height): for col in range(self.grid_width): if self.cells[row][col] == 0: available_positions.append([row, col]) if not available_positions: print "There are no available positions." else: random_tile = random.choice(available_positions) weighted_choices = [(2, 9), (4, 1)] population = [val for val, cnt in weighted_choices for i in range(cnt)] tile = random.choice(population) self.set_tile(random_tile[0],random_tile[1], tile) def set_tile(self, row, col, value): # Set the tile at position row, col to have the given value. self.cells[row][col] = value def get_tile(self, row, col): # Return the value of the tile at position row, col. return self.cells[row][col] poc_2048_gui.run_gui(TwentyFortyEight(4, 4))
4 10% of the time. """ random_row = random.randrange(self.height) random_col = random.randrange(self.width) while self.get_tile(random_row,random_col)!= 0: random_row = random.randrange(self.height) random_col = random.randrange(self.width) if random.random() >= 0.1: self.set_tile(random_row, random_col, 2) else: self.set_tile(random_row, random_col, 4) def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ self.grid[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ return self.grid[row][col] ## SimpleGUICS2Pygame only works if self._frame.start() is moved to bottom of __init__ in poc_2048_gui.py poc_2048_gui.run_gui(TwentyFortyEight(9, 10)) #import testsuite #testsuite.run_test(TwentyFortyEight, merge)
Function that iterates through the cells in a grid in a linear direction Both start_cell is a tuple(row, col) denoting the starting cell direction is a tuple that contains difference between consecutive cells in the traversal """ return [ self._grid[start_cell[0] + step * direction[0]][start_cell[1] + step * direction[1]] for step in range(num_steps) ] def change_grid(self, start_cell, direction, new_list): """ Function like traverse_grid, but modifies the original grid as it iterates """ for step in range(len(new_list)): row = start_cell[0] + step * direction[0] col = start_cell[1] + step * direction[1] self._grid[row][col] = new_list[step] poc_2048_gui.run_gui(TwentyFortyEight(4, 4))
4 10% of the time. """ # Find an empty cell ("0") cell_value = -1 while cell_value != 0: rrow = random.randrange(0, self._height) rcol = random.randrange(0, self._width) cell_value = self._grid[rrow][rcol] # Assign a value to it if random.randrange(0, 10) > 0: self.set_tile(rrow, rcol, 2) else: self.set_tile(rrow, rcol, 4) def set_tile(self, row, col, value): """ Set the tile at position row, col to have the given value. """ self._grid[row][col] = value def get_tile(self, row, col): """ Return the value of the tile at position row, col. """ return self._grid[row][col] poc_2048_gui.run_gui(TwentyFortyEight(6, 7))