import random import time # 声明全局变量 global monkey, box, banana, x, z, count, a x=0 z=0 # -1往左走,1往右走 def direction(): a = random.randint(1, 10) if(a>5): return 1 else: return -1 # 移动 def Goto(): global monkey count = 0 flag = 0 flag = direction() if (flag == -1): while (monkey > 0): monkey -= 1 count += 1 if(monkey==0): print('状态:', monkey, x, box, z) print('方向错了...') time.sleep(1) print('猴子调整了方向...') flag = 1 time.sleep(1) if(flag==1): while (monkey < box): monkey += 1 count += 1 if(monkey==box): break print('猴子找到了箱子!') print('状态:', monkey, x, box, z) print('总共走了
Python-猴子(AI)摘香蕉问题
最新推荐文章于 2023-08-22 19:13:20 发布
这段Python代码模拟了一个猴子通过智能决策寻找并摘取香蕉的过程。猴子在平面上移动,遇到箱子时会尝试推动箱子以接近香蕉。通过随机数决定猴子的行动策略,包括改变方向、推箱子和爬箱子等。最终,猴子成功摘到香蕉。
摘要由CSDN通过智能技术生成