• 团队编程项目总结


    项目名称;黑白棋

    小组成员;王闯,徐浩文,吴桐,岳云涛,常帆,朱佳明

    项目说明;1黑白棋在西方和日本很流行。游戏通过相互翻转对方的棋子,最后以棋盘上谁的棋子多来判断胜负。它的游戏规则简单,因此上手很容易,但是它的变化又非常复杂。有一种说法是:只需要几分钟学会它,却需要一生的时间去精通它。

    2棋子:黑白棋棋子每颗由黑白两色组成,一面白,一面黑,共64个(包括棋盘中央的4个)。棋子呈圆饼形。两个玩家各下一面棋子。
    棋盘:黑白棋棋盘由64(8*8)格的正方格组成,游戏进行时棋子要下在格内。

    游戏玩法;棋盘为8×8的方格布局,开局时在棋盘正中有摆好的四枚棋子,黑白各2枚,交叉放置,由执黑棋的一方先落子,双方交替下子,棋子落在方格内,一局游戏结束后双方更换执子颜色。
    步合法的棋步包括:在一个空格新落下一个棋子,并且翻转对手一个或多个棋子。
    下子方式:把自己颜色的棋子放在棋盘的空格上,而当自己放下的棋子在横、竖、斜八个方向内有一个自己的棋子,则被夹在中间的对方棋子全部翻转会成为自己的棋子。夹住的位置上必须全部是对手的棋子,不能有空格。并且,只有在可以翻转棋子的地方才可以下子。
    一步棋可以在数个方向上翻棋,任何被夹住的棋子都必须被翻转过来,棋手无权选择不去翻某个棋子必须是刚下的子夹对方才能够翻对方的子,因翻转对方的棋子而夹住的子是不能被翻的。
    翻转棋子时,有一个棋子的翻转动画大概显示1秒左右每次下子最少必须翻转对方一个棋子,若棋局中下子都不能翻转对方棋子,则自动pass轮空,客户端界面气泡提示:您无子可下由对方继续下子,而对方气泡提示:由于对方无子可下,您可继续下子。若二个玩家都不能下子翻转对方棋子,游戏结束。

    项目代码;

    # -*- coding: cp936 -*-
    import pygame, sys, random
    from pygame.locals import *

    BACKGROUNDCOLOR = (255, 255, 255)
    FPS = 40


    # 退出
    def terminate():
    pygame.quit()
    sys.exit()


    # 初始化
    pygame.init()
    mainClock = pygame.time.Clock()

    # 加载图片
    boardImage = pygame.image.load('board.png')
    boardRect = boardImage.get_rect()
    blackImage = pygame.image.load('black.png')
    blackRect = blackImage.get_rect()
    whiteImage = pygame.image.load('white.png')
    whiteRect = whiteImage.get_rect()


    # 设置窗口
    windowSurface = pygame.display.set_mode((boardRect.width, boardRect.height))
    pygame.display.set_caption('黑白棋')


    # 游戏主循环
    while True:
    for event in pygame.event.get():
    if event.type == QUIT:
    terminate()

    windowSurface.fill(BACKGROUNDCOLOR)
    windowSurface.blit(boardImage, boardRect, boardRect)

    pygame.display.update()
    mainClock.tick(FPS)


    CELLWIDTH = 50
    CELLHEIGHT = 50
    PIECEWIDTH = 47
    PIECEHEIGHT = 47
    BOARDX = 35
    BOARDY = 35


    # 重置棋盘
    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 board

    mainBoard = getNewBoard()
    resetBoard(mainBoard)

    for x in range(8):
    for y in range(8):
    rectDst = pygame.Rect(BOARDX+x*CELLWIDTH+2, BOARDY+y*CELLHEIGHT+2, PIECEWIDTH, PIECEHEIGHT)
    if mainBoard[x][y] == 'black':
    windowSurface.blit(blackImage, rectDst, blackRect)
    elif mainBoard[x][y] == 'white':
    windowSurface.blit(whiteImage, rectDst, whiteRect)

    # 谁先走
    def whoGoesFirst():
    if random.randint(0, 1) == 0:
    return 'computer'
    else:
    return 'player'

    turn = whoGoesFirst()
    if turn == 'player':
    playerTile = 'black'
    computerTile = 'white'
    else:
    playerTile = 'white'
    computerTile = 'black'

    for event in pygame.event.get():
    if event.type == QUIT:
    terminate()
    if turn == 'player' and event.type == MOUSEBUTTONDOWN and event.button == 1:
    x, y = pygame.mouse.get_pos()
    col = int((x-BOARDX)/CELLWIDTH)
    row = int((y-BOARDY)/CELLHEIGHT)
    if makeMove(mainBoard, playerTile, col, row) == True:
    if getValidMoves(mainBoard, computerTile) != []:
    turn = 'computer'
    windowSurface.fill(BACKGROUNDCOLOR)
    windowSurface.blit(boardImage, boardRect, boardRect)
    if (turn == 'computer'):
    x, y = getComputerMove(mainBoard, computerTile)
    makeMove(mainBoard, computerTile, x, y)
    savex, savey = x, y

    # 玩家没有可行的走法了
    if getValidMoves(mainBoard, playerTile) != []:
    turn = 'player'

    windowSurface.fill(BACKGROUNDCOLOR)
    windowSurface.blit(boardImage, boardRect, boardRect)

    import pygame, sys, random
    from pygame.locals import *

    BACKGROUNDCOLOR = (255, 255, 255)
    BLACK = (255, 255, 255)
    BLUE = (0, 0, 255)
    CELLWIDTH = 50
    CELLHEIGHT = 50
    PIECEWIDTH = 47
    PIECEHEIGHT = 47
    BOARDX = 35
    BOARDY = 35
    FPS = 40

    # 退出
    def terminate():
    pygame.quit()
    sys.exit()


    # 重置棋盘
    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 board


    # 是否是合法走法
    def 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 tilesToFlip


    # 是否出界
    def isOnBoard(x, y):
    return x >= 0 and x <= 7 and y >= 0 and y <=7


    # 获取可落子的位置
    def 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 validMoves


    # 获取棋盘上黑白双方的棋子数
    def 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'


    # 将一个tile棋子放到(xstart, ystart)
    def makeMove(board, tile, xstart, ystart):
    tilesToFlip = isValidMove(board, tile, xstart, ystart)

    if tilesToFlip == False:
    return False

    board[xstart][ystart] = tile
    for x, y in tilesToFlip:
    board[x][y] = tile
    return True

    # 复制棋盘
    def getBoardCopy(board):
    dupeBoard = getNewBoard()

    for x in range(8):
    for y in range(8):
    dupeBoard[x][y] = board[x][y]

    return dupeBoard

    # 是否在角上
    def 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)


    # 电脑走法,AI
    def getComputerMove(board, computerTile):
    # 获取所以合法走法
    possibleMoves = getValidMoves(board, computerTile)

    # 打乱所有合法走法
    random.shuffle(possibleMoves)

    # [x, y]在角上,则优先走,因为角上的不会被再次翻转
    for x, y in possibleMoves:
    if isOnCorner(x, y):
    return [x, y]

    bestScore = -1
    for x, y in possibleMoves:
    dupeBoard = getBoardCopy(board)
    makeMove(dupeBoard, computerTile, x, y)
    # 按照分数选择走法,优先选择翻转后分数最多的走法
    score = getScoreOfBoard(dupeBoard)[computerTile]
    if score > bestScore:
    bestMove = [x, y]
    bestScore = score
    return bestMove

    # 是否游戏结束
    def isGameOver(board):
    for x in range(8):
    for y in range(8):
    if board[x][y] == 'none':
    return False
    return True


    # 初始化
    pygame.init()
    mainClock = pygame.time.Clock()

    # 加载图片
    boardImage = pygame.image.load('board.png')
    boardRect = boardImage.get_rect()
    blackImage = pygame.image.load('black.png')
    blackRect = blackImage.get_rect()
    whiteImage = pygame.image.load('white.png')
    whiteRect = whiteImage.get_rect()

    basicFont = pygame.font.SysFont(None, 48)
    gameoverStr = 'Game Over Score '

    mainBoard = getNewBoard()
    resetBoard(mainBoard)

    turn = whoGoesFirst()
    if turn == 'player':
    playerTile = 'black'
    computerTile = 'white'
    else:
    playerTile = 'white'
    computerTile = 'black'

    print(turn)

    # 设置窗口
    windowSurface = pygame.display.set_mode((boardRect.width, boardRect.height))
    pygame.display.set_caption('黑白棋')


    gameOver = False


    # 游戏主循环
    while True:
    for event in pygame.event.get():
    if event.type == QUIT:
    terminate()
    if gameOver == False and turn == 'player' and event.type == MOUSEBUTTONDOWN and event.button == 1:
    x, y = pygame.mouse.get_pos()
    col = int((x-BOARDX)/CELLWIDTH)
    row = int((y-BOARDY)/CELLHEIGHT)
    if makeMove(mainBoard, playerTile, col, row) == True:
    if getValidMoves(mainBoard, computerTile) != []:
    turn = 'computer'

    windowSurface.fill(BACKGROUNDCOLOR)
    windowSurface.blit(boardImage, boardRect, boardRect)

    if (gameOver == False and turn == 'computer'):
    x, y = getComputerMove(mainBoard, computerTile)
    makeMove(mainBoard, computerTile, x, y)
    savex, savey = x, y

    # 玩家没有可行的走法了
    if getValidMoves(mainBoard, playerTile) != []:
    turn = 'player'

    windowSurface.fill(BACKGROUNDCOLOR)
    windowSurface.blit(boardImage, boardRect, boardRect)

    for x in range(8):
    for y in range(8):
    rectDst = pygame.Rect(BOARDX+x*CELLWIDTH+2, BOARDY+y*CELLHEIGHT+2, PIECEWIDTH, PIECEHEIGHT)
    if mainBoard[x][y] == 'black':
    windowSurface.blit(blackImage, rectDst, blackRect)
    elif mainBoard[x][y] == 'white':
    windowSurface.blit(whiteImage, rectDst, whiteRect)

    if isGameOver(mainBoard):
    scorePlayer = getScoreOfBoard(mainBoard)[playerTile]
    scoreComputer = getScoreOfBoard(mainBoard)[computerTile]
    outputStr = gameoverStr + str(scorePlayer) + ":" + str(scoreComputer)
    text = basicFont.render(outputStr, True, BLACK, BLUE)
    textRect = text.get_rect()
    textRect.centerx = windowSurface.get_rect().centerx
    textRect.centery = windowSurface.get_rect().centery
    windowSurface.blit(text, textRect)

    pygame.display.update()
    mainClock.tick(FPS)

     

     项目总结;通过这次合作,我们小组成员已经学会了黑白棋的游戏玩法,并且我们都在这次合作中学到了很多,虽然还有许多美中不足的地方,但我们会更加努力的去完善

  • 相关阅读:
    JavaScriptCore框架介绍
    可执行程序加载到内存的过程
    阿里云服务器怎么更换系统盘
    什么是A记录  域名
    宝塔服务器管理助手Linux面版-使用教程
    虚拟主机、VPS以及云主机的区别和对比
    怎样选择一个好的虚拟主机
    解Linux SSH命令大全,新手必看SSH命令
    虚拟主机,VPS,云主机之间的区别?
    云服务器和虚拟主机的区别:
  • 原文地址:https://www.cnblogs.com/bobokeke/p/7944400.html
Copyright © 2020-2023  润新知