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

本文展示了如何使用Python3和Pygame库创建一个黑白棋(翻转棋)游戏。通过游戏类AppleBoard实现了棋盘逻辑,包括棋子移动、判断胜负等功能,并提供了简单和中等难度的AI对战模式。
摘要由CSDN通过智能技术生成

直接上代码:

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值