直接上代码:
import pygame
print(pygame.ver)EMPTY = 0
BLACK = 1
WHITE = 2
MOVEOUT = 0blackColor = [0, 0, 0]
whiteColor = [255, 255, 255]
class AppleBoard(object):def __init__(self):self.board = [[]] * 8self.reset()def reset(self):for row in range(len(self.board)):self.board[row] = [EMPTY] * 8self.board[3][4] = WHITEself.board[4][3] = WHITEself.board[3][3] = BLACKself.board[4][4] = BLACKdef move(self, row, col, isBlack):flag = FalseresultLi = []checkLiShuiping = []shuipingCount = colcheckLiShuzhi = []shuzhiCount = rowcheckLiZhengxie = []zhengxieCount = NonecheckLiFanxie = []fanxieCount = Noneif self.board[row][col] == EMPTY:qiziColor = BLACK if isBlack else WHITEfanseColor = WHITE if isBlack else BLACKcheckLiShuiping = 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]]:passelse:zhengxieRow = rowzhengxieCol = colwhile zhengxieRow > 0 and zhengxieCol > 0:zhengxieRow -= 1zhengxieCol -= 1zhengxieCount = row if zhengxieRow == 0 else colwhile zhengxieRow <= 7 and zhengxieCol <= 7:checkLiZhengxie.append(self.board[zhengxieRow][zhengxieCol])zhengxieRow += 1zhengxieCol += 1if [row, col] in [[0, 0], [0, 1], [1, 0], [6, 7], [7, 6], [7, 7]]:passelse:fanxieRow = rowfanxieCol = colwhile fanxieRow < 7 and fanxieCol > 0:fanxieRow += 1fanxieCol -= 1fanxieCount = 7 - row if fanxieRow == 7 else colwhile fanxieRow >= 0 and fanxieCol <= 7:checkLiFanxie.append(self.board[fanxieRow][fanxieCol])fanxieRow -= 1fanxieCol += 1if BLACK not in checkLiShuiping or WHITE not in checkLiShuiping:resultLi.append(shuipingCount)resultLi.append(0)resultLi.append(0)else:tiebian = TruechangeCount = -1emptyCount = 0for i in range(shuipingCount, 0, -1):if checkLiShuiping[i] == fanseColor:changeCount += 1elif checkLiShuiping[i] == qiziColor:tiebian = Falsebreakelif checkLiShuiping[i] == EMPTY:changeCount = 0emptyCount += 1if emptyCount >= 2:breakresultLi.append(shuipingCount)resultLi.append(changeCount)if changeCount > 0 and tiebian is False:flag = TruechangeCount = -1emptyCount = 0tiebian = Truefor i in range(shuipingCount, 8):if checkLiShuiping[i] == fanseColor:changeCount += 1elif checkLiShuiping[i] == qiziColor:tiebian = Falsebreakelif checkLiShuiping[i] == EMPTY:changeCount = 0emptyCount += 1if emptyCount >= 2:breakresultLi.append(changeCount)if changeCount > 0 and tiebian is False:flag = Trueif BLACK not in checkLiShuzhi or WHITE not in checkLiShuzhi:resultLi.append(shuzhiCount)resultLi.append(0)resultLi.append(0)else:changeCount = -1emptyCount = 0tiebian = Truefor i in range(shuzhiCount, 0, -1):if checkLiShuzhi[i] == fanseColor:changeCount += 1elif checkLiShuzhi[i] == qiziColor:tiebian = Falsebreakelif checkLiShuzhi[i] == EMPTY:changeCount = 0emptyCount += 1if emptyCount >= 2:breakresultLi.append(shuzhiCount)resultLi.append(changeCount)if changeCount > 0 and tiebian is False:flag = TrueemptyCount = 0changeCount = -1tiebian = Truefor i in range(shuzhiCount, 8):if checkLiShuzhi[i] == fanseColor:changeCount += 1elif checkLiShuzhi[i] == qiziColor:tiebian = Falsebreakelif checkLiShuzhi[i] == EMPTY:changeCount = 0emptyCount += 1if emptyCount >= 2:breakresultLi.append(changeCount)if changeCount > 0 and tiebian is False:flag = Trueif 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 = TruechangeCount = -1emptyCount = 0for i in range(zhengxieCount, -1, -1):if checkLiZhengxie[i] == fanseColor:changeCount += 1elif checkLiZhengxie[i] == qiziColor:tiebian = Falsebreakelif checkLiZhengxie[i] == EMPTY:changeCount = 0emptyCount += 1if emptyCount >= 2:breakresultLi.append(changeCount)if changeCount > 0 and tiebian is False:flag = TruechangeCount = -1emptyCount = 0tiebian = Truefor i in range(zhengxieCount, len(checkLiZhengxie)):if checkLiZhengxie[i] == fanseColor:changeCount += 1elif checkLiZhengxie[i] == qiziColor:tiebian = Falsebreakelif checkLiZhengxie[i] == EMPTY:changeCount = 0emptyCount += 1if emptyCount >= 2:breakresultLi.append(changeCount)if changeCount > 0 and tiebian is False:flag = Trueif 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 = TruechangeCount = -1emptyCount = 0for i in range(fanxieCount, -1, -1):if checkLiFanxie[i] == fanseColor:changeCount += 1elif checkLiFanxie[i] == qiziColor:tiebian = Falsebreakelif checkLiFanxie[i] == EMPTY:changeCount = 0emptyCount += 1if emptyCount >= 2:breakresultLi.append(changeCount)if changeCount > 0 and tiebian is False:flag = TruechangeCount = -1emptyCount = 0tiebian = Truefor i in range(fanxieCount, len(checkLiFanxie)):if checkLiFanxie[i] == fanseColor:changeCount += 1elif checkLiFanxie[i] == qiziColor:tiebian = Falsebreakelif checkLiFanxie[i] == EMPTY:changeCount = 0emptyCount += 1if emptyCount >= 2:breakresultLi.append(changeCount)if changeCount > 0 and tiebian is False:flag = Truereturn [flag, resultLi]def realMove(self, para, isBlack, row, col):qiziColor = BLACK if isBlack else WHITEshuipingCount = 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] = qiziColorfor j in range(changCountShuipingYou + 1):self.board[row][shuipingCount + j] = qiziColorfor j in range(changCountShuzhiZuo + 1):self.board[shuzhiCount - j][col] = qiziColorfor j in range(changCountShuzhiYou + 1):self.board[shuzhiCount + j][col] = qiziColorfor j in range(changCountZhengxieZuo + 1):self.board[row - j][col - j] = qiziColorfor j in range(changCountZhengxieYou + 1):self.board[row + j][col + j] = qiziColorfor j in range(changCountFanxieZuo + 1):self.board[row + j][col - j] = qiziColorfor j in range(changCountFanxieYou + 1):self.board[row - j][col + j] = qiziColordef 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 whiteColorpos = [40 * (col + 1), 40 * (row + 1)]pygame.draw.circle(screen, ccolor, pos, 18, 0)def isWin(board, isBlack):global MOVEOUTallChess = [board.board[i][j] for i in range(8) for j in range(8)]if WHITE not in allChess:return "黑棋胜"if BLACK not in allChess:return "白棋胜"if EMPTY in allChess:aroundLi = []moveornotLi = []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 orcheckBoard[i - 1][j + 1] != EMPTY or checkBoard[i][j - 1] != EMPTY orcheckBoard[i][j] != EMPTY or checkBoard[i][j + 1] != EMPTY orcheckBoard[i + 1][j - 1] != EMPTY or checkBoard[i + 1][j] != EMPTY orcheckBoard[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 Falseif not moveornot:MOVEOUT += 1return "换棋子继续下"if MOVEOUT == 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 = 0def main():board = AppleBoard()isBlack = Truepygame.init()pygame.display.set_caption("黑白棋")screen = pygame.display.set_mode((360, 360))screen.fill([125, 95, 24])board.draw(screen)pygame.display.flip()running = Truepause = Falsewhile running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYUP:passelif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:x, y = event.posrow = round((y - 40) / 40)col = round((x - 40) / 40)moveRes = board.move(row, col, 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 isBlackelif res == "黑棋胜" or res == "白棋胜" or res == "和棋":pause = Truewhile pause:print(res)for event in pygame.event.get():if event.type == pygame.QUIT:pause = Falserunning = Falseelif event.type == pygame.KEYDOWN:if event.key == pygame.K_SPACE:board.reset()screen.fill([125, 95, 24])board.draw(screen)pygame.display.flip()isBlack = Truepause = Falseif __name__ == '__main__':main()