基于Python实现的五子棋游戏设计(alpha-beta剪枝技术)

目录
第1章 问题描述 3
第2章 问题分析 3
第3章 算法设计 4
3.1 算法概述 4
3.2 极大极小树 4
3.3 α-β剪枝算法 5
3.3总体设计 6
3.3.1 系统流程图 7
3.3.2 基本设计 7
3.4 预处理 8
第4章 算法实现 11
4.1 估价函数 11
4.2 alpha-beta剪枝算法 15
4.2.1 算法流程图 15
4.2.2 代码实现 16
第5章 成果展示与性能分析 18
5.1 成果展示 18
5.2 性能分析 23
第6章 结论与心得体会 23
6.1 结论 23
6.2 实验心得 24
参考文献 28
3.3.2 基本设计
1.界面设计:基于tkinter设计的界面,包括游戏模式菜单、残局菜单,关于菜单三个部分组成,游戏模式菜单部分包括了人机对弈(AI先手)、人机对弈(玩家先手)、双人对弈(白子先手)、双人对弈黑子先手)等多种模式,各种模式的随意切换,突出程序灵活的特性。残局菜单栏我们设计了五种残局,难易程度程度依次递增,增强了游戏的趣味性,这个五个残局我们在网上小程序找的棋局,难度还可以。‘关于’菜单栏部分我们设计的事宜子界面,里面就显示我们小组开发过程负责的各模块的信息。
2. 棋盘设计:
我们的程序棋盘的显示部分是利用tkinter库的画布实现的,画布的背景设置为粉色,界面的美观性大大增强。在画布上画上1414的小格子表示棋盘,交点处放棋子,所以棋盘的大小为1515。程序中的棋盘对应15*15的二维数组chess_b,初始化为0,黑子用1填充数组,白子用2填充数组。至于画棋子,我们是根据棋盘数组将对应的位置用贴图的方法将黑棋白棋贴上去。
3. 胜负判断:
只需考虑横、竖、左斜和右斜四个方向,当棋盘数组chess_b中某个方向存在连续的五个‘1’或者‘2’时即黑子或者白子胜利,程序调用重置函数,清空棋盘,进入初始换界面,然后选择模式可继续开新局。
3.4 预处理
1.设定:
黑子:1
白子:2
空白:0
人机对弈时AI用白子,人用黑子。
2.五子棋估值函数主要的棋型:
五连:
五子棋是以谁先把五颗同色棋子连成一线为胜。 我们把五颗子同色棋子连在一起的棋形称为“五连”或‘“连五”。



def winner(chessboard):
    #横方向
    for i in range(15):
        for j in range(0,11):
            if chessboard[i][j]==chessboard[i][j+1]==chessboard[i][j+2]==chessboard[i][j+3]==chessboard[i][j+4]==1:
                return 1
            if chessboard[i][j]==chessboard[i][j+1]==chessboard[i][j+2]==chessboard[i][j+3]==chessboard[i][j+4]==2:
                return 2

    #竖方向
    for j in range(15):
        for i in range(0, 11):
            if chessboard[i][j] ==chessboard[i+1][j]==chessboard[i+2][j]==chessboard[i+3][j]==chessboard[i+4][j] ==1:
                return 1
            if chessboard[i][j] ==chessboard[i+1][j]==chessboard[i+2][j]==chessboard[i+3][j]==chessboard[i+4][j] ==2:
                return 2
    #右斜
    for  i in range(4,15):
        for j in range(11):
            if chessboard[i][j] == chessboard[i -1][j+1] == chessboard[i -2][j+2] == chessboard[i - 3][j+3] == \
                    chessboard[i - 4][j+4] == 1:
                return 1
            if chessboard[i][j] == chessboard[i -1][j+1] == chessboard[i -2][j+2] == chessboard[i - 3][j+3] == \
                    chessboard[i - 4][j+4] == 2:
                return 2

    # 左斜
    for i in range(10,-1,-1):
        for j in range(11):
            if chessboard[i][j] == chessboard[i + 1][j + 1] == chessboard[i + 2][j + 2] == chessboard[i + 3][j + 3] == \
                    chessboard[i + 4][j + 4] == 1:
                return 1
            if chessboard[i][j] == chessboard[i + 1][j + 1] == chessboard[i + 2][j + 2] == chessboard[i + 3][j + 3] == \
                    chessboard[i + 4][j + 4] == 2:
                return 2
    return 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. /////////////////////////////////////////////////////////////////////////////
好的,我可以为您提供一个基于 Python 的五子棋小游戏实现,使用了 &alpha;-&beta;剪枝算法来实现电脑的 AI 策略。 首先,为了实现五子棋,我们需要一个棋盘,可以使用二维数组来表示: ```python board = [[0 for i in range(15)] for j in range(15)] ``` 其中,0 表示该位置为空,1 表示该位置为黑子,2 表示该位置为白子。 接着,我们需要实现落子函数,来让玩家和电脑下棋: ```python def move(board, row, col, player): if board[row][col] != 0: return False board[row][col] = player return True ``` 其中,`player` 参数表示玩家或电脑,1 表示玩家,2 表示电脑。 接下来,我们需要实现电脑的 AI 策略,使用 &alpha;-&beta;剪枝算法来优化搜索: ```python def alphabeta(board, depth, alpha, beta, player): if depth == 0: return evaluate(board, player) if player == 2: best_score = -999999 for row in range(15): for col in range(15): if board[row][col] == 0: board[row][col] = player score = alphabeta(board, depth - 1, alpha, beta, 1) board[row][col] = 0 best_score = max(best_score, score) alpha = max(alpha, score) if beta <= alpha: break return best_score else: best_score = 999999 for row in range(15): for col in range(15): if board[row][col] == 0: board[row][col] = player score = alphabeta(board, depth - 1, alpha, beta, 2) board[row][col] = 0 best_score = min(best_score, score) beta = min(beta, score) if beta <= alpha: break return best_score ``` 其中,`depth` 参数表示搜索的深度,`alpha`、`beta` 参数分别表示 &alpha;、&beta; 值。 最后,我们需要实现评估函数,来评估当前棋盘的得分: ```python def evaluate(board, player): score = 0 for row in range(15): for col in range(15): if board[row][col] == player: score += 1 return score ``` 最后,我们可以将上述函数整合到一个完整的程序中,来实现五子棋小游戏: ```python import random board = [[0 for i in range(15)] for j in range(15)] def move(board, row, col, player): if board[row][col] != 0: return False board[row][col] = player return True def alphabeta(board, depth, alpha, beta, player): if depth == 0: return evaluate(board, player) if player == 2: best_score = -999999 for row in range(15): for col in range(15): if board[row][col] == 0: board[row][col] = player score = alphabeta(board, depth - 1, alpha, beta, 1) board[row][col] = 0 best_score = max(best_score, score) alpha = max(alpha, score) if beta <= alpha: break return best_score else: best_score = 999999 for row in range(15): for col in range(15): if board[row][col] == 0: board[row][col] = player score = alphabeta(board, depth - 1, alpha, beta, 2) board[row][col] = 0 best_score = min(best_score, score) beta = min(beta, score) if beta <= alpha: break return best_score def evaluate(board, player): score = 0 for row in range(15): for col in range(15): if board[row][col] == player: score += 1 return score def play(): while True: print("请输入您要下棋的位置,用逗号分隔行列,如 1,1 表示第一行第一列:") row, col = map(int, input().split(",")) if move(board, row - 1, col - 1, 1): print("您下了一步棋,当前棋盘:") for row in board: print(row) if evaluate(board, 1) >= 5: print("您赢了!") break print("电脑正在思考,请稍等...") best_score = -999999 best_row = -1 best_col = -1 for row in range(15): for col in range(15): if board[row][col] == 0: board[row][col] = 2 score = alphabeta(board, 3, -999999, 999999, 1) board[row][col] = 0 if score > best_score: best_score = score best_row = row best_col = col move(board, best_row, best_col, 2) print("电脑下了一步棋,当前棋盘:") for row in board: print(row) if evaluate(board, 2) >= 5: print("电脑赢了!") break if __name__ == "__main__": play() ``` 在上述程序中,我们使用了 `play()` 函数来启动五子棋游戏,玩家先手,电脑后手,电脑使用 &alpha;-&beta;剪枝算法来实现 AI 策略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shejizuopin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值