QT五子棋项目详解之五:AI人机对战Alpha-Beta剪枝算法

本文详细讲解了在QT五子棋项目中,如何通过Alpha-Beta剪枝算法优化AI人机对战的效率。在max-min极大极小值博弈算法基础上,阐述了剪枝的必要性和工作原理,通过举例说明了在计算过程中如何提前终止不必要的分支搜索,从而大幅提高搜索效率。代码实现也相对简单,该算法模拟了人类思考方式,确保了搜索的合理性。
摘要由CSDN通过智能技术生成

前面介绍了单纯的max-min算法,其效率是很低的,只能思考四层。没有看过的,先去看看,才能理解后面的。

只能思考4层是由于可能的走法太多了,如第一步电脑有225种走法,每一种走法都会导致玩家有224种走法............就像是一颗庞大的树。由于可能性太多,所以优化是必须的。优化的第一种办法就是剪枝。

没有看过的,先去看看,才能理解后面的。

QT五子棋项目详解之四:AI人机对战max-min极大极小值博弈算法:

http://blog.csdn.net/weixin_39788534/article/details/79499966


我接着前面的讲,如下图,思考两层的情况:计算的顺序是这样的:

先遍历了A分支的D,E,又遍历其他分支。F,G.........


我们可以考虑一下下面的这种情况:即当计算到H的时候,我已经没有必要去计算C节点的其他分支I....了

为什么,你想一下,当电脑走了C节点后,玩家会选择一个对玩家最不利的点,如果I的值大于1,那玩家就会选择1.

如果I的值等于0,那玩家就会选择0这个点,导致C的分数变成了0.不管I的值是什么,玩家都会选择前面的3这个节点,这样就大大的提高了效率。

因此,对于剪枝需要保留这一层的最值,对于电脑这一层来说,就是需要保留最大的3,如果后面的节点如C的节点比3小就不用再判断了。

就像是模拟人的思考方式一下,真的很简单。一切都是合理的方式。

代码也很简单,多加了几行。:


QPoint Game::maxmin2(int deep)
{
 
    int best = INT_MIN;
   QVector<QPoint> points =  gen(deep,computerColor);
   for(int i = 0;i<points.count();i++)
   {
 
       chess[points[i].x()][points[i].y()] = computerColor;
            int v = min2(deep-1,best > INT_MIN ? best : INT_MIN,INT_MAX,points[
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
======================================================================== 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. /////////////////////////////////////////////////////////////////////////////
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值