在线等!求一个python 五子棋源代码,最好是有“人人对弈”和“人机对弈”功能的,不胜感谢! 什么叫编程

\u7f16\u7a0b\u662f\u4ec0\u4e48\u610f\u601d

\u7f16\u7a0b\u662f\u7f16\u5b9a\u7a0b\u5e8f\u7684\u4e2d\u6587\u7b80\u79f0\uff0c\u5c31\u662f\u8ba9\u8ba1\u7b97\u673a\u4ee3\u4e3a\u89e3\u51b3\u67d0\u4e2a\u95ee\u9898\uff0c\u5bf9\u67d0\u4e2a\u8ba1\u7b97\u4f53\u7cfb\u89c4\u5b9a\u4e00\u5b9a\u7684\u8fd0\u7b97\u65b9\u5f0f\uff0c\u4f7f\u8ba1\u7b97\u4f53\u7cfb\u6309\u7167\u8be5\u8ba1\u7b97\u65b9\u5f0f\u8fd0\u884c\uff0c\u5e76\u6700\u7ec8\u5f97\u5230\u76f8\u5e94\u7ed3\u679c\u7684\u8fc7\u7a0b\u3002
\u4e3a\u4e86\u4f7f\u8ba1\u7b97\u673a\u80fd\u591f\u7406\u89e3\u4eba\u7684\u610f\u56fe\uff0c\u5c31\u5fc5\u987b\u5c06\u9700\u89e3\u51b3\u7684\u95ee\u9898\u7684\u601d\u8def\u3001\u65b9\u6cd5\u548c\u624b\u6bb5\u901a\u8fc7\u8ba1\u7b97\u673a\u80fd\u591f\u7406\u89e3\u7684\u5f62\u5f0f\u544a\u8bc9\u8ba1\u7b97\u673a\uff0c\u4f7f\u5f97\u8ba1\u7b97\u673a\u80fd\u591f\u6839\u636e\u4eba\u7684\u6307\u4ee4\u53bb\u5de5\u4f5c\uff0c\u5b8c\u6210\u67d0\u79cd\u7279\u5b9a\u7684\u4efb\u52a1\u3002\u8fd9\u79cd\u4eba\u548c\u8ba1\u7b97\u4f53\u7cfb\u4e4b\u95f4\u4ea4\u6d41\u7684\u8fc7\u7a0b\u5c31\u662f\u7f16\u7a0b\u3002\u8bbe\u8ba1\u5177\u5907\u903b\u8f91\u6d41\u52a8\u4f5c\u7528\u7684\u4e00\u79cd\u201c\u53ef\u63a7\u4f53\u7cfb\u201d\u3002

\u6269\u5c55\u8d44\u6599\uff1a
\u8ba1\u7b97\u673a\u5bf9\u9664\u673a\u5668\u8bed\u8a00\u4ee5\u5916\u7684\u6e90\u7a0b\u5e8f\u4e0d\u80fd\u76f4\u63a5\u8bc6\u522b\u3001\u7406\u89e3\u548c\u6267\u884c\uff0c\u90fd\u5fc5\u987b\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\u8f6c\u6362\u4e3a\u8ba1\u7b97\u673a\u80fd\u591f\u76f4\u63a5\u6267\u884c\u7684\u3002\u8fd9\u79cd\u5c06\u9ad8\u7ea7\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u7f16\u5199\u7684\u6e90\u7a0b\u5e8f\u8f6c\u6362\u5230\u673a\u5668\u76ee\u6807\u7a0b\u5e8f\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\uff1a
1\u3001\u89e3\u91ca\u65b9\u5f0f\uff1a\u8ba1\u7b97\u673a\u5bf9\u9ad8\u7ea7\u8bed\u8a00\u4e66\u5199\u7684\u6e90\u7a0b\u5e8f\u4e00\u8fb9\u89e3\u91ca\u4e00\u8fb9\u6267\u884c\uff0c\u4e0d\u80fd\u5f62\u6210\u76ee\u6807\u6587\u4ef6\u548c\u6267\u884c\u6587\u4ef6\u3002
2\u3001\u7f16\u8bd1\u65b9\u5f0f\uff1a\u7ecf\u8fc7\u5bf9\u6e90\u7a0b\u5e8f\u7684\u8bcd\u6cd5\u5206\u6790\u3001\u8bed\u6cd5\u5206\u6790\u3001\u8bed\u610f\u5206\u6790\u3001\u4ee3\u7801\u751f\u6210\u548c\u4ee3\u7801\u4f18\u5316\u7b49\u9636\u6bb5\u5c06\u6240\u5904\u7406\u7684\u6e90\u7a0b\u5e8f\u8f6c\u6362\u4e3a\u7528\u4e8c\u8fdb\u5236\u4ee3\u7801\u8868\u793a\u7684\u76ee\u6807\u7a0b\u5e8f\uff0c\u7136\u540e\u901a\u8fc7\u8fde\u63a5\u7a0b\u5e8f\u5904\u7406\u5c06\u7a0b\u5e8f\u4e2d\u6240\u7528\u7684\u51fd\u6570\u8c03\u7528\u3001\u7cfb\u7edf\u529f\u80fd\u8c03\u7528\u7b49\u5d4c\u5165\u5230\u76ee\u6807\u7a0b\u5e8f\u4e2d\uff0c\u6784\u6210\u4e00\u4e2a\u53ef\u4ee5\u8fde\u7eed\u6267\u884c\u7684\u4e8c\u8fdb\u5236\u6267\u884c\u6587\u4ef6\u3002
\u53c2\u8003\u8d44\u6599\u6765\u6e90\uff1a\u767e\u5ea6\u767e\u79d1-\u7f16\u7a0b

\u7f16\u7a0b\uff08bi\u0101n ch\u00e9ng\uff09\u662f\u7f16\u5b9a\u7a0b\u5e8f\u7684\u4e2d\u6587\u7b80\u79f0\uff0c\u5c31\u662f\u8ba9\u8ba1\u7b97\u673a\u4ee3\u4e3a\u89e3\u51b3\u67d0\u4e2a\u95ee\u9898\uff0c\u5bf9\u67d0\u4e2a\u8ba1\u7b97\u4f53\u7cfb\u89c4\u5b9a\u4e00\u5b9a\u7684\u8fd0\u7b97\u65b9\u5f0f\uff0c\u4f7f\u8ba1\u7b97\u4f53\u7cfb\u6309\u7167\u8be5\u8ba1\u7b97\u65b9\u5f0f\u8fd0\u884c\uff0c\u5e76\u6700\u7ec8\u5f97\u5230\u76f8\u5e94\u7ed3\u679c\u7684\u8fc7\u7a0b\u3002
\u4e3a\u4e86\u4f7f\u8ba1\u7b97\u673a\u80fd\u591f\u7406\u89e3\u4eba\u7684\u610f\u56fe\uff0c\u4eba\u7c7b\u5c31\u5fc5\u987b\u5c06\u9700\u89e3\u51b3\u7684\u95ee\u9898\u7684\u601d\u8def\u3001\u65b9\u6cd5\u548c\u624b\u6bb5\u901a\u8fc7\u8ba1\u7b97\u673a\u80fd\u591f\u7406\u89e3\u7684\u5f62\u5f0f\u544a\u8bc9\u8ba1\u7b97\u673a\uff0c\u4f7f\u5f97\u8ba1\u7b97\u673a\u80fd\u591f\u6839\u636e\u4eba\u7684\u6307\u4ee4\u4e00\u6b65\u4e00\u6b65\u53bb\u5de5\u4f5c\uff0c\u5b8c\u6210\u67d0\u79cd\u7279\u5b9a\u7684\u4efb\u52a1\u3002\u8fd9\u79cd\u4eba\u548c\u8ba1\u7b97\u4f53\u7cfb\u4e4b\u95f4\u4ea4\u6d41\u7684\u8fc7\u7a0b\u5c31\u662f\u7f16\u7a0b\u3002


\u6269\u5c55\u8d44\u6599\uff1a
\u5728\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\uff0c\u4e00\u6761\u673a\u5668\u6307\u4ee4\u89c4\u5b9a\u4e86\u8ba1\u7b97\u673a\u7cfb\u7edf\u7684\u4e00\u4e2a\u7279\u5b9a\u52a8\u4f5c\u3002\u4e00\u4e2a\u7cfb\u5217\u7684\u8ba1\u7b97\u673a\u5728\u786c\u4ef6\u8bbe\u8ba1\u5236\u9020\u65f6\u5c31\u7528\u4e86\u82e5\u5e72\u6307\u4ee4\u89c4\u5b9a\u4e86\u8be5\u7cfb\u5217\u8ba1\u7b97\u673a\u80fd\u591f\u8fdb\u884c\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u6307\u4ee4\u4e00\u8d77\u6784\u6210\u4e86\u8be5\u7cfb\u5217\u8ba1\u7b97\u673a\u7684\u6307\u4ee4\u7cfb\u7edf\u3002
\u5728\u8ba1\u7b97\u673a\u5e94\u7528\u7684\u521d\u671f\uff0c\u7a0b\u5e8f\u5458\u4f7f\u7528\u673a\u5668\u7684\u6307\u4ee4\u7cfb\u7edf\u6765\u7f16\u5199\u8ba1\u7b97\u673a\u5e94\u7528\u7a0b\u5e8f\uff0c\u8fd9\u79cd\u7a0b\u5e8f\u79f0\u4e3a\u673a\u5668\u8bed\u8a00\u7a0b\u5e8f\u3002\u4f7f\u7528\u673a\u5668\u8bed\u8a00\u7f16\u5199\u7684\u7a0b\u5e8f\uff0c\u7531\u4e8e\u6bcf\u6761\u6307\u4ee4\u90fd\u5bf9\u5e94\u8ba1\u7b97\u673a\u4e00\u4e2a\u7279\u5b9a\u7684\u57fa\u672c\u52a8\u4f5c\uff0c\u6240\u4ee5\u7a0b\u5e8f\u5360\u7528\u5185\u5b58\u5c11\u3001\u6267\u884c\u6548\u7387\u9ad8\u3002\u7f3a\u70b9\u4e5f\u5f88\u660e\u663e\uff0c\u5982\uff1a\u7f16\u7a0b\u5de5\u4f5c\u91cf\u5927\uff0c\u5bb9\u6613\u51fa\u9519\uff1b\u4f9d\u8d56\u5177\u4f53\u7684\u8ba1\u7b97\u673a\u4f53\u7cfb\uff0c\u56e0\u800c\u7a0b\u5e8f\u7684\u901a\u7528\u6027\u3001\u79fb\u690d\u6027\u90fd\u5f88\u5dee\u3002
\u53c2\u8003\u8d44\u6599\u6765\u6e90\uff1a\u767e\u5ea6\u767e\u79d1-\u7f16\u7a0b\u8bed\u8a00
\u53c2\u8003\u8d44\u6599\u6765\u6e90\uff1a\u767e\u5ea6\u767e\u79d1-\u7f16\u7a0b

试试这个吧。
import numpy as np
import pygame
import sys
import traceback
import copy
from pygame.locals import *

pygame.init()
pygame.mixer.init()

#颜色
background=(201,202,187)
checkerboard=(80,80,80)
button=(52,53,44)

#音乐
play_chess_sound = pygame.mixer.Sound("music/play_chess.wav")
play_chess_sound.set_volume(0.2)
button_sound = pygame.mixer.Sound("music/button.wav")
button_sound.set_volume(0.2)
victor_sound = pygame.mixer.Sound("music/victory.wav")
victor_sound.set_volume(0.2)

#绘制棋盘
def Draw_a_chessboard(screen):
#填充背景色
screen.fill(background)
Background=pygame.image.load("background.jpg").convert_alpha()
screen.blit(Background,(0,0))
#画棋盘
for i in range(21):
pygame.draw.line(screen, checkerboard, (40*i+3, 3), (40*i+3, 803))
pygame.draw.line(screen, checkerboard, (3, 40*i+3), (803, 40*i+3))
#画边线
pygame.draw.line(screen, checkerboard, (3, 3), (803, 3),5)
pygame.draw.line(screen, checkerboard, (3, 3), (3, 803),5)
pygame.draw.line(screen, checkerboard, (803, 3), (803, 803),5)
pygame.draw.line(screen, checkerboard, (3, 803), (803, 803),5)

#画定位点
pygame.draw.circle(screen, checkerboard, (163, 163), 6)
pygame.draw.circle(screen, checkerboard, (163, 643), 6)
pygame.draw.circle(screen, checkerboard, (643, 163), 6)
pygame.draw.circle(screen, checkerboard, (643, 643), 6)
pygame.draw.circle(screen, checkerboard, (403, 403), 6)

#画‘悔棋’‘重新开始’跟‘退出’按钮
pygame.draw.rect(screen,button,[900,350,120,100],5)
pygame.draw.rect(screen,button,[900,500,200,100],5)
pygame.draw.rect(screen,button,[900,650,200,100],5)
s_font=pygame.font.Font('font.ttf',40)
text1=s_font.render("悔棋",True,button)
text2=s_font.render("重新开始",True,button)
text3=s_font.render("退出游戏",True,button)
screen.blit(text1,(920,370))
screen.blit(text2,(920,520))
screen.blit(text3,(920,670))

#绘制棋子(横坐标,纵坐标,屏幕,棋子颜色(1代表黑,2代表白))
def Draw_a_chessman(x,y,screen,color):
if color==1:
Black_chess=pygame.image.load("Black_chess.png").convert_alpha()
screen.blit(Black_chess,(40*x+3-15,40*y+3-15))
if color==2:
White_chess=pygame.image.load("White_chess.png").convert_alpha()
screen.blit(White_chess,(40*x+3-15,40*y+3-15))

#绘制带有棋子的棋盘
def Draw_a_chessboard_with_chessman(map,screen):
screen.fill(background)
Draw_a_chessboard(screen)
for i in range(24):
for j in range(24):
Draw_a_chessman(i+1,j+1,screen,map[i][j])

#定义存储棋盘的列表,
#列表为24列24行是因为判断是否胜利函数里的索引会超出19
#列表大一点不会对游戏有什么影响
map=[]
for i in range(24):
map.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])

#清零map列表
def clear():
global map
for i in range(24):
for j in range(24):
map[i][j]=0

#判断是否胜利
def win(i, j):
k = map[i][j]
p=[]
for a in range(20):
p.append(0)
for i3 in range(i-4,i+5):
for j3 in range(j-4,j+5):
if (map[i3][j3] == k and i3 - i == j3 - j and i3 <= i and j3 <= j):
p[0]+=1
if (map[i3][j3] == k and j3 == j and i3 <= i and j3 <= j):
p[1]+=1
if (map[i3][j3] == k and i3 == i and i3 <= i and j3 <= j):
p[2]+=1
if (map[i3][j3] == k and i3 - i == j3 - j and i3 >= i and j3 >= j):
p[3]+=1
if (map[i3][j3] == k and j3 == j and i3 >= i and j3 >= j):
p[4]+=1
if (map[i3][j3] == k and i3 == i and i3 >= i and j3 >= j):
p[5]+=1
if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i and j3 >= j):
p[6]+=1
if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i and j3 <= j):
p[7]+=1
if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
p[8]+=1
if (map[i3][j3] == k and j == j3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
p[9]+=1
if (map[i3][j3] == k and i == i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
p[10]+=1
if (map[i3][j3] == k and j - j3 == i - i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
p[11]+=1
if (map[i3][j3] == k and j == j3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
p[12]+=1
if (map[i3][j3] == k and i == i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
p[13]+=1
if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 1 and i3 >= i - 3 and j3 >= j - 1 and j3 <= j + 3):
p[14]+=1
if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i - 1 and i3 <= i + 3 and j3 <= j + 1 and j3 >= j - 3):
p[15]+=1
if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
p[16]+=1
if (map[i3][j3] == k and j == j3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
p[17]+=1
if (map[i3][j3] == k and i == i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
p[18]+=1
if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
p[19]+=1
for b in range(20):
if p[b]==5:
return True
return False

#绘制提示器(类容,屏幕,字大小)
def text(s,screen,x):
#先把上一次的类容用一个矩形覆盖
pygame.draw.rect(screen,background,[850,100,1200,100])
#定义字体跟大小
s_font=pygame.font.Font('font.ttf',x)
#定义类容,是否抗锯齿,颜色
s_text=s_font.render(s,True,button)
#将字放在窗口指定位置
screen.blit(s_text,(880,100))
pygame.display.flip()

#用于控制顺序
t=True

#用于结束游戏后阻止落子
running=True

#主函数
def main():
#将 t,map,running设置为可改的
global t,map,running,maps,r,h
#将map置零
clear()
#定义储存所有棋盘状态的列表(用于悔棋)
map2=copy.deepcopy(map)
maps=[map2]

#定义窗口
screen = pygame.display.set_mode([1200,806])

#定义窗口名字
pygame.display.set_caption("五子棋")

#在窗口画出棋盘,提示器以及按钮
Draw_a_chessboard(screen)
pygame.display.flip()
clock=pygame.time.Clock()
while True:
#只有running为真才能落子,主要用于游戏结束后防止再次落子
if running:
if t:
color=1
text('黑棋落子',screen,54)
else:
color=2
text('白棋落子',screen,54)

for event in pygame.event.get():
#点击x则关闭窗口
if event.type ==pygame.QUIT:
pygame.quit()
sys.exit()

#点击窗口里面类容则完成相应指令
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
x,y=event.pos[0],event.pos[1]
for i in range(19):
for j in range(19):
#点击棋盘相应位置
if i*40+3+20<x<i*40+3+60 and j*40+3+20<y<j*40+3+60 and not map[i][j] and running:
#在棋盘相应位置落相应颜色棋子
Draw_a_chessman(i+1,j+1,screen,color)
#播放音效
play_chess_sound.play(0)
#在map里面记录落子位置
map[i][j]=color

#将map存入maps
map3=copy.deepcopy(map)
maps.append(map3)

#判断落子后是否有五子一线
if win(i,j):
if t:
text('黑棋胜利,请重新游戏',screen,30)
else:
text('白棋胜利,请重新游戏',screen,30)
#播放音效
victor_sound.play(0)
#阻止再往棋盘落子
running=False
pygame.display.flip()
t=not t
#如果点击‘重新开始’
if 900<x<1100 and 500<y<600:
#取消阻止
running=True
#播放音效
button_sound.play(0)
#重新开始
main()

#点击‘退出游戏’,退出游戏
elif 900<x<1100 and 650<y<750:
#播放音效
button_sound.play(0)
pygame.quit()
sys.exit()

#点击‘悔棋’
elif 900<x<1020 and 350<y<450 and len(maps)!=1:
#播放音效
button_sound.play(0)
#删除maps里最后一个元素
del maps[len(maps)-1]
#再将最后一个元素copy给map
map=copy.deepcopy(maps[len(maps)-1])
#切换顺序
t=not t
#将map显示出来
Draw_a_chessboard_with_chessman(map,screen)
#悔棋完成,阻止再次悔棋
x,y=0,0
clock.tick(60)
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()

扩展阅读:高清在线观看免费的ppt ... www.sony.com.cn ... 1688精品货源网站入口 ... m3u8视频可以永久保存 ... 免费的网页入口无需下载 ... mac蜜桃奶茶314网页版 ... mindmaster永久免费版 ... pubmed官方官网入口 ... 亚马逊aws永久免费电影 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网