python代码游戏反恐精英和报告_敲代码学Python:CS188之吃完所有角落的豆豆

先上视频:吃完所有角落的豆豆视频演示https://www.zhihu.com/video/1177971208993325056

简单说一下我的思路:

搜索状态的表达是搜索算法中的重要部分,在本题中,为了记录下吃豆人的吃豆过程,我把吃豆人所经过的包含豆豆的坐标放在搜索状态中,这样就可以从搜索状态中判定它是否完成了当前地图的任务目标。

贴代码吧:

class CornersProblem(search.SearchProblem):

"""This search problem finds paths through all four corners of a layout.You must select a suitable state space and successor function"""

def __init__(self, startingGameState):

"""Stores the walls, pacman's starting position and corners."""

self.walls = startingGameState.getWalls()

self.startingPosition = startingGameState.getPacmanPosition()

top, right = self.walls.height-2, self.walls.width-2

self.corners = ((1,1), (1,top), (right, 1), (right, top))

for corner in self.corners:

if not startingGameState.hasFood(*corner):

print('Warning: no food in corner ' + str(corner))

self._expanded = 0 # DO NOT CHANGE; Number of search nodes expanded

# Please add any code here which you would like to use

# in initializing the problem

"*** YOUR CODE HERE ***"

# 这里我没有用上,^_^

def getStartState(self):

"""Returns the start state (in your state space, not the full Pacman statespace)"""

"*** YOUR CODE HERE ***"

# 依然以吃豆人的初始位置作为起始状态

return self.startingPosition

def isGoalState(self, state):

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值