最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現五子棋雙人對弈

 更新時間:2022年05月02日 10:09:23   作者:無名程序員就是我  
這篇文章主要為大家詳細介紹了python實現五子棋雙人對弈,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python實現五子棋雙人對弈的具體代碼,供大家參考,具體內容如下

我用的是pygame模塊來制作窗口

代碼如下:

# 1、引入pygame 和 pygame.locals
import pygame
from pygame.locals import *
import time
import sys
?
initChessList = []
initRole = 2 # 代表白子下 2:代表當前是黑子下
resultFlag ?= 0
userFlag = True
?
class StornPoint():
? ? def __init__(self, x, y, value = 0):
? ? ? ? '''
? ? ? ? :param x: 代表x軸坐標
? ? ? ? :param y: 代表y軸坐標
? ? ? ? :param value: 當前坐標點的棋子:0:沒有棋子 1:白子 2:黑子
? ? ? ? '''
? ? ? ? self.x = x
? ? ? ? self.y = y
? ? ? ? self.value = value
? ? ? ? pass
?
?
def initChessSquare(x, y):
? ? '''
? ? 初始化棋盤的坐標
? ? :param x:
? ? :param y:
? ? :return:
? ? '''
? ? # 使用二維列表保存了棋盤是的坐標系,和每個落子點的數值
? ? for i in range(15): ? ? ?# 每一行的交叉點坐標
? ? ? ? rowList = []
? ? ? ? for j in range(15): ?# 每一列的交叉點坐標
? ? ? ? ? ? pointX = x + j*40
? ? ? ? ? ? pointY = y + i*40
? ? ? ? ? ? # value ?= 0
? ? ? ? ? ? sp = StornPoint(pointX, pointY, 0)
? ? ? ? ? ? rowList.append(sp)
? ? ? ? ? ? pass
? ? ? ? initChessList.append(rowList)
? ? pass
?
# 處理事件
def eventHandler():
? ? global userFlag
? ? '''
? ? 監(jiān)聽各種事件
? ? :return:
? ? '''
? ? for event in pygame.event.get():
?
? ? ? ? global ?initRole
? ? ? ? # 監(jiān)聽點積退出按鈕事件
? ? ? ? if event.type == QUIT:
? ? ? ? ? ? pygame.quit()
? ? ? ? ? ? sys.exit()
? ? ? ? ? ? pass
? ? ? ? # 監(jiān)聽鼠標點積事件
? ? ? ? if event.type == MOUSEBUTTONDOWN:
? ? ? ? ? ? x, y = pygame.mouse.get_pos() #
? ? ? ? ? ? print((x, y))
? ? ? ? ? ? i = j = 0
? ? ? ? ? ? for temp in initChessList:
? ? ? ? ? ? ? ? for point in temp:
? ? ? ? ? ? ? ? ? ? if ? x >= point.x - 15 and x <= point.x + 15 \
? ? ? ? ? ? ? ? ? ? ? ? and y >= point.y - 15 and y <= point.y + 15:
? ? ? ? ? ? ? ? ? ? ? ? # 當前區(qū)域沒有棋子,并且是白子下
? ? ? ? ? ? ? ? ? ? ? ? if point.value == 0 and initRole == 1 and userFlag:
? ? ? ? ? ? ? ? ? ? ? ? ? ? point.value = 1
? ? ? ? ? ? ? ? ? ? ? ? ? ? judgeResult(i, j, 1)
? ? ? ? ? ? ? ? ? ? ? ? ? ? initRole = 2 ? ?# 切換棋子顏色
? ? ? ? ? ? ? ? ? ? ? ? ? ? pass
? ? ? ? ? ? ? ? ? ? ? ? elif point.value == 0 and initRole == 2 and userFlag:
? ? ? ? ? ? ? ? ? ? ? ? ? ? point.value = 2
? ? ? ? ? ? ? ? ? ? ? ? ? ? judgeResult(i, j, 2)
? ? ? ? ? ? ? ? ? ? ? ? ? ? initRole = 1 ? ?# 切換棋子顏色
? ? ? ? ? ? ? ? ? ? ? ? ? ? pass
? ? ? ? ? ? ? ? ? ? ? ? break
? ? ? ? ? ? ? ? ? ? ? ? pass
? ? ? ? ? ? ? ? ? ? j += 1
? ? ? ? ? ? ? ? ? ? pass
? ? ? ? ? ? ? ? i += 1
? ? ? ? ? ? ? ? j = 0
? ? ? ? ? ? pass
? ? ? ? pass
?
? ? pass
?
# 判斷輸贏函數
def judgeResult(i, j, value):
? ? global resultFlag
?
? ? flag = False ?# 用于判斷是否已經判決出輸贏
? ? for ?x in ?range(j - 4, j + 5): ?# 水平方向有沒有出現5連
? ? ? ? if x >= 0 and x + 4 < 15 :
? ? ? ? ? ? if initChessList[i][x].value == value and \
? ? ? ? ? ? ? ? initChessList[i][x + 1].value == value and \
? ? ? ? ? ? ? ? initChessList[i][x + 2].value == value and \
? ? ? ? ? ? ? ? initChessList[i][x + 3].value == value and \
? ? ? ? ? ? ? ? initChessList[i][x + 4].value == value :
? ? ? ? ? ? ? ? flag = True
? ? ? ? ? ? ? ? break
? ? ? ? ? ? ? ? pass
? ? for x in range(i - 4, i + 5): ?# 垂直方向有沒有出現5連
? ? ? ? if x >= 0 and x + 4 < 15:
? ? ? ? ? ? if initChessList[x][j].value == value and \
? ? ? ? ? ? ? ? ? ? initChessList[x + 1][j].value == value and \
? ? ? ? ? ? ? ? ? ? initChessList[x + 2][j].value == value and \
? ? ? ? ? ? ? ? ? ? initChessList[x + 3][j].value == value and \
? ? ? ? ? ? ? ? ? ? initChessList[x + 4][j].value == value:
? ? ? ? ? ? ? ? flag = True
? ? ? ? ? ? ? ? break
? ? ? ? ? ? ? ? pass
?
? ? # 判斷東北方向的對角線是否出現了5連
? ? for x, y in zip(range(j + 4, j - 5, -1), range(i - 4, i + 5)):
? ? ? ? if x >= 0 and x+4 ?< 15 and y + 4 >= 0 and y < 15:
? ? ? ? ? ? if initChessList[y][x].value == value and \
? ? ? ? ? ? ? ? ? ? initChessList[y - 1][x + 1].value == value and \
? ? ? ? ? ? ? ? ? ? initChessList[y - 2][x + 2].value == value and \
? ? ? ? ? ? ? ? ? ? initChessList[y - 3][x + 3].value == value and \
? ? ? ? ? ? ? ? ? ? initChessList[y - 4][x + 4].value == value:
? ? ? ? ? ? ? ? flag = True
? ? ? ? ? ? ? ? break
? ? ? ? ? ? ? ? pass
? ? ? ? ? ? pass
? ? ? ? pass
?
? ? # 判斷西北方向的對角是否出現了五連
? ? for x, y in zip(range(j - 4, j + 5), range(i - 4, i + 5)):
? ? ? ? if x >= 0 and x + 4 < 15 and y >= 0 and y + 4 < 15:
? ? ? ? ? ? if initChessList[y][x].value == value and \
? ? ? ? ? ? ? ? ? ? initChessList[y + 1][x + 1].value == value and \
? ? ? ? ? ? ? ? ? ? initChessList[y + 2][x + 2].value == value and \
? ? ? ? ? ? ? ? ? ? initChessList[y + 3][x + 3].value == value and \
? ? ? ? ? ? ? ? ? ? initChessList[y + 4][x + 4].value == value:
? ? ? ? ? ? ? ? flag = True
? ? ? ? ? ? ? ? break
? ? ? ? ? ? ? ? pass
? ? ? ? ? ? pass
? ? ? ? pass
?
? ? if flag:
? ? ? ? resultFlag = value
? ? ? ? pass
? ? pass
?
# 加載素材
def main():
? ? initRole = 2 ?# 代表白子下 2:代表當前是黑子下
? ? global resultFlag, initChessList
? ? initChessSquare(27, 27) ?# 初始化棋牌
? ? pygame.init() ? ? ? ? ? ?# 初始化游戲環(huán)境
? ? # 創(chuàng)建游戲窗口
? ? screen = pygame.display.set_mode((620,620), 0, 0) # 第一個參數是元組:窗口的長和寬
? ? # 添加游戲標題
? ? pygame.display.set_caption("五子棋小游戲")
? ? # 圖片的加載
? ? background = pygame.image.load('images/bg.png')
? ? blackStorn = pygame.image.load('images/storn_black.png')
? ? whiteStorn = pygame.image.load('images/storn_white.png')
? ? winStornW = pygame.image.load('images/white.png')
? ? winStornB = pygame.image.load('images/black.png')
? ? rect = blackStorn.get_rect()
?
? ? while True:
? ? ? ? screen.blit(background, (0, 0))
? ? ? ? # 更新棋盤棋子
? ? ? ? for temp in initChessList:
? ? ? ? ? ? for point in temp:
? ? ? ? ? ? ? ? if point.value == 1:
? ? ? ? ? ? ? ? ? ? screen.blit(whiteStorn, (point.x - 18, point.y - 18))
? ? ? ? ? ? ? ? ? ? pass
? ? ? ? ? ? ? ? elif point.value == 2:
? ? ? ? ? ? ? ? ? ? screen.blit(blackStorn, (point.x - 18, point.y - 18))
? ? ? ? ? ? ? ? ? ? pass
? ? ? ? ? ? ? ? pass
? ? ? ? ? ? pass
? ? ? ? # 如果已經判決出輸贏
? ? ? ? if resultFlag > 0:
? ? ? ? ? ? initChessList = [] ? ? ?# 清空棋盤
? ? ? ? ? ? initChessSquare(27, 27) # 重新初始化棋盤
? ? ? ? ? ? if resultFlag == 1:
? ? ? ? ? ? ? ? screen.blit(winStornW, (50,100))
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? screen.blit(winStornB, (50,100))
? ? ? ? ? ? pass
? ? ? ? pygame.display.update()
?
? ? ? ? if resultFlag >0:
? ? ? ? ? ? time.sleep(3)
? ? ? ? ? ? resultFlag = 0
? ? ? ? ? ? pass
? ? ? ? eventHandler()
? ? ? ? pass
?
? ? pass
?
if __name__ == "__main__":
? ? main()
? ? pass

插圖:放在同一目錄下的images文件夾里

bg.png

storn_white.png

storn_black.png

white.png

black.png

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Python庫functools示例詳解

    Python庫functools示例詳解

    Python?的?functools?模塊提供了一些常用的高階函數,也就是用于處理其它函數的特殊函數。換言之,就是能使用該模塊對?所有可調用對象(?即?參數?或(和)?返回值?為其他函數的函數?)?進行處理,這篇文章主要介紹了Python庫functools詳解,需要的朋友可以參考下
    2023-01-01
  • Python2和Python3中@abstractmethod使用方法

    Python2和Python3中@abstractmethod使用方法

    這篇文章主要介紹了Python2和Python3中@abstractmethod使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • python實現畫循環(huán)圓

    python實現畫循環(huán)圓

    今天小編就為大家分享一篇python實現畫循環(huán)圓,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python中__init__.py文件的作用詳解

    Python中__init__.py文件的作用詳解

    __init__.py 文件的作用是將文件夾變?yōu)橐粋€Python模塊,Python 中的每個模塊的包中,都有__init__.py 文件.這篇文章主要介紹了Python中__init__.py文件的作用詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • Python?pandas中apply函數簡介以及用法詳解

    Python?pandas中apply函數簡介以及用法詳解

    apply()函數是pandas里面所有函數中自由度最高的函數, apply()函數的參數是一個函數指針,這里可以使用lambda表達式幫助簡化代碼,下面這篇文章主要給大家介紹了關于Python?pandas中apply函數簡介以及用法的相關資料,需要的朋友可以參考下
    2022-09-09
  • python opencv之SURF算法示例

    python opencv之SURF算法示例

    這篇文章主要介紹了python opencv之SURF算法示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • Python使用pandas模塊實現表之間的關聯

    Python使用pandas模塊實現表之間的關聯

    在數據分析和處理中,表之間的關聯是非常常見的操作,本文為大家介紹了pandas中實現表之間的關聯有四種方式,感興趣的小伙伴可以了解一下
    2023-07-07
  • 如何利用python實現圖片批處理

    如何利用python實現圖片批處理

    這篇文章主要給大家介紹了關于如何利用python實現圖片批處理的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • 淺談Python中chr、unichr、ord字符函數之間的對比

    淺談Python中chr、unichr、ord字符函數之間的對比

    chr、unichr、ord在Python中都可以被用作字符類型轉換,這里我們就來淺談Python中chr、unichr、ord字符函數之間的對比,需要的朋友可以參考下
    2016-06-06
  • Python使用pymupdf實現PDF內容搜索并顯示功能

    Python使用pymupdf實現PDF內容搜索并顯示功能

    在日常工作和學習中,我們可能需要查找和提取PDF文件中的特定內容,本文將介紹如何使用pymupdf實現PDF內容搜索并顯示的功能,需要的可以參考下
    2023-08-08

最新評論

临沧市| 肃宁县| 达尔| 铜陵市| 沾化县| 栾城县| 铜梁县| 巨野县| 龙陵县| 邳州市| 左贡县| 左贡县| 宁安市| 小金县| 永安市| 大埔区| 深圳市| 南江县| 印江| 彭州市| 正宁县| 小金县| 蒙山县| 沽源县| 确山县| 辰溪县| 宁国市| 武清区| 响水县| 高淳县| 桓仁| 思南县| 双流县| 准格尔旗| 佛教| 广南县| 塘沽区| 曲水县| 德江县| 通许县| 区。|