python實(shí)現(xiàn)五子棋雙人對(duì)弈
本文實(shí)例為大家分享了python實(shí)現(xiàn)五子棋雙人對(duì)弈的具體代碼,供大家參考,具體內(nèi)容如下
我用的是pygame模塊來(lái)制作窗口
代碼如下:
# 1、引入pygame 和 pygame.locals import pygame from pygame.locals import * import time import sys ? initChessList = [] initRole = 2 # 代表白子下 2:代表當(dāng)前是黑子下 resultFlag ?= 0 userFlag = True ? class StornPoint(): ? ? def __init__(self, x, y, value = 0): ? ? ? ? ''' ? ? ? ? :param x: 代表x軸坐標(biāo) ? ? ? ? :param y: 代表y軸坐標(biāo) ? ? ? ? :param value: 當(dāng)前坐標(biāo)點(diǎn)的棋子:0:沒(méi)有棋子 1:白子 2:黑子 ? ? ? ? ''' ? ? ? ? self.x = x ? ? ? ? self.y = y ? ? ? ? self.value = value ? ? ? ? pass ? ? def initChessSquare(x, y): ? ? ''' ? ? 初始化棋盤的坐標(biāo) ? ? :param x: ? ? :param y: ? ? :return: ? ? ''' ? ? # 使用二維列表保存了棋盤是的坐標(biāo)系,和每個(gè)落子點(diǎn)的數(shù)值 ? ? for i in range(15): ? ? ?# 每一行的交叉點(diǎn)坐標(biāo) ? ? ? ? rowList = [] ? ? ? ? for j in range(15): ?# 每一列的交叉點(diǎn)坐標(biāo) ? ? ? ? ? ? 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)聽(tīng)各種事件 ? ? :return: ? ? ''' ? ? for event in pygame.event.get(): ? ? ? ? ? global ?initRole ? ? ? ? # 監(jiān)聽(tīng)點(diǎn)積退出按鈕事件 ? ? ? ? if event.type == QUIT: ? ? ? ? ? ? pygame.quit() ? ? ? ? ? ? sys.exit() ? ? ? ? ? ? pass ? ? ? ? # 監(jiān)聽(tīng)鼠標(biāo)點(diǎ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: ? ? ? ? ? ? ? ? ? ? ? ? # 當(dāng)前區(qū)域沒(méi)有棋子,并且是白子下 ? ? ? ? ? ? ? ? ? ? ? ? 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 ? # 判斷輸贏函數(shù) def judgeResult(i, j, value): ? ? global resultFlag ? ? ? flag = False ?# 用于判斷是否已經(jīng)判決出輸贏 ? ? for ?x in ?range(j - 4, j + 5): ?# 水平方向有沒(méi)有出現(xiàn)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): ?# 垂直方向有沒(méi)有出現(xiàn)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 ? ? ? # 判斷東北方向的對(duì)角線是否出現(xiàn)了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 ? ? ? # 判斷西北方向的對(duì)角是否出現(xiàn)了五連 ? ? 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:代表當(dāng)前是黑子下 ? ? global resultFlag, initChessList ? ? initChessSquare(27, 27) ?# 初始化棋牌 ? ? pygame.init() ? ? ? ? ? ?# 初始化游戲環(huán)境 ? ? # 創(chuàng)建游戲窗口 ? ? screen = pygame.display.set_mode((620,620), 0, 0) # 第一個(gè)參數(shù)是元組:窗口的長(zhǎng)和寬 ? ? # 添加游戲標(biāo)題 ? ? 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 ? ? ? ? # 如果已經(jīng)判決出輸贏 ? ? ? ? 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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python3利用Qt5實(shí)現(xiàn)簡(jiǎn)易的五子棋游戲
- Python實(shí)現(xiàn)雙人五子棋對(duì)局
- python實(shí)現(xiàn)五子棋算法
- python實(shí)現(xiàn)人人對(duì)戰(zhàn)的五子棋游戲
- python版本五子棋的實(shí)現(xiàn)代碼
- python實(shí)現(xiàn)五子棋小游戲
- python pygame實(shí)現(xiàn)五子棋小游戲
- python實(shí)現(xiàn)簡(jiǎn)單五子棋游戲
- python實(shí)現(xiàn)五子棋游戲(pygame版)
- python代碼實(shí)現(xiàn)五子棋游戲
相關(guān)文章
利用Python pandas對(duì)Excel進(jìn)行合并的方法示例
這篇文章主要給大家介紹了關(guān)于利用Python pandas對(duì)Excel進(jìn)行合并的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11使用?PyQt5?設(shè)計(jì)下載遠(yuǎn)程服務(wù)器日志文件程序的思路
這篇文章主要介紹了使用?PyQt5?設(shè)計(jì)下載遠(yuǎn)程服務(wù)器日志文件程序,借助 PyQt5 強(qiáng)大的能力,我們可以通過(guò)“拖拉拽”的形式很容易地實(shí)現(xiàn)桌面端程序,只需要將原來(lái)的 Python 腳本綁定到 UI 程序的事件中,就實(shí)現(xiàn)了命令行程序到桌面程序的演進(jìn),需要的朋友可以參考下2022-11-11Python進(jìn)程池Pool應(yīng)用實(shí)例分析
這篇文章主要介紹了Python進(jìn)程池Pool應(yīng)用,結(jié)合實(shí)例形式分析了Python進(jìn)程池Pool功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-11-11詳解Python如何實(shí)現(xiàn)發(fā)送帶附件的電子郵件
SMTP(Simple Mail Transfer Protocol)即簡(jiǎn)單郵件傳輸協(xié)議,它是一組用于由源地址到目的地址傳送郵件的規(guī)則,由它來(lái)控制信件的中轉(zhuǎn)方式。本文將利用SMTP實(shí)現(xiàn)發(fā)送帶附件的電子郵件,感興趣的可以了解一下2023-04-04Python wxPython庫(kù)Core組件BoxSizer用法示例
這篇文章主要介紹了Python wxPython庫(kù)Core組件BoxSizer用法,結(jié)合實(shí)例形式分析了wxPython BoxSizer布局管理相關(guān)使用方法及操作注意事項(xiàng),需要的朋友可以參考下2018-09-09在Mac中配置Python虛擬環(huán)境過(guò)程解析
這篇文章主要介紹了在Mac中配置Python虛擬環(huán)境過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06