python中alpha_python中的Alphabeta剪枝

我正在尝试在一个连接四型游戏中实现一个计算机播放器。Alpha-beta修剪似乎是实现这一目标的最佳方法,但我似乎不知道我做错了什么。在

下面是我想出的代码。它从初始根状态开始。对于每一个可能的、有效的移动(如果没有修剪发生的话),算法:对状态进行深度复制,更新状态(增加深度、切换圈数、添加片段、设置启发式值),并将此新状态添加到根的后续项列表中。在

如果新状态不是叶(即在最大深度),它递归地继续。如果它是一个叶,算法会检查根的值和适当的局部alpha/beta值,并相应地更新。在检查了所有可能的有效选项后,算法返回相应的本地alpha/beta值。在

至少,这是我想要的。每次运行都返回值0。此处请求的是初始化代码:class GameState:

def __init__(self, parentState = None):

# copy constructor

if not(parentState == None):

self.matrix = copy.deepcopy(parentState.matrix)

self.successor = copy.deepcopy(parentState.successor)

self.depth = parentState.depth

self.turn = parentState.turn

self.alpha = parentState.alpha

self.beta = parentState.beta

self.connects = copy.deepcopy(parentState.connects)

self.value = parentState.value

self.algo_value = parentState.value

self.solution = parentState.solution

# new instance

else:

# empty board

self.matrix = [[0 for y in xrange(6)] for x in xrange(7)]

## USED WHEN GROWING TREE

self.successor = [] # empty list

self.depth = 0 # start at root

self.turn = 1 # game starts on user's turn

## USED WHEN SEARCHING FOR SOLUTION

self.alpha = float("-inf")

self.beta = float("+inf")

self.connects = [0, 0, 0] # connects in state

self.algo_value = float("-inf")

self.value = 0 # alpha-beta value of connects

self.solution = False # connect four

def alphabeta(root):

if root.depth < MAX_EXPANSION_DEPTH:

# pass down alpha/beta

alpha = root.alpha

beta = root.beta

# for each possible move

for x in range(7):

# ALPHA-BETA PRUNING

# if root is MAXIMIZER

if (root.turn == 2) and (root.algo_value > beta): print "beta prune"

# if root is MINIMIZER

elif (root.turn == 1) and (root.algo_value < alpha): print "alpha prune"

# CANNOT prune

else:

# if move legal

if (checkMove(root, x)):

# CREATE NEW STATE

root.successor.append(GameState(root))

working_state = root.successor[-1]

# update state

working_state.successor = []

working_state.depth += 1

working_state.turn = (working_state.turn % 2) + 1

cons = dropPiece(working_state, x, working_state.turn)

# update state values

# MAXIMIZER

if working_state.turn == 2:

working_state.value = ((cons[0]*TWO_VAL)+(cons[1]*THREE_VAL)+(cons[2]*FOUR_VAL)) + root.value

working_state.algo_value = float("-inf")

# MINIMIZER

else:

working_state.value = ((-1)*((cons[0]*TWO_VAL)+(cons[1]*THREE_VAL)+(cons[2]*FOUR_VAL))) + root.value

working_state.algo_value = float("inf")

# if NOT a leaf node

if (working_state.depth < MAX_EXPANSION_DEPTH):

# update alpha/beta values

working_state.alpha = alpha

working_state.beta = beta

ret = alphabeta(working_state)

# if MAXIMIZER

if (root.turn == 2):

if (ret > root.algo_value): root.algo_value = ret

if (ret > alpha): alpha = ret

# if MINIMIZER

else:

if (ret < root.algo_value): root.algo_value = ret

if (ret < beta): beta = ret

# if leaf, return value

else:

if root.turn == 2:

if (working_state.value > root.algo_value): root.algo_value = working_state.value

if working_state.value > alpha: alpha = working_state.value

else:

if (working_state.value < root.algo_value): root.algo_value = working_state.value

if working_state.value < beta: beta = working_state.value

if root.turn == 2: return alpha

else: return beta

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
======================================================================== MICROSOFT FOUNDATION CLASS LIBRARY : fir ======================================================================== AppWizard has created this fir application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your fir application. fir.dsp This file (the project file) contains information at the project level and is used to build a single project or subproject. Other users can share the project (.dsp) file, but they should export the makefiles locally. fir.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CFirApp application class. fir.cpp This is the main application source file that contains the application class CFirApp. fir.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. fir.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. res\fir.ico This is an icon file, which is used as the application's icon. This icon is included by the main resource file fir.rc. res\fir.rc2 This file contains resources that are not edited by Microsoft Visual C++. You should place all resources not editable by the resource editor in this file. ///////////////////////////////////////////////////////////////////////////// For the main frame window: MainFrm.h, MainFrm.cpp These files contain the frame class CMainFrame, which is derived from CFrameWnd and controls all SDI frame features. ///////////////////////////////////////////////////////////////////////////// AppWizard creates one document type and one view: firDoc.h, firDoc.cpp - the document These files contain your CFirDoc class. Edit these files to add your special document data and to implement file saving and loading (via CFirDoc::Serialize). firView.h, firView.cpp - the view of the document These files contain your CFirView class. CFirView objects are used to view CFirDoc objects. ///////////////////////////////////////////////////////////////////////////// Other standard files: StdAfx.h, StdAfx.cpp These files are used to build a precompiled header (PCH) file named fir.pch and a precompiled types file named StdAfx.obj. Resource.h This is the standard header file, which defines new resource IDs. Microsoft Visual C++ reads and updates this file. ///////////////////////////////////////////////////////////////////////////// Other notes: AppWizard uses "TODO:" to indicate parts of the source code you should add to or customize. If your application uses MFC in a shared DLL, and your application is in a language other than the operating system's current language, you will need to copy the corresponding localized resources MFC42XXX.DLL from the Microsoft Visual C++ CD-ROM onto the system or system32 directory, and rename it to be MFCLOC.DLL. ("XXX" stands for the language abbreviation. For example, MFC42DEU.DLL contains resources translated to German.) If you don't do this, some of the UI elements of your application will remain in the language of the operating system. /////////////////////////////////////////////////////////////////////////////

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值