十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

python+pygame实现五子棋人机对战之五

python+pygame实现五子棋人机对战之五 pythonpygame实现五子棋人机对战之一pythonpygame实现五子棋人机对战之二pythonpygame实现五子棋人机对战之三pythonpygame实现五子棋人机对战之四在之前的文章中已经完成了所有的基础工作剩下的就是把空填上就可以了。六、完成程序# encoding:utf-8 import pygame from pygame.locals import * import sys import os import piece from params import Params from utils import * pygame.init() pygame.mixer.init() #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 ... # 判断棋盘某位置上是否有棋子 def isempty(theposition): #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 ... # 电脑选取落子的位置 def computerdecision(): value max1 max2 0 pos1 pos2 () # 进攻 for i in range(0, rows): row 28 i * blocksize for j in range(0, rows): col 28 j * blocksize pos (row, col) if isempty(pos): continue value pointvalue(chess_map,pos, 1, 2) if value max1: max1 value pos1 (row, col) # 防守 for i in range(0, rows): for j in range(0, rows): row 28 i * blocksize col 28 j * blocksize if isempty((row, col)): continue value pointvalue(chess_map,(row, col), 2, 1) if value max2: max2 value pos2 (row, col) if max1 max2: return pos1 else: return pos2 # 初始化棋盘 def init(): #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 ... def main(): #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 ... running True clock pygame.time.Clock() # 字体 #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 ... # 初始化 init() while running: screen.blit(bg_image, (0, 0)) # 绘制游戏菜单 if not is_choise: screen.blit(zz_image, (0, 0)) #遮罩层 screen.blit(txtplayerandplayer, txtplayerandplayer_rect) screen.blit(txtplayerandcomputer, txtplayerandcomputer_rect) if not is_play_sound: screen.blit(txtclosesound, txtclosesound_rect) else: screen.blit(txtplaysound, txtplaysound_rect) # 绘制棋盘 if is_choise: if chesses: for i in chesses: screen.blit(i.image, i.image_rect()) for event in pygame.event.get(): if event.type QUIT: pygame.quit() sys.exit() if event.type MOUSEBUTTONDOWN: #人下棋 if event.button 1: if is_choise: pos pygame.mouse.get_pos() # 判断是否是人下棋时间 if is_player and not is_finish: me piece.Piece(pos,pieces_white.png) if not isempty(me.pointtrans()): white_chesses.append(me) white_positions.append(me.pointtrans()) chesses.append(me) chess_map[str(me.pointtrans()[0]) , str(me.pointtrans()[1])] 1 is_player False piece_sound.play() # 判断输赢 新增判断输赢 if len(white_chesses)5 and not is_finish and checkwin(white_positions,me.pointtrans()[0],me.pointtrans()[1]) : is_finish True white_win True else: del (me) else: pos pygame.mouse.get_pos() if txtplayerandplayer_rect.left pos[0] txtplayerandplayer_rect.left 170 and \ txtplayerandplayer_rect.top 100 pos[1] txtplayerandplayer_rect.top 130: is_choise True people2computer True if txtplayerandplayer_rect.left pos[0] txtplayerandplayer_rect.left 160 and \ txtplayerandplayer_rect.top 200 pos[1] txtplayerandplayer_rect.top 230: is_play_sound not is_play_sound if not is_play_sound: pygame.mixer.stop() pygame.mixer.music.stop() else: pygame.mixer.music.play() if is_finish : pos pygame.mouse.get_pos() if successtext_rect.left pos[0] successtext_rect.right - 50 and \ successtext_rect.top pos[1] successtext_rect.top 30: if people2computer: is_playagain True is_player True is_finish False init() if successtext_rect.left pos[0] successtext_rect.right - 50 and \ successtext_rect.top 50 pos[1] successtext_rect.top 120: people2people False main() # 电脑落子 if people2computer and not is_player: robot piece.Piece(computerdecision(),pieces_black.png) black_chesses.append(robot) black_positions.append(robot.pointtrans()) chesses.append(robot) chess_map[str(robot.pointtrans()[0]) , str(robot.pointtrans()[1])] 2 is_player True piece_sound.play() # 判断输赢 if len(black_chesses)5 and not is_finish and checkwin(black_positions,robot.pointtrans()[0],robot.pointtrans()[1]) : is_finish True black_win True #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 if is_playagain: #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 pygame.display.flip() clock.tick(60) if __name__ __main__: main()理解一下这个过程人下了一个子赋值给me然后添加到white_chesseswhite_positionschesses中改变chess_map相应坐标上的值为1is_player设置为False表示人不能再下棋了当white_chesses的个数是5个以上时调用checkwin判断是否连5。如果还要继续则下棋权力在电脑调用computerdecision函数算出最有利的步骤电脑下黑棋赋值给robot然后添加到black_chessesblack_positionschesses中改变chess_map相应坐标上的值为2is_player设置为True表示下一步由人下棋了当black_chesses的个数是5个以上时调用checkwin判断是否连5。到此为止实现pythonpygame五子棋人机对战。后续会使用套接字socket实现联网对战。pythonpygame实现五子棋人机对战源码及资源另一个五子棋电脑自动应手代码就不详细讲了大家可以直接参考下面的源码pythonpygame实现五子棋人机源码电脑应手两套
返回列表