python+pygame實現五子棋人機對戰之三

上回講過:

python+pygame實現五子棋人機對戰之一

python+pygame實現五子棋人機對戰之二

界面已經有了,并且可以支持鼠標操作選擇菜單和人機對戰開始下棋了,那電腦是如何應手落子呢?以下內容是通用的類,全部放在utils.py中

四、

最常見的基本棋型大體有以下幾種:連五,活四,沖四,活三,眠三,活二,眠二了,下面簡要講解一下:

4.1 連五:五顆同色棋子連在一起

4.2 活四:由4枚同色棋子形成的兩端沒有對方棋子的四枚棋子

4.3 沖四一(連四):由4枚同色棋子形成的一端有對方棋子的四枚棋子

4.4 沖四二(跳四):由4枚同色棋子形成的中間沒有對方棋子的四枚棋子。

4.5 眠四:由4枚同色棋子形成的兩端有對方棋子的4枚棋子。? 不能成五的四連。

相對比活四來說,沖四的威脅性就小了很多,此時,防守方只要跟著防守在那個唯一的連五點上,沖四就沒法形成連五。

4.6 活三一(連三):由3枚同色棋子形成連三、跳三。? 再走一著可以形成活四的三。 兩端都是威脅的活三。簡稱“連三”。

4.7 活三二(跳三):由3枚同色棋子形成的中間沒有對方棋子的三枚棋子。中間夾有一個威脅的活三。簡稱“跳三”。

4.8 眠三:由3枚同色棋子形成的兩端有對方棋子的3枚棋子。? 即再走一著可以形成沖四的三。眠三的形狀是很豐富的。眠三的棋型與活三的棋型相比,危險系數下降不少,因為眠三棋型即使不去防守,下一手它也只能形成沖四,而對于單純的沖四棋型,我們是可以防守住的。

活三可以形成眠三,但也能夠形成活四。眠三只能夠形成沖四。

4.9 活二 :由2枚同色棋子形成的連二(由2枚同色棋子連在一起形成的兩頭沒有對方棋子的2枚棋子。)、跳二(由2枚同色棋子形成的中間沒有對方棋子的2枚棋子。)、大跳二(中間有兩個空白位置的跳二)。

4.10 眠二 :由2枚同色棋子形成的兩端有對方棋子的2枚棋子。是能夠形成眠三的二。

? ? ??????? ???

對于棋盤上每一個交叉點,會有8個方向連成五子的可能性,那么我們就利用算法算出所有的可能性,并根據它成五的概率給它打分,這樣,得分最高的就是最好的落子點。

如何去實現呢?首先我們在8個方向上將其抽象成坐標的加減,例如水平向右,那么x+1,y不變,向右上,則x+1,y+1 .按正下方開始,逆時針8個方向就形成下面的數據結構:

directions = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]

?獲取當前坐標的各個方向上棋子屬性,返回1代表白棋,返回2代表黑棋,返回0代表沒有棋,返回5表示在棋盤外。

from params import Paramsrows = int(Params.get('ROWS'))
blocksize = int(Params.get('blockSize'))
width = int(Params.get('WIDTH'))
height = int(Params.get('HEIGHT'))# 獲取當前坐標的各個方向上棋子屬性,返回1代表白棋,返回2代表黑棋,返回0代表沒有棋,返回5表示在棋盤外
def getpointpiece(_mapchess,pos, src, offset):# 8個方向directions = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]x1, y1 = posx1 = x1 + directions[src - 1][0] * offset * blocksizey1 = y1 + directions[src - 1][1] * offset * blocksizeif x1 > 588 or y1 > 588 or x1 < 28 or y1 < 28:return 5else:return _mapchess[str(x1) + ',' + str(y1)]

再加上剛才分析的五子棋的基本棋型,可以設計出一個打分算法:

from params import Paramsrows = int(Params.get('ROWS'))
blocksize = int(Params.get('blockSize'))
width = int(Params.get('WIDTH'))
height = int(Params.get('HEIGHT'))# 判斷每個點的value,用來排序該點下棋的可行性
def pointvalue(_mapchess, pos, flag1, flag2):value = 0    #加權值for i in range(1, 9):  #8個方向# 11111   連五if getpointpiece(_mapchess,pos, i, 1) == flag1 and \getpointpiece(_mapchess,pos, i, 2) == flag1 and \getpointpiece(_mapchess,pos, i, 3) == flag1 and \getpointpiece(_mapchess,pos, i, 4) == flag1 and \getpointpiece(_mapchess,pos, i, 5) == flag1:value += 1000000# *1111_ 活四if getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == flag1 and \getpointpiece(_mapchess, pos, i, 3) == flag1 and \getpointpiece(_mapchess, pos, i, 4) == flag1 and \getpointpiece(_mapchess, pos, i, 5) == 0:value += 50000# *11112 沖四1,沖四指加一個點就是連五,比活四威力小得多if getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == flag1 and \getpointpiece(_mapchess, pos, i, 3) == flag1 and \getpointpiece(_mapchess, pos, i, 4) == flag1 and \getpointpiece(_mapchess, pos, i, 5) == flag2:value += 30000# 1*111 沖四2if getpointpiece(_mapchess, pos, i, -1) == flag1 and \getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == flag1 and \getpointpiece(_mapchess, pos, i, 3) == flag1:value += 30000# 11*11 沖四3if getpointpiece(_mapchess, pos, i, -2) == flag1 and \getpointpiece(_mapchess, pos, i, -1) == flag1 and \getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == flag1:value += 30000# *111_ 活三1,活三可以形成活四的三if getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == flag1 and \getpointpiece(_mapchess, pos, i, 3) == flag1 and \getpointpiece(_mapchess, pos, i, 4) == 0:value += 20000# *1_11_ 活三2if getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == 0 and \getpointpiece(_mapchess, pos, i, 3) == flag1 and \getpointpiece(_mapchess, pos, i, 4) == flag1 and \getpointpiece(_mapchess, pos, i, 5) == 0:value += 20000# *1112 眠三1  眠三是只能形成沖四的三if getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == flag1 and \getpointpiece(_mapchess, pos, i, 3) == flag1 and \getpointpiece(_mapchess, pos, i, 4) == flag2:value += 15000# _1_112 眠三2if getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == 0 and \getpointpiece(_mapchess, pos, i, 3) == flag1 and \getpointpiece(_mapchess, pos, i, 4) == flag1 and \getpointpiece(_mapchess, pos, i, 5) == flag2:value += 15000# _11_12 眠三3if getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == flag1 and \getpointpiece(_mapchess, pos, i, 3) == 0 and \getpointpiece(_mapchess, pos, i, 4) == flag1 and \getpointpiece(_mapchess, pos, i, 5) == flag2:value += 15000# 1__11 眠三4if getpointpiece(_mapchess, pos, i, -1) == flag1 and \getpointpiece(_mapchess, pos, i, 1) == 0 and \getpointpiece(_mapchess, pos, i, 2) == flag1 and \getpointpiece(_mapchess, pos, i, 3) == flag1:value += 15000# 1_1_1 眠三5if getpointpiece(_mapchess, pos, i, -1) == flag1 and \getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == 0 and \getpointpiece(_mapchess, pos, i, 3) == flag1:value += 15000# 2_111_2 眠三6if getpointpiece(_mapchess, pos, i, -1) == flag2 and \getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == flag1 and \getpointpiece(_mapchess, pos, i, 3) == flag1 and \getpointpiece(_mapchess, pos, i, 4) == 0 and \getpointpiece(_mapchess, pos, i, 5) == flag2:value += 15000# __11__ 活二1  活二能夠形成活三的二if getpointpiece(_mapchess, pos, i, -1) == 0 and \getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == flag1 and \getpointpiece(_mapchess, pos, i, 3) == 0 and \getpointpiece(_mapchess, pos, i, 4) == 0:value += 10000# _1_1_ 活二2if getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == 0 and \getpointpiece(_mapchess, pos, i, 3) == flag1 and \getpointpiece(_mapchess, pos, i, 4) == 0:value += 10000# 211__ 眠二1 ,能夠形成眠三的二if getpointpiece(_mapchess, pos, i, -1) == flag2 and \getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == flag1 and \getpointpiece(_mapchess, pos, i, 3) == 0 and \getpointpiece(_mapchess, pos, i, 4) == 0 and \getpointpiece(_mapchess, pos, i, 5) == 0:value += 1000#21_1__ 眠二2if getpointpiece(_mapchess, pos, i, -1) == flag2 and \getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == 0 and \getpointpiece(_mapchess, pos, i, 3) == flag1 and \getpointpiece(_mapchess, pos, i, 4) == 0 and \getpointpiece(_mapchess, pos, i, 5) == 0:value += 1000#21__1_ 眠二3if getpointpiece(_mapchess, pos, i, -1) == flag2 and \getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == 0 and \getpointpiece(_mapchess, pos, i, 3) == 0 and \getpointpiece(_mapchess, pos, i, 4) == flag1 and \getpointpiece(_mapchess, pos, i, 5) == 0:value += 1000#*1___1 眠二4if getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == 0 and \getpointpiece(_mapchess, pos, i, 3) == 0 and \getpointpiece(_mapchess, pos, i, 4) == 0 and \getpointpiece(_mapchess, pos, i, 5) == flag1:value += 1000# *1__if getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == 0 and \getpointpiece(_mapchess, pos, i, 3) == 0:value += 30# *1_if getpointpiece(_mapchess, pos, i, 1) == flag1 and \getpointpiece(_mapchess, pos, i, 2) == 0:value += 20# *1if getpointpiece(_mapchess, pos, i, 1) == flag1:value += 10return value# 獲取當前坐標的各個方向上棋子屬性,返回1代表白棋,返回2代表黑棋,返回0代表沒有棋,返回5表示在棋盤外
def getpointpiece(_mapchess,pos, src, offset):# 8個方向directions = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]x1, y1 = posx1 = x1 + directions[src - 1][0] * offset * blocksizey1 = y1 + directions[src - 1][1] * offset * blocksizeif x1 > 588 or y1 > 588 or x1 < 28 or y1 < 28:return 5else:return _mapchess[str(x1) + ',' + str(y1)]

這樣就可以實現:當人下了棋后,電腦通過這個算法找出最有利的落子點,然后就在那里下棋。

注:本程序只是提供一個思路而已,并不是最優解。代碼中value值可以自行修改,算法可以自己再設計,例如可以假如雙活三之類的必須防御的應手。

如何判斷勝負呢?待續。。。

python+pygame實現五子棋人機對戰之四

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/44983.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/44983.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/44983.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

LiteOS 多線程編程

? 鴻蒙系統的多線程編程步驟&#xff1a; 1. 描述要創建的線程的屬性配置. attr: attributeosThreadAttr_t attr;//聲明一個線程屬性變量memset(&attr, 0, sizeof(attr));//memset改變一個內存單元上存的值為0//以下三個為必須設置的線程屬性attr.name "ledThread&q…

全球高端銷量第一 凱迪仕智能鎖建博會獲重磅大獎再次遙遙領先

2024年7月11日&#xff0c;第26屆中國廣州建博會圓滿落幕。Kaadas凱迪仕第11年受邀參展&#xff0c;憑借超吸睛的賽博風展館和重磅旗艦傳奇大師K70系列智能鎖震撼亮相&#xff0c;吸引抖音網紅云集打卡直播以及眾多主流及行業媒體聚集報道。在大家居建裝行業全球第一展的舞臺上…

問題清除指南|Dell OptiPlex 7070 升級 win11 開啟 TPM 2.0 教程

前言&#xff1a;最近想把實驗室臺式機的系統從 Windows 10 升級到 Windows 11&#xff0c;遇到一點小問題&#xff0c;在此記錄一下解決辦法。 ?? 注&#xff1a;本教程僅在 Dell OptiPlex 7070 臺式機系統中測試有效&#xff0c;并不保證其余型號機器適用此教程。 參考鏈接…

中國科學院地理所牛書麗團隊《Global Change Biology 》最新成果!

本文首發于“生態學者”微信公眾號&#xff01; 在全球氣候變化的背景下&#xff0c;干旱地區的擴張對生態系統的氮循環產生了深遠影響。氮同位素&#xff08;δ15N&#xff09;的天然豐度&#xff0c;尤其是土壤中的δ15N&#xff0c;是評估陸地生態系統氮循環動態和氮限制的關…

【ARMv8/v9 GIC 系列 1.7 -- GIC PPI | SPI | SGI | LPI 中斷使能配置概述】

請閱讀【ARM GICv3/v4 實戰學習 】 文章目錄 GIC 各種中斷使能配置PPIs(每個處理器私有中斷)SPIs(共享外設中斷)SGIs(軟件生成的中斷)LPIs(局部中斷)GIC 各種中斷使能配置 在ARM GICv3和GICv4架構中,不同類型的中斷(如PPIs、SPIs、SGIs和LPIs)可以通過不同的方式進…

分享:2024好的ai文章生成器下載資源 tzqsbic

在當今數字化的時代&#xff0c;ai技術的發展日新月異&#xff0c;為我們的生活和工作帶來了諸多便利。其中&#xff0c;ai文章生成器作為一項創新的工具&#xff0c;給當代人們帶來了很多好處&#xff0c;尤其是對于很多創作者&#xff0c;不僅能解決創作困難&#xff0c;而且…

【開發工具】webStrom2024版-永久使用

1、解壓文件 2、安裝步驟 先執行unistall-current-user.vbs&#xff0c;確保當前環境變量下沒有歷史使用記錄。再執行install-current-user.vbs。運行的時候&#xff0c;會有第一個彈窗&#xff0c;點擊確定&#xff0c;稍微等待一會&#xff0c;會出現 Done 的彈窗&#xff0…

【Linux】進程間通信之System V共享內存

&#x1f466;個人主頁&#xff1a;Weraphael ?&#x1f3fb;作者簡介&#xff1a;目前正在學習c和算法 ??專欄&#xff1a;Linux &#x1f40b; 希望大家多多支持&#xff0c;咱一起進步&#xff01;&#x1f601; 如果文章有啥瑕疵&#xff0c;希望大佬指點一二 如果文章對…

Prometheus+Grafana監控Linux主機

1、安裝Prometheus 1.1 、下載Prometheus 下載網址 https://github.com/prometheus/prometheus/releases選擇需要的版本 wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz1.2、安裝Prometheus軟件 1.2.1、…

解決鴻蒙開發中克隆項目無法簽名問題

文章目錄 問題描述問題分析解決方案 問題描述 在一個風和日麗的早晨&#xff0c;這是我學習鴻蒙開發的第四天&#xff0c;把文檔過了一遍的我準備看看別人的項目學習一下&#xff0c;于是就用git去clone了一個大佬的開源項目&#xff0c;在簽名的時候遇到了問題&#xff1a; h…

在攻防演練中遇到的一個“有馬蜂的蜜罐”

在攻防演練中遇到的一個“有馬蜂的蜜罐” 有趣的結論&#xff0c;請一路看到文章結尾 在前幾天的攻防演練中&#xff0c;我跟隊友的氣氛氛圍都很好&#xff0c;有說有笑&#xff0c;恐怕也是全場話最多、笑最多的隊伍了。 也是因為我們遇到了許多相當有趣的事情&#xff0c;其…

Spring JDBC 具名參數用法

Spring JDBC中具名參數的用法 maven引入Spring jdbc <dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.19</version></dependency> 在Spring配置中配置 <!-…

【leetcode】滑動窗口專題

文章目錄 1.長度最小的子數組2.無重復字符的最長子串3.最大連續1的個數III4.將x減小到0的最小操作數5.水果成籃6.找到字符串中所有字母異位詞7.串聯所有單詞的子串8.最小覆蓋子串 1.長度最小的子數組 leetcode 209.長度最小的子數組 看到這個題目&#xff0c;第一眼肯定想到的…

正則表達式控制everything等搜索工具更快速的對需要的內容進行檢索

正則表達式對文件搜索工具規則 表格模式 匹配模式描述abgr(ale)y匹配 “gray” 或 “grey”.匹配除換行符之外的任意單個字符[abc]匹配字符 “a”、“b” 或 “c” 中的任意一個[^abc]匹配除了 “a”、“b”、“c” 之外的任意單個字符[a-z]匹配小寫字母 a 到 z 之間的任意一…

科普文:深入理解Mybatis

概敘 (1) JDBC JDBC(Java Data Base Connection,java數據庫連接)是一種用于執行SQL語句的Java API,可以為多種關系數據庫提供統一訪問,它由一組用Java語言編寫的類和接口組成.JDBC提供了一種基準,據此可以構建更高級的工具和接口,使數據庫開發人員能夠編寫數據庫應用程序。 優點…

Vue3 + Echarts堆疊折線圖的tooltip不顯示問題

問題介紹 使用Echarts在Vue3Vite項目中繪制堆疊折線圖的的時候&#xff0c;tooltip總是不顯示&#xff0c;經過很長時間的排查和修改&#xff0c;最后發現是在使用上有錯誤導致的。 錯誤圖片展示 問題原因 由于Vue3底層使用proxy代理創建示例&#xff0c;使用其創建出來的實…

RDD 專項練習

RDD 專項練習 現有分數信息文件 scores.txt 班級ID 姓名 年齡 性別 科目 成績 12 張三 25 男 chinese 50 12 張三 25 男 math 60 12 張三 25 男 english 70 12 李四 20 男 chinese 50 12 李四 20 男 math 50 12 李四 20 男 english 50 12 王芳 19 女 chinese 70 12 王芳 19 女…

FPGA-Verilog-Vivado-軟件使用

這里寫目錄標題 1 軟件配置2 FPGA-7000使用2.1 運行啟動方式 1 軟件配置 編輯器綁定為Vscode&#xff0c;粘貼VS code運行文件的目錄&#xff0c;后綴參數保持不變&#xff1a; 如&#xff1a; D:/Users/xdwu/AppData/Local/Programs/Microsoft VS Code/Code.exe [file name]…

從技術到管理:你必須知道的七個轉變

在職業生涯的道路上&#xff0c;很多技術骨干會逐步轉向管理崗位。這不僅是職位的晉升&#xff0c;更是角色、思維和能力的全方位轉變。以下是七個關鍵的轉變&#xff0c;幫助技術人員順利完成這一跨越。 一、從個人貢獻者到團隊領導者的轉變 在技術崗位上&#xff0c;成功往…

(19)夾鉗(用于送貨)

文章目錄 前言 1 常見的抓手參數 2 參數說明 前言 Copter 支持許多不同的抓取器&#xff0c;這對送貨應用和落瓶很有用。 按照下面的鏈接&#xff08;或側邊欄&#xff09;&#xff0c;根據你的設置了解配置信息。 Electro Permanent Magnet v3 (EPMv3)Electro Permanent M…