今天分享给大家的是采用Python3+tkinter制作而成的小项目——黑白棋
tkinter是Python内置的图形化模块,简单易用,一般的小型UI程序可以快速用它实现,具体的tkinter相关知识王老师会在以后开辟专栏单独讲解
我们先来看看这个黑白棋项目吧
完整代码如下,用到的素材(图片等)下载地址为:www.itprojects.cn/detail.html…
from tkinter import *from tkinter.messagebox import *import randomroot = Tk('黑白棋')root.title("黑白棋(更多项目实例请访问www.itprojects.cn)")# 加载图片imgs = [PhotoImage(file='black.png'), PhotoImage(file='white.png'), PhotoImage(file='board.png'), PhotoImage(file='info2.png')]def resetBoard(board): """重置棋盘""" for x in range(8): for y in range(8): board[x][y] = 'none' # Starting pieces: board[3][3] = 'black' board[3][4] = 'white' board[4][3] = 'white' board[4][4] = 'black'def getNewBoard(): """开局时建立新棋盘""" board = [] for i in range(8): board.append(['none'] * 8) return boarddef isValidMove(board, tile, xstart, ystart): """是否是合法走法,如果合法返回需要翻转的棋子列表""" # 如果该位置已经有棋子或者出界了,返回False if not isOnBoard(xstart, ystart) or board[xstart][ystart] != 'none': return False # 临时将tile 放到指定的位置 board[xstart][ystart] = tile if tile == 'black': otherTile = 'white' else: otherTile = 'black' # 要被翻转的棋子 tilesToFlip = [] for xdirection, ydirection in [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]: x, y = xstart, ystart x += xdirection y += ydirection if isOnBoard(x, y) and board[x][y] == otherTile: x += xdirection y += ydirection if not isOnBoard(x, y): continue # 一直走到出界或不是对方棋子的位置 while board[x][y] == otherTile: x += xdirection y += ydirection if not isOnBoard(x, y): break # 出界了,则没有棋子要翻转OXXXXX if not isOnBoard(x, y): continue # 是自己的棋子OXXXXXXO if board[x][y] == tile: while True: x -= xdirection y -= ydirection # 回到了起点则结束 if x == xstart and y == ystart: break # 需要翻转的棋子 tilesToFlip.append([x, y]) # 将前面临时放上的棋子去掉,即还原棋盘 board[xstart][ystart] = 'none' # restore the empty space # 没有要被翻转的棋子,则走法非法。翻转棋的规则。 if len(tilesToFlip) == 0: # If no tiles were flipped, this is not a valid move. return False return tilesToFlipdef isOnBoard(x, y): """是否出界""" return x >= 0 and x <= 7 and y >= 0 and y <= 7def getValidMoves(board, tile): """获取可落子的位置""" validMoves = [] for x in range(8): for y in range(8): if isValidMove(board, tile, x, y) != False: validMoves.append([x, y]) return validMovesdef getScoreOfBoard(board): """获取棋盘上黑白双方的棋子数""" xscore = 0 oscore = 0 for x in range(8): for y in range(8): if board[x][y] == 'black': xscore += 1 if board[x][y] == 'white': oscore += 1 return {'black': xscore, 'white': oscore}def whoGoesFirst(): """决定谁先走""" if random.randint(0, 1) == 0: return 'computer' else: return 'player'def makeMove(board, tile, xstart, ystart): """将一个tile棋子放到(xstart, ystart)""" tilesToFlip = isValidMove(board, tile, xstart, ystart) if tilesToFlip == False: return False board[xstart][ystart] = tile for x, y in tilesToFlip: # tilesToFlip是需要翻转的棋子列表 board[x][y] = tile # 翻转棋子 return Truedef getBoardCopy(board): """复制棋盘""" dupeBoard = getNewBoard() for x in range(8): for y in range(8): dupeBoard[x][y] = board[x][y] return dupeBoarddef isOnCorner(x, y): """是否在角上""" return (x == 0 and y == 0) or (x == 7 and y == 0) or (x == 0 and y == 7) or (x == 7 and y == 7)def getComputerMove(board, computerTile): """电脑走法,AI""" # 获取所以合法走法 possibleMoves = getValidMoves(board, computerTile) if not possibleMoves: # 如果没有合法走法 print("电脑没有合法走法")