直接上代码:
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 =