def run(self): board = Board(size=4) board.place_new_value_randomly() states = [ [0], # move up after having moved left [0], # move up after having moved right [1], # move right [3], # move left [1, 3], # move right after having moved down [2] # move down ] state = 0 num_consecutive_ignores = 0 for x in xrange(1000): # self.gfx.draw(board.board_values) moved = False for direction in states[state]: if board.can_move(direction): board_values_copy = deepcopy(board.board_values) self.board_states.append(board_values_copy) self.moves.append(direction) board.move(direction) moved = True break if moved: state = self.get_next_state(state) board.place_new_value_randomly() num_consecutive_ignores = 0 else: # print 'ignored move' num_consecutive_ignores += 1 state = self.get_next_state(state, force_down=num_consecutive_ignores >= 3) if len(board.get_possible_moves()) == 0: # print # print 'game over' break # print x + 1, 'moves' # print len(self.board_states), len(self.moves) filename = "run_" + str(uuid4()) + ".pickle" pickle.dump([self.board_states, self.moves], open('runs/' + filename, "wb"))
def play_game_randomly(self, max_num_moves=2000): board = Board(size=4) board.place_new_value_randomly() for x in xrange(max_num_moves): directions = range(4) random.shuffle(directions) moved = False for direction in directions: if board.can_move(direction): board.move(direction) moved = True break if not moved: break board.place_new_value_randomly() num_empty_tiles, max_tile_value, tile_sum = board.get_tile_stats() print(max_tile_value) self.max_tile_value = max_tile_value
def run(self): self.board_states = [] board = Board(size=4) board.place_new_value_randomly() for i in xrange(2000): if self.args.print_results: print('iteration', i) print(board) self.board_states.append(deepcopy(board.board_values)) directions = self.choose_direction(board.board_values) has_moved = False for direction in directions: if board.can_move(direction): board.move(direction) has_moved = True break if not has_moved: break board.place_new_value_randomly() num_empty_tiles, max_tile_value, tile_sum = board.get_tile_stats() print(max_tile_value) self.max_tile_value = max_tile_value