日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3 pygame 黑白棋 翻转棋_Python3 + pygame 实现黑白棋(翻转棋)

發布時間:2023/12/14 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 pygame 黑白棋 翻转棋_Python3 + pygame 实现黑白棋(翻转棋) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

直接上代碼:

import pygame

# 確認導入成功

print(pygame.ver)

EMPTY = 0

BLACK = 1

WHITE = 2

MOVEOUT = 0

blackColor = [0, 0, 0]

whiteColor = [255, 255, 255]

# 權重

WEIGHT = [[120, -20, 20, 5, 5, 20, -20, 120],

[-20, -40, -5, -5, -5, -5, -40, -20],

[20, -5, 15, 3, 3, 15, -5, 20],

[5, -5, 3, 3, 3, 3, -5, 5],

[5, -5, 3, 3, 3, 3, -5, 5],

[20, -5, 15, 3, 3, 15, -5, 20],

[-20, -40, -5, -5, -5, -5, -40, -20],

[120, -20, 20, 5, 5, 20, -20, 120]]

# 棋盤類

class AppleBoard(object):

def __init__(self):

self.board = [[]] * 8

self.reset()

def reset(self):

# row表示行 col表示列 重置棋盤

for row in range(len(self.board)):

self.board[row] = [EMPTY] * 8

self.board[3][4] = WHITE

self.board[4][3] = WHITE

self.board[3][3] = BLACK

self.board[4][4] = BLACK

def move(self, row, col, isBlack):

# isBlack表示當前點是黑棋還是白棋

flag = False

# 用來解耦 將是否可以落子與實際落子分開

resultLi = []

# 水平 豎直 正斜 反斜 四個方向上的坐標各存在一個列表里 并且用XXXCount記錄落子點在這四個列表中的位置

checkLiShuiping = []

shuipingCount = col

checkLiShuzhi = []

shuzhiCount = row

checkLiZhengxie = []

zhengxieCount = None

checkLiFanxie = []

fanxieCount = None

if self.board[row][col] == EMPTY:

qiziColor = BLACK if isBlack else WHITE

fanseColor = WHITE if isBlack else BLACK

checkLiShuiping = self.board[row]

checkLiShuzhi = [self.board[i][col] for i in range(8)]

# 找正斜左上角的點:

# 先去掉無效點:

if [row, col] in [[0, 7], [0, 6], [1, 7], [6, 0], [7, 0], [7, 1]]:

pass

else:

zhengxieRow = row

zhengxieCol = col

while zhengxieRow > 0 and zhengxieCol > 0:

zhengxieRow -= 1

zhengxieCol -= 1

zhengxieCount = row if zhengxieRow == 0 else col

while zhengxieRow <= 7 and zhengxieCol <= 7:

checkLiZhengxie.append(self.board[zhengxieRow][zhengxieCol])

zhengxieRow += 1

zhengxieCol += 1

# 找反斜左下角的點:

if [row, col] in [[0, 0], [0, 1], [1, 0], [6, 7], [7, 6], [7, 7]]:

pass

else:

fanxieRow = row

fanxieCol = col

while fanxieRow < 7 and fanxieCol > 0:

fanxieRow += 1

fanxieCol -= 1

fanxieCount = 7 - row if fanxieRow == 7 else col

while fanxieRow >= 0 and fanxieCol <= 7:

checkLiFanxie.append(self.board[fanxieRow][fanxieCol])

fanxieRow -= 1

fanxieCol += 1

# 先判斷水平情況:

# 先去掉無效情況

# 加一個變量判斷是否是貼邊的情況 如最邊的是白子這時在這個白子旁邊落下一顆黑子 則這個白子不應該變色

if BLACK not in checkLiShuiping or WHITE not in checkLiShuiping:

resultLi.append(shuipingCount)

resultLi.append(0)

resultLi.append(0)

else:

tiebian = True

changeCount = -1

emptyCount = 0

if shuipingCount > 1:

for i in range(shuipingCount, -1, -1):

if checkLiShuiping[i] == fanseColor:

changeCount += 1

elif checkLiShuiping[i] == qiziColor:

# 換句話說只有再次遇到相同顏色棋子才有效

tiebian = False

break

elif checkLiShuiping[i] == EMPTY:

changeCount = 0

emptyCount += 1

# 判斷除了落子點是空之外還有其他空點則直接退出且changeCount為0

if emptyCount >= 2:

break

else:

changeCount = 0

resultLi.append(shuipingCount)

resultLi.append(changeCount)

if changeCount > 0 and tiebian is False:

flag = True

changeCount = -1

emptyCount = 0

tiebian = True

if shuipingCount < 6:

for i in range(shuipingCount, 8):

if checkLiShuiping[i] == fanseColor:

changeCount += 1

elif checkLiShuiping[i] == qiziColor:

tiebian = False

break

elif checkLiShuiping[i] == EMPTY:

changeCount = 0

emptyCount += 1

if emptyCount >= 2:

break

else:

changeCount = 0

resultLi.append(changeCount)

if changeCount > 0 and tiebian is False:

flag = True

# 豎直情況:

if BLACK not in checkLiShuzhi or WHITE not in checkLiShuzhi:

resultLi.append(shuzhiCount)

resultLi.append(0)

resultLi.append(0)

else:

changeCount = -1

emptyCount = 0

tiebian = True

# print(shuzhiCount)

if shuzhiCount > 1:

for i in range(shuzhiCount, -1, -1):

if checkLiShuzhi[i] == fanseColor:

changeCount += 1

elif checkLiShuzhi[i] == qiziColor:

tiebian = False

break

elif checkLiShuzhi[i] == EMPTY:

changeCount = 0

emptyCount += 1

if emptyCount >= 2:

break

else:

changeCount = 0

resultLi.append(shuzhiCount)

resultLi.append(changeCount)

if changeCount > 0 and tiebian is False:

flag = True

emptyCount = 0

changeCount = -1

tiebian = True

if shuzhiCount < 6:

for i in range(shuzhiCount, 8):

if checkLiShuzhi[i] == fanseColor:

changeCount += 1

elif checkLiShuzhi[i] == qiziColor:

tiebian = False

break

elif checkLiShuzhi[i] == EMPTY:

changeCount = 0

emptyCount += 1

if emptyCount >= 2:

break

else:

changeCount = 0

resultLi.append(changeCount)

if changeCount > 0 and tiebian is False:

flag = True

# 正斜情況:

if BLACK not in checkLiZhengxie or WHITE not in checkLiZhengxie or not checkLiZhengxie:

resultLi.append(0)

resultLi.append(0)

elif zhengxieCount is None:

resultLi.append(0)

resultLi.append(0)

else:

tiebian = True

changeCount = -1

emptyCount = 0

if zhengxieCount > 1:

for i in range(zhengxieCount, -1, -1):

if checkLiZhengxie[i] == fanseColor:

changeCount += 1

elif checkLiZhengxie[i] == qiziColor:

tiebian = False

break

elif checkLiZhengxie[i] == EMPTY:

changeCount = 0

emptyCount += 1

if emptyCount >= 2:

break

else:

changeCount = 0

resultLi.append(changeCount)

if changeCount > 0 and tiebian is False:

flag = True

changeCount = -1

emptyCount = 0

tiebian = True

if zhengxieCount < len(checkLiZhengxie) - 2:

for i in range(zhengxieCount, len(checkLiZhengxie)):

if checkLiZhengxie[i] == fanseColor:

changeCount += 1

elif checkLiZhengxie[i] == qiziColor:

tiebian = False

break

elif checkLiZhengxie[i] == EMPTY:

changeCount = 0

emptyCount += 1

if emptyCount >= 2:

break

else:

changeCount = 0

resultLi.append(changeCount)

if changeCount > 0 and tiebian is False:

flag = True

# 反斜情況:

if BLACK not in checkLiFanxie or WHITE not in checkLiFanxie or not checkLiFanxie:

resultLi.append(0)

resultLi.append(0)

elif fanxieCount is None:

resultLi.append(0)

resultLi.append(0)

else:

tiebian = True

changeCount = -1

emptyCount = 0

if fanxieCount > 1:

for i in range(fanxieCount, -1, -1):

if checkLiFanxie[i] == fanseColor:

changeCount += 1

elif checkLiFanxie[i] == qiziColor:

tiebian = False

break

elif checkLiFanxie[i] == EMPTY:

changeCount = 0

emptyCount += 1

if emptyCount >= 2:

break

else:

changeCount = 0

resultLi.append(changeCount)

if changeCount > 0 and tiebian is False:

flag = True

changeCount = -1

emptyCount = 0

tiebian = True

if fanxieCount < len(checkLiFanxie) - 2:

for i in range(fanxieCount, len(checkLiFanxie)):

if checkLiFanxie[i] == fanseColor:

changeCount += 1

elif checkLiFanxie[i] == qiziColor:

tiebian = False

break

elif checkLiFanxie[i] == EMPTY:

changeCount = 0

emptyCount += 1

if emptyCount >= 2:

break

else:

changeCount = 0

resultLi.append(changeCount)

if changeCount > 0 and tiebian is False:

flag = True

return [flag, resultLi]

def realMove(self, para, isBlack, row, col):

qiziColor = BLACK if isBlack else WHITE

shuipingCount = para[0]

changCountShuipingZuo = para[1]

changCountShuipingYou = para[2]

shuzhiCount = para[3]

changCountShuzhiZuo = para[4]

changCountShuzhiYou = para[5]

changCountZhengxieZuo = para[6]

changCountZhengxieYou = para[7]

changCountFanxieZuo = para[8]

changCountFanxieYou = para[9]

for j in range(changCountShuipingZuo + 1):

self.board[row][shuipingCount - j] = qiziColor

for j in range(changCountShuipingYou + 1):

self.board[row][shuipingCount + j] = qiziColor

for j in range(changCountShuzhiZuo + 1):

self.board[shuzhiCount - j][col] = qiziColor

for j in range(changCountShuzhiYou + 1):

self.board[shuzhiCount + j][col] = qiziColor

for j in range(changCountZhengxieZuo + 1):

self.board[row - j][col - j] = qiziColor

for j in range(changCountZhengxieYou + 1):

self.board[row + j][col + j] = qiziColor

for j in range(changCountFanxieZuo + 1):

self.board[row + j][col - j] = qiziColor

for j in range(changCountFanxieYou + 1):

self.board[row - j][col + j] = qiziColor

def draw(self, screen):

for h in range(1, 10):

pygame.draw.line(screen, blackColor, [40, h * 40], [320, h * 40], 1)

pygame.draw.line(screen, blackColor, [h * 40, 40], [h * 40, 320], 1)

pygame.draw.rect(screen, blackColor, [36, 36, 289, 289], 3)

for row in range(len(self.board)):

for col in range(len(self.board[row])):

if self.board[row][col] != EMPTY:

ccolor = blackColor if self.board[row][col] == BLACK else whiteColor

pos = [40 * (col + 1), 40 * (row + 1)]

pygame.draw.circle(screen, ccolor, pos, 18, 0)

def simpleCom(board, isBlack):

aroundLi = []

linshiLi = []

coordinate = []

checkBoard = [i[:] for i in board.board]

for i in range(8):

checkBoard[i].insert(0, 0)

checkBoard[i].append(0)

checkBoard.insert(0, [0] * 10)

checkBoard.append([0] * 10)

for i in range(1, 9):

for j in range(1, 9):

if checkBoard[i][j] == EMPTY and (checkBoard[i - 1][j - 1] != EMPTY or checkBoard[i - 1][j] != EMPTY or

checkBoard[i - 1][j + 1] != EMPTY or checkBoard[i][

j - 1] != EMPTY or

checkBoard[i][j] != EMPTY or checkBoard[i][j + 1] != EMPTY or

checkBoard[i + 1][j - 1] != EMPTY or checkBoard[i + 1][

j] != EMPTY or

checkBoard[i + 1][j + 1] != EMPTY):

linshiLi.append([i - 1, j - 1] if board.move(i - 1, j - 1, isBlack)[0] is True else None)

for i in linshiLi:

if i not in aroundLi:

aroundLi.append(i)

aroundLi.remove(None)

if not aroundLi:

return [False, []]

count = -40

for i in aroundLi:

if WEIGHT[i[0]][i[1]] > count:

count = WEIGHT[i[0]][i[1]]

coordinate = i

return [True, coordinate]

def midCom(board, isBlack):

if isBlack:

qiziColor = BLACK

else:

qiziColor = WHITE

aroundLi = []

oldBoard = [i[:] for i in board.board]

checkBoard = [i[:] for i in board.board]

for i in range(8):

checkBoard[i].insert(0, 0)

checkBoard[i].append([0] * 10)

checkBoard.insert(0, [0] * 10)

checkBoard.append([0] * 10)

for i in range(1, 9):

for j in range(1, 9):

if checkBoard[i][j] == EMPTY and (checkBoard[i - 1][j - 1] != EMPTY or checkBoard[i - 1][j] != EMPTY or

checkBoard[i - 1][j + 1] != EMPTY or checkBoard[i][

j - 1] != EMPTY or

checkBoard[i][j] != EMPTY or checkBoard[i][j + 1] != EMPTY or

checkBoard[i + 1][j - 1] != EMPTY or checkBoard[i + 1][

j] != EMPTY or

checkBoard[i + 1][j + 1] != EMPTY):

aroundLi.append([i - 1, j - 1])

if not aroundLi:

return [False, []]

else:

pointDic = {}

count = 0

for i in aroundLi:

flag, para = board.move(i[0], i[1], isBlack)

if flag:

board.realMove(para, isBlack, i[0], i[1])

for row in range(len(board.board)):

for col in range(len(board.board[row])):

if board.board[row][col] == qiziColor:

count += WEIGHT[row][col]

if count not in list(pointDic.keys()):

pointDic[count] = i

count = 0

board.board = [i[:] for i in oldBoard]

resCoordinate = pointDic[max(pointDic.keys())]

return [True, resCoordinate]

def isWin(board, isBlack):

# 勝利有三種結算方式 一是無子 二是雙方均無子可下 三是棋盤下滿算數量

global MOVEOUT

allChess = [board.board[i][j] for i in range(8) for j in range(8)]

# 情況1 單方無子

if WHITE not in allChess:

return "黑棋勝"

if BLACK not in allChess:

return "白棋勝"

if EMPTY in allChess:

# 正常情況下黑子和白子是連在一起的 所以只需要檢查周圍一圈是否可以落子即可

aroundLi = []

moveornotLi = []

# 臨時給棋盤對象最外面加一圈 這里若不使用[:][:]則為同一對象

# !!!并且如果只是 checkBoard = board.board[:] 則內部的數組為同一對象

checkBoard = [i[:] for i in board.board]

for i in range(8):

checkBoard[i].insert(0, 0)

checkBoard[i].append(0)

checkBoard.insert(0, [0] * 10)

checkBoard.append([0] * 10)

for i in range(1, 9):

for j in range(1, 9):

if checkBoard[i][j] == EMPTY and (checkBoard[i - 1][j - 1] != EMPTY or checkBoard[i - 1][j] != EMPTY or

checkBoard[i - 1][j + 1] != EMPTY or checkBoard[i][j - 1] != EMPTY or

checkBoard[i][j] != EMPTY or checkBoard[i][j + 1] != EMPTY or

checkBoard[i + 1][j - 1] != EMPTY or checkBoard[i + 1][j] != EMPTY or

checkBoard[i + 1][j + 1] != EMPTY):

aroundLi.append([i - 1, j - 1])

for i in aroundLi:

moveornotLi.append(board.move(i[0], i[1], isBlack)[0])

moveornot = True if True in moveornotLi else False

if not moveornot:

MOVEOUT += 1

return "換棋子繼續下"

if MOVEOUT == 2:

# 如果連續沒下棋換邊2次 則證明雙方均無子可下

baiqiCount = allChess.count(WHITE)

heiqiCount = allChess.count(BLACK)

if baiqiCount > heiqiCount:

return "白棋勝"

elif baiqiCount < heiqiCount:

return "黑棋勝"

else:

return "和棋"

return "一般情況"

if EMPTY not in allChess:

baiqiCount = allChess.count(WHITE)

heiqiCount = allChess.count(BLACK)

if baiqiCount > heiqiCount:

return "白棋勝"

elif baiqiCount < heiqiCount:

return "黑棋勝"

else:

return "和棋"

MOVEOUT = 0

def main():

# 創建棋盤對象

board = AppleBoard()

isBlack = True

pygame.init()

pygame.display.set_caption("黑白棋")

screen = pygame.display.set_mode((360, 360))

screen.fill([125, 95, 24])

board.draw(screen)

pygame.display.flip()

running = True

pause = False

HUIQI = None

while running:

if not isBlack:

point = simpleCom(board, isBlack)

if not point[0]:

isBlack = not isBlack

continue

moveRes = board.move(point[1][0], point[1][1], isBlack)

if moveRes[0]:

board.realMove(moveRes[1], isBlack, point[1][0], point[1][1])

screen.fill([125, 95, 24])

board.draw(screen)

pygame.display.flip()

res = isWin(board, isBlack)

if res == "一般情況" or res == "換棋子繼續下":

isBlack = not isBlack

continue

elif res == "黑棋勝" or res == "白棋勝" or res == "和棋":

pause = True

while pause:

print(res)

for event in pygame.event.get():

if event.type == pygame.QUIT:

pause = False

running = False

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_SPACE:

board.reset()

screen.fill([125, 95, 24])

board.draw(screen)

pygame.display.flip()

isBlack = True

pause = False

else:

for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False

elif event.type == pygame.KEYUP:

pass

elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:

x, y = event.pos

row = round((y - 40) / 40)

col = round((x - 40) / 40)

moveRes = board.move(row, col, isBlack)

# board.realMove(moveRes[1], isBlack, row, col)

# screen.fill([125, 95, 24])

# board.draw(screen)

# pygame.display.flip()

# isBlack = not isBlack

res = isWin(board, isBlack)

if res == "一般情況" or res == "換棋子繼續下":

if moveRes[0]:

board.realMove(moveRes[1], isBlack, row, col)

screen.fill([125, 95, 24])

board.draw(screen)

pygame.display.flip()

isBlack = not isBlack

elif res == "黑棋勝" or res == "白棋勝" or res == "和棋":

pause = True

while pause:

# print(res)

for event in pygame.event.get():

if event.type == pygame.QUIT:

pause = False

running = False

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_SPACE:

board.reset()

screen.fill([125, 95, 24])

board.draw(screen)

pygame.display.flip()

isBlack = True

pause = False

if __name__ == '__main__':

main()

之前的有BUG 這是改進之后加了AI的 目前默認是簡單電腦 將489行的simpleCom改成midCom就是中等電腦 還沒做選項 后面有空了再加上

點贊

收藏

分享

文章舉報

本能優勢

發布了1 篇原創文章 · 獲贊 0 · 訪問量 79

私信

關注

總結

以上是生活随笔為你收集整理的python3 pygame 黑白棋 翻转棋_Python3 + pygame 实现黑白棋(翻转棋)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。