资源简介
Python制作的吃豆人游戏代码,需要在Python2环境下运行。
代码片段和文件信息
# eightpuzzle.py
# --------------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley primarily by
# John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# For more info see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html
import search
import random
# Module Classes
class EightPuzzleState:
“““
The Eight Puzzle is described in the course textbook on
page 64.
This class defines the mechanics of the puzzle itself. The
task of recasting this puzzle as a search problem is left to
the EightPuzzleSearchProblem class.
“““
def __init__( self numbers ):
“““
Constructs a new eight puzzle from an ordering of numbers.
numbers: a list of integers from 0 to 8 representing an
instance of the eight puzzle. 0 represents the blank
space. Thus the list
[1 0 2 3 4 5 6 7 8]
represents the eight puzzle:
-------------
| 1 | | 2 |
-------------
| 3 | 4 | 5 |
-------------
| 6 | 7 | 8 |
------------
The configuration of the puzzle is stored in a 2-dimensional
list (a list of lists) ‘cells‘.
“““
self.cells = []
numbers = numbers[:] # Make a copy so as not to cause side-effects.
numbers.reverse()
for row in range( 3 ):
self.cells.append( [] )
for col in range( 3 ):
self.cells[row].append( numbers.pop() )
if self.cells[row][col] == 0:
self.blankLocation = row col
def isGoal( self ):
“““
Checks to see if the puzzle is in its goal state.
-------------
| | 1 | 2 |
-------------
| 3 | 4 | 5 |
-------------
| 6 | 7 | 8 |
-------------
>>> EightPuzzleState([0 1 2 3 4 5 6 7 8]).isGoal()
True
>>> EightPuzzleState([1 0 2 3 4 5 6 7 8]).isGoal()
False
“““
current = 0
for row in range( 3 ):
for col in range( 3 ):
if current != self.cells[row][col]:
return False
current += 1
return True
def legalMoves( self ):
“““
Returns a list of legal moves from the current state.
Moves consist of moving the blank space up down left or right.
These are encoded as ‘up‘ ‘down‘ ‘left‘ and ‘right‘ respectively.
>>> EightPuzzleState([0 1 2 3 4 5 6 7 8]).legalMoves()
[‘down‘ ‘right‘]
“““
moves = []
row col = self.blankLocation
if(row != 0):
moves.append(‘up‘)
if(row != 2):
moves.append(‘down‘)
if(col != 0):
moves.append(‘left‘)
if(col != 2):
moves.append(‘right‘)
return moves
def result(self move):
“““
Returns a new eightPuzzle with the current state and blankLocation
updated based on the provided move.
The move should be a string drawn from a list returned by legalMoves.
Illegal moves will raise an exception which may be
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2014-11-24 21:20 search\
文件 1214 2014-03-05 15:35 search\commands.txt
目录 0 2014-11-24 20:55 search\docs\
文件 31014 2014-03-05 15:35 search\docs\eightpuzzle.html
文件 110267 2014-03-05 15:35 search\docs\game.html
文件 11958 2014-03-05 15:35 search\docs\ghostAgents.html
文件 150527 2014-03-05 15:35 search\docs\graphicsDisplay.html
文件 62329 2014-03-05 15:35 search\docs\graphicsUtils.html
文件 13892 2014-03-05 15:35 search\docs\keyboardAgents.html
文件 30401 2014-03-05 15:35 search\docs\layout.html
文件 102444 2014-03-05 15:35 search\docs\pacman.html
文件 8298 2014-03-05 15:35 search\docs\pacmanAgents.html
文件 7632 2014-03-05 15:35 search\docs\search.html
文件 67061 2014-03-05 15:35 search\docs\searchAgents.html
文件 9273 2014-03-05 15:35 search\docs\textDisplay.html
文件 54796 2014-03-05 15:35 search\docs\util.html
文件 7551 2014-03-05 15:35 search\eightpuzzle.py
文件 21413 2014-03-05 15:35 search\game.py
文件 2702 2014-03-05 15:35 search\ghostAgents.py
文件 24877 2014-03-05 15:35 search\graphicsDisplay.py
文件 11388 2014-03-05 15:35 search\graphicsUtils.py
文件 19981 2014-11-24 21:18 search\instructions_search.html
文件 2664 2014-03-05 15:35 search\keyboardAgents.py
文件 5060 2014-03-05 15:35 search\layout.py
目录 0 2014-11-24 20:55 search\layouts\
文件 1405 2014-03-05 15:35 search\layouts\bigCorners.lay
文件 1405 2014-03-05 15:35 search\layouts\bigMaze.lay
文件 256 2014-03-05 15:35 search\layouts\bigSafeSearch.lay
文件 480 2014-03-05 15:35 search\layouts\bigSearch.lay
文件 210 2014-03-05 15:35 search\layouts\boxSearch.lay
文件 141 2014-03-05 15:35 search\layouts\capsuleClassic.lay
............此处省略39个文件信息
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论