编程农场通关全部代码
在这里插入图片描述
在这里插入图片描述

#pumpkin###############################
#set_execution_speed(1)
clear()
while True:
	for c in range(3):
		for i in range(get_world_size()):
			for m in range(get_world_size()):
				if Entities.Pumpkin == get_entity_type():
					move(North)
				else:
					if can_harvest():
						harvest()
					till()
					plant(Entities.Pumpkin)
					use_item(Items.Water)
			move(East)
	for ii in range(get_world_size()):
		for mm in range(get_world_size()):
			if can_harvest():
				harvest()
			else:
				till()
				plant(Entities.Pumpkin)
			move(North)
		move(East)



###########Carrot#########################
while True:
	for m in range(get_world_size()):
		move(East)
		for i in range(get_world_size()):
			if can_harvest():
				harvest()
			till()
			plant(Entities.Carrot)
			use_item(Items.Weird_Substance)
			move(North)
	

###########Tree#########################
while True:
	for i in range(get_world_size()):
		if i % 2 ==0:
			for m in range(get_world_size()):
				if m % 2 == 0:
					till()
					plant(Entities.Tree)
				else:
					till()
					plant(Entities.carrot)
				move(North)
		if i%2 ==1:
			for m in range(get_world_size()):
				if m%2 == 1:
					till()
					plant(Entities.Tree)
					use_item(Items.Water) 
					use_item(Items.Weird_Substance)
				else:
					till()
					plant(Entities.carrot)
				move(North)
		move(East)
	for i in range(get_world_size()):
		for i in range(get_world_size()):
			if can_harvest():
				harvest()
			move(North)
		move(East)
		

###########Bone#########################
Clear()
def go(x,y):
	if x>0:
		for i in range(abs(x)):
			if move(East):
				pass
			else:
				change_hat(Hats.Dinosaur_Hat)
	elif x==0:
		pass
	else:
		for j in range(abs(x)):
			if move(West):
				pass
			else:
				change_hat(Hats.Dinosaur_Hat)
	if y>0:
		for m in range(abs(y)):
			if move(North):
				pass
			else:
				change_hat(Hats.Dinosaur_Hat)
	elif y==0:
		pass
	else:
		for k in range(abs(y)):
			if move(South):
				pass
			else:
				change_hat(Hats.Dinosaur_Hat)	
		
while True:
	change_hat(Hats.Dinosaur_Hat)
	for mm in range(16):
		if measure()!=None:
			next_x, next_y = measure()
			x, y = get_pos_x(), get_pos_y()
			go(next_x-x,next_y-y)
	change_hat(Hats.Dinosaur_Hat)
	
	
################cactus######################
clear()
def plantcactus():
	for i in range(get_world_size()):
		for m in range(get_world_size()):
			if Entities.Cactus==get_entity_type():
				pass
			else:
				till()
				plant(Entities.Cactus)
			move(North)
		move(East)

def swapcactus():
	a = measure()
	for i in range(4):
		if measure(North)>=a:
			pass
		else:
			swap(North)
		if measure(East)>=a:
			pass
		else:
			swap(East)
		#  South
		if measure(South)<=a:
			pass
		else:
			swap(South)
		if measure(West)<=a:
			pass
		else:
			swap(West)
			
def home():
	while get_pos_x() != 0:
		move(West)
	while get_pos_y() != 0:
		move(South)

def goto(x,y):
	home()
	for i in range(x):
		move(East)
	for j in range(y):
		move(North)

while True:
	clear()
	plantcactus()
	home()
	for k in range(4):
		for i in range(get_world_size()):
				for i in range(get_world_size()):
					move(North)
					swapcactus()
				move(East)

	
	home()
	for m in range(get_world_size()):
			for n in range(get_world_size()):
				if can_harvest():
					harvest()
				move(North)
			move(East)
			

################Gold####################
#set_execution_speed(1)
# 深度优先搜索实现
clear()
plant(Entities.Bush)
use_item(Items.Weird_Substance, 16)

current_position = (0, 0)
def dfs_maze_exploration():
    # 记录访问过的位置
    visited = set()
    # 记录路径,用于回溯
    path = []
    # 记录当前位置
        
    global current_position
    # 将起点标记为已访问
    visited.add(current_position)
    
    # 深度优先搜索递归函数
    def dfs():
        global current_position
		global flag
        # 尝试向北移动
        if get_entity_type()==Entities.Treasure:
            harvest()
		
		if get_entity_type()==Entities.Grass:
			return 0
			
        dx, dy = 0, 1
        next_position = (current_position[0] + dx, current_position[1] + dy)
        if next_position not in visited:
            if move(North):
                # 移动成功,更新当前位置
                previous_position = current_position
                current_position = next_position
                visited.add(current_position)
                path.append('North')
                
                pass
                
                # 递归探索
                dfs()
                
                # 回溯:返回到上一个位置
                move(South)
                current_position = previous_position
                path.append('South')
            else:
                # 移动失败,遇到墙壁
                pass
        else:
            pass
        
        # 尝试向南移动
        dx, dy = 0, -1
        next_position = (current_position[0] + dx, current_position[1] + dy)
        if next_position not in visited:
            if move(South):
                # 移动成功,更新当前位置
                previous_position = current_position
                current_position = next_position
                visited.add(current_position)
                path.append('South')
                
                # 递归探索
                dfs()
                
                # 回溯:返回到上一个位置
                move(North)
                current_position = previous_position
                path.append('North')
            else:
                # 移动失败,遇到墙壁
                pass
        else:
            pass
        
        # 尝试向东移动
        dx, dy = 1, 0
        next_position = (current_position[0] + dx, current_position[1] + dy)
        if next_position not in visited:
            if move(East):
                # 移动成功,更新当前位置
                previous_position = current_position
                current_position = next_position
                visited.add(current_position)
                path.append('East')
                
                # 递归探索
                dfs()
                
                # 回溯:返回到上一个位置
                move(West)
                current_position = previous_position
                path.append('West')
            else:
                # 移动失败,遇到墙壁
                pass
        else:
            pass
        
        # 尝试向西移动
        dx, dy = -1, 0
        next_position = (current_position[0] + dx, current_position[1] + dy)
        if next_position not in visited:
            if move(West):
                # 移动成功,更新当前位置
                previous_position = current_position
                current_position = next_position
                visited.add(current_position)
                path.append('West')
                
                # 递归探索
                dfs()
                
                # 回溯:返回到上一个位置
                move(East)
                current_position = previous_position
                path.append('East')
            else:
                # 移动失败,遇到墙壁
                pass
        else:
            pass
    
    # 开始探索
    dfs()
    return path

# 示例使用

path_taken = dfs_maze_exploration()
#print("Exploration completed.")

while True:
	clear()
	plant(Entities.Bush)
	use_item(Items.Weird_Substance, 100)
	
	current_position = (0, 0)
	
	path_taken = dfs_maze_exploration()
	#print("Exploration completed.")
	

Logo

中国智能体开发者社区,聚焦智能体与大模型开发,提供前沿资讯、实用工具链、开源项目及行业案例。通过技术沙龙、开发者大赛等活动,促进经验交流与协作,助力开发者快速构建创新智能应用。

更多推荐