直接上代码:

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 实现黑白棋(翻转棋)相关推荐

  1. 黑白棋出现pass 的条件 java_JAVA黑白棋之学习感悟

    前言 这是我来到蓝杰之后的第一个学习感悟,阶段成果也是我第一个觉得小有成就的作品,不在于所用的知识有多么高深,而在与这是第一个凝结了失败.努力.成功这样颇有曲折经历的项目,使我收获颇多. 下面切入正题 ...

  2. python pygame模块按键延迟_用pygame做游戏时,用pygame.KEYDOWN来实现的按键控制“不灵敏”能怎么解决?...

    这样写就十分灵活了 就是每个按键添加了一个k变量来检测按键是否被按着. 每一对儿方向相对的按键加了一个f变量来记录两个按键谁最后按下的. speed=[x,y] for event in pygame ...

  3. Pygame学习笔记6:使用Pygame精灵以及Escape the Dragon

    上一章介绍了位图编程的相关内容,这一章就开始实现动画操作,那么主要就是要来了解pygame.sprite模块. 在pygame.sprite模块中,有一个名为Sprite的类,我们需要对其进行扩展使其 ...

  4. python pygame小游戏_python:利用pygame实现消消乐小游戏

    消消乐记分小游戏GUI界面 文件结构规划 定义config.py文件存储相关参数:包括界面的宽高,整个方格行列个数,总格数等等. 定义utils.py文件用于存放基础的类和函数:包括整个消除拼图类,游 ...

  5. python中的pygame弹球游戏代码_【pygame系列 第三课 弹球游戏-上 】

    python我们可以做文字版的游戏,比如猜数字游戏,21点游戏.那python可以做图形界面的游戏吗?偷偷告诉你,用pygame库就可以实现了.pygame是python中专门用来编写游戏的一个引擎库 ...

  6. vb编写脚本能让计算机屏幕黑屏,,win7上设置颜色黑屏

    当前位置:我的异常网» VB » ,win7上设置颜色黑屏 ,win7上设置颜色黑屏 www.myexceptions.net  网友分享于:2013-12-16  浏览:7次 求助,win7下设置颜 ...

  7. android 黑边边框,手机屏幕边缘的黑边是什么呢?

    原标题:手机屏幕边缘的黑边是什么呢? 手机"黑边" 2019/4/4 现在的手机屏幕尺寸现在是变得越来越大了,而且手机屏幕用上的技术也越来越优秀.不过细心的用户就可以发现,在我们的 ...

  8. dvi黑屏解决方法_赛博朋克2077黑梦黑屏怎么办 黑梦BUG全黑模式解决方法

    赛博朋克2077黑梦任务中,许多玩家们会遇到黑屏的情况,屏幕一下子就全黑模式了似得.黑梦黑屏了怎么办?黑梦BUG要怎么解决呢?这就来了解下吧. "能让玩家相信它不是bug的bug,还反向给游 ...

  9. 虚拟机黑裙加载硬盘_虚拟机黑群晖拯救实体黑群晖硬盘数据-全网首发。

    来吧兄弟,一起玩一起讨论! 您需要 登录 才可以下载或查看,没有帐号?注册 x 本帖最后由 woshiwaxiu 于 2015-8-5 15:01 编辑 昨天,黑群晖突然监测不到了,反复重启,发现原来 ...

最新文章

  1. Numpy 中的 arange 函数
  2. OpenCV与c语言图像融合
  3. 直击微软MIX11 聚焦IE10、Silverlight5、WP7
  4. 怎样呵护友谊_怎样呵护友谊(作文)
  5. 一句话概括互联网巨头,简直不要太真实!哈哈哈哈哈哈哈哈哈
  6. php自定义弹窗,自定义弹窗Style样式
  7. 查找三 哈希表的查找
  8. python中的reduce函数用法
  9. 淘宝Web服务器Tengine正式开源
  10. 基于知识引入的情感分析
  11. Integer与int比较的坑
  12. 【开发心得】json解析报错Uncaught SyntaxError: Unexpected identifier的解决方法
  13. 敖丙大佬的《吐血整理》-顶级程序员书单集 JAVA
  14. win10 安装mysql 卡死_win10安装Mysql5.5卡住假死
  15. Facebook登陆问题和在Android 11 上的问题
  16. 如何用LaTeX写一个PPT
  17. JavaScript整理
  18. hash碰撞处理方法
  19. CUDA学习(十一):原子操作实现向量内积
  20. iOS各个版本的特性和差别

热门文章

  1. python01python的基础知识点(一)
  2. Wireshark配置显示IP地理位置信息
  3. C++ nth_element 介绍
  4. 视频教程-2020年软考系统集成项目管理工程师应用技术软考视频教程-软考
  5. Black Duck
  6. 章泽天又晒27岁生日照:刚刚又重返18岁!
  7. Orleans 2.0 官方文档 —— 3.1 核心概念 - 什么是grain
  8. 【Azure Data Platform】ETL工具(19)——Azure Databricks
  9. 图像特征提取(纹理特征)
  10. Explaining away