一个喜欢编程的五年级学生 2024-09-22 21:19 采纳率: 0%
浏览 3

python做的五子棋程序未响应


from tkinter import *
import random
import time

tk = Tk()
tk.title('五子棋游戏')
tk.geometry("400x450")
tk.config(bg='black')

stateList = [[0 for i in range(8)] for j in range(8)]

canvas = Canvas(tk, width=400, height=400, bg='#FFEFD5')
canvas.pack(side=BOTTOM)

stateLabel = Label(tk, text='轮到您...', font=('consoles', 20), fg='white', bg='black')
stateLabel.place(x=150, y=10)

for i in range(8):
    canvas.create_line(50, 50 + i * 42.7, 350, 50 + i * 42.7, fill='black')
    canvas.create_line(50 + i * 42.7, 50, 50 + i * 42.7, 350, fill='black')

def cauculate_position(x,y,fill):
    x1 = 50 + x * 42.7 + 1 - 20 / 2
    y1 = 50 + y * 42.7 + 1 - 20 / 2
    x2 = 50 + x * 42.7 + 1 + 20 / 2
    y2 = 50 + y * 42.7 + 1 + 20 / 2
    if fill == 1:
        fill = 'black'
    else:
        fill = 'white'
    canvas.create_oval(x1, y1, x2, y2, fill=fill)
    stateLabel['text'] = '轮到您...'
    canvas.bind("<Button-1>", on_canvas_click)
    return 0

def win(player):
    d={1:2,2:1}
    for i in range(8):
        for j in range(6):
            if stateList[i][j] == player and stateList[i][j+1] == player and stateList[i][j+2] == player:
                try:
                    a=stateList[i][j+4]
                except:
                    return False
                try:
                    a=stateList[i][j-1]
                except:
                    return False
                if stateList[i][j+4] == d[player]:
                    cauculate_position(i,j,2)
                elif stateList[i][j-1] == d[player]:
                    cauculate_position(i,j,2)
                return True

    for j in range(8):
        for i in range(6):
            if stateList[i][j] == player and stateList[i+1][j] == player and stateList[i+2][j] == player:
                try:
                    a=stateList[i+3][j]
                except:
                    return False
                try:
                    a=stateList[i-1][j]
                except:
                    return False
                if stateList[i+3][j] == d[player]:
                    cauculate_position(i,j,2)
                elif stateList[i-1][j] == d[player]:
                    cauculate_position(i,j,2)
                return True

    for i in range(6):
        for j in range(6):
            if stateList[i][j] == player and stateList[i+1][j+1] == player and stateList[i+2][j+2] == player:
                try:
                    a=stateList[i+3][j+3]
                except:
                    return False
                try:
                    a=stateList[i-1][j-1]
                except:
                    return False
                if stateList[i+3][j+3] == d[player]:
                    cauculate_position(i,j,2)
                elif stateList[i-1][j-1] == d[player]:
                    cauculate_position(i,j,2)
                return True

    for i in range(7, 1, -1):
        for j in range(6):
            if stateList[i][j] == player and stateList[i-1][j+1] == player and stateList[i-2][j+2] == player:
                try:
                    a=stateList[i-3][j+3]
                except:
                    return False
                try:
                    a=stateList[i+1][j-1]
                except:
                    return False
                if stateList[i-3][j+3] == d[player]:
                    cauculate_position(i,j,2)
                elif stateList[i+1][j-1] == d[player]:
                    cauculate_position(i,j,2)
                return True

    return False

def check_win(player):
    for i in range(8):
        for j in range(5):
            if stateList[i][j] == player and stateList[i][j+1] == player and stateList[i][j+2] == player and stateList[i][j+3] == player and stateList[i][j+4] == player:
                return True

    for i in range(5):
        for j in range(8):
            if stateList[i][j] == player and stateList[i+1][j] == player and stateList[i+2][j] == player and stateList[i+3][j] == player and stateList[i+4][j] == player:
                return True

    for i in range(5):
        for j in range(5):
            if stateList[i][j] == player and stateList[i+1][j+1] == player and stateList[i+2][j+2] == player and stateList[i+3][j+3] == player and stateList[i+4][j+4] == player:
                return True

    for i in range(5):
        for j in range(3, 8):
            if stateList[i][j] == player and stateList[i+1][j-1] == player and stateList[i+2][j-2] == player and stateList[i+3][j-3] == player and stateList[i+4][j-4] == player:
                return True
    cnt = 0
    for i in stateList:
        for j in i:
            if j != 0:
                cnt += 1
    if cnt == 64:
        stateLabel['text'] = '平局!'
        canvas.unbind("<Button-1>")
    return False


def on_canvas_click(event):
    global stateList,x_index,y_index
    x_index = int((event.x - 50) / 42.7)
    y_index = int((event.y - 50) / 42.7)
    if stateList[y_index][x_index]!= 0:
        return
    cauculate_position(x_index, y_index, 1)
    stateLabel['text'] = '轮到电脑...'
    canvas.update()
    stateList[y_index][x_index] = 1
    canvas.unbind("<Button-1>")
    computer_move()
    if check_win(1):
        stateLabel['text'] = '您赢了!'
        canvas.unbind("<Button-1>")
        tk.update()

def computer_move():
    global stateList
    while True:
        if not win(2):
            angle = random.choice([[1, 0], [0, 1], [-1, 0], [0, -1]])
            try:
                if stateList[y_index + angle[0]][x_index + angle[1]] == 0:
                    stateList[y_index + angle[0]][x_index + angle[1]] = 2
                    cauculate_position(y_index + angle[0], x_index + angle[1], 2)
                    break
            except :
                continue


canvas.bind("<Button-1>", on_canvas_click)
tk.mainloop()


紧急!
windows 11 22631.4169
pycharm社区版2023.3.7

问题的视频在

https://www.bilibili.com/video/BV16yskegEu4/?spm_id_from=333.337.search-card.all.click&vd_source=1b96c0b6a8f8d8daadbe989e447d7882

  • 写回答

1条回答 默认 最新

  • 一轮明月照丘壑 2024-09-22 21:20
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    首先,让我确认一下您的请求是否有关于Python编程中的五子棋游戏。如果您有特定的问题或者遇到错误,请提供更多的细节以便我能更准确地为您提供帮助。

    关于您的问题描述中提到的"win"函数和"check_win"函数,它们用于检查游戏是否结束以及谁赢得了比赛。这些函数似乎没有实现。通常,在五子棋游戏中,当一方赢得连续五个相同的棋子时(例如,三个横排或三个竖排),该方将获胜。此外,如果所有棋盘上都没有空位,则平局发生。

    为了解决这个问题,您可以尝试添加wincheck_win函数,确保它们能够正确检测并返回正确的结果。此外,您还需要更新on_canvas_click函数以处理计算机的移动,并确保它不会在当前玩家已经移动的情况下继续执行。同时,确保在循环中正确地调用computer_move()

    以下是修改后的代码示例:

    # 假设我们定义了一些棋盘状态变量
    stateList = [[0 for i in range(8)] for j in range(8)]
    player = 1  # 玩家为1表示白棋,为2表示黑棋
    
    # 初始化游戏逻辑
    while True:
        # 检查是否有足够的空间放置新的棋子
        if all([all(s for s in row) for row in stateList]):
            print("平局!")
            break
    
        # 获取鼠标点击位置
        event = canvas.event_generate("<Button-1>")
    
        # 如果是玩家的点击
        if stateList[y_index][x_index] == 0:
            cauculate_position(x_index, y_index, player)
            stateList[y_index][x_index] = player
            player = (player + 1) % 2  # 轮换玩家
    
        # 如果是电脑的点击
        elif stateList[y_index][x_index] == 0 and not check_win(2):
            angle = random.choice([[1, 0], [0, 1], [-1, 0], [0, -1]])
            try:
                if stateList[y_index + angle[0]][x_index + angle[1]] == 0:
                    stateList[y_index + angle[0]][x_index + angle[1]] = 2
                    cauculate_position(y_index + angle[0], x_index + angle[1], 2)
                    break
            except IndexError:
                pass
    
        # 检查是否有足够的空间放置新的棋子
        if all([all(s for s in row) for row in stateList]):
            print("平局!")
            break
    
        # 更新屏幕显示
        canvas.update()
    
        # 计算下一个玩家
        player = (player + 1) % 2
    

    这段代码应该能解决您所遇到的问题。如果您有任何疑问或需要进一步的帮助,请随时提问。

    评论

报告相同问题?

问题事件

  • 创建了问题 9月22日

悬赏问题

  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见
  • ¥15 一共有五道问题关于整数幂的运算还有房间号码 还有网络密码的解答?(语言-python)
  • ¥20 sentry如何捕获上传Android ndk 崩溃
  • ¥15 在做logistic回归模型限制性立方条图时候,不能出完整图的困难
  • ¥15 G0系列单片机HAL库中景园gc9307液晶驱动芯片无法使用硬件SPI+DMA驱动,如何解决?