def add_tile(self): # pick an empty tile and put a 2 (sometimes 4) there. empties = find_empty_positions(self.mat) new_pos = empties[self.rand_mod(len(empties))] new_value = 2 if self.rand_mod(10) > 0 else 4 self.mat = set_value(self.mat, new_pos, new_value) return self.mat
def my_score(matrix): num_empty = len(find_empty_positions(matrix)) high_val, high_pos = max( max( (val, (i,j)) for j,val in enumerate(row) ) for i,row in enumerate(matrix) ) # best_in_corner = ( # (high_pos[0] == 0 or high_pos[0] == len(matrix)-1) and # (high_pos[1] == 0 or high_pos[1] == len(matrix)-1) # ) return log2(get_adjacent_goodness(matrix)+1) * (num_empty + 1) * score(matrix)
def get_fills_and_num_samples(mat, max_samples): ''' >>> list(get_fills_and_num_samples( ((0,4,4,4),(4,4,4,4),(4,4,4,4),(4,4,4,4)), 100)) [(((0, 0), 4), 10), (((0, 0), 2), 90)] >>> list(get_fills_and_num_samples( ((0,4,4,4),(4,4,0,4),(4,4,4,4),(4,4,4,4)), 200)) [(((1, 2), 4), 10), (((1, 2), 2), 90), (((0, 0), 2), 90), (((0, 0), 4), 10)] ''' empty_positions = find_empty_positions(mat) fills_and_weights = ([((pos, 2), 9) for pos in empty_positions] + [((pos, 4), 1) for pos in empty_positions]) random.shuffle(fills_and_weights) fills = [f_w[0] for f_w in fills_and_weights] weights = [f_w[1] for f_w in fills_and_weights] num_samples = distribute(max_samples, weights) return zip(fills, num_samples)
def my_score(matrix): return len(find_empty_positions(matrix)) * 10000 + score(matrix)