Python结合Tkinter实现简单的15 puzzle游戏

 更新时间:2026年06月16日 08:25:36   作者:金銀銅鐵  
15 puzzle 是维基百科中关于该游戏的介绍,这篇文章主要介绍了Python如何结合Tkinter实现简单的15 puzzle游戏,感兴趣的小伙伴可以了解下

背景

TkDocs tutorial 里介绍了 Tkinter,其中有 A First (Real) Example 一文,这篇文章里有一个使用Tkinter 生成图形化界面的简单例子。我想在那篇文章的基础上实战一下,于是想到可以实现简单的 15 puzzle。15 puzzle 是维基百科中关于该游戏的介绍。

需要解决哪些问题

整体布局

整体布局的草图如下  (有些细节并不准确,这里只是展示一下大意)

一共需要 222frame 

  • 上方的frame 用于展示所有按钮,其中包括
    • 15个数字按钮
    • 1个表示空位置的按钮
  • 下方的 frame 用于展示提示信息

如何生成随机开局?

我们可以对 161个按钮执行shuffle 操作,从而得到随机的开局。但是 15 puzzle 里提到,并非所有的开局都有解。于是我想到,可以从最终局面倒着来(这样可以保证用户看到的局面总是有解的)。具体操作是这样的:最终局面是这样的

我们可以随机点击与空位置相邻的某个按钮。例如,如果点击 12 的话,得到的结果会是 

这样操作多次之后,就可以得到一个看似“随机”的开局。一个可能的开局如下

这部分的关键代码如下(略去了一些细节)

def shuffle_buttons():
    while True:
        for _ in range(100):
            swap_empty_with_random_neighbor()
        if not all_buttons_at_original_position():
            break
    reset_click_cnt()
    update_message()

def initialize_buttons():
    for r in range(n):
        for c in range(n):
            add_button(r, c)

def initialize_board():
    initialize_buttons()
    shuffle_buttons()
    
initialize_board()

如何交换两个button?

我们并不需要真的交换两个 button 的指针或者引用。只要交换两个buttontext,就会造成 “这两个button 被交换了” 的错觉。这部分的关键代码如下(略去了一些细节)⬇️ 请注意,表示 “空位置” 的那个 button 总是会参与 “交换” 操作。

def update_button_text(button_position, text):
    button_dict[button_position]['text'] = text

def build_state(disable):
    return ['disabled'] if disable else ['!disabled']

def update_button_state(button_position, disable):
    state = build_state(disable)
    button_dict[button_position].state(state)

def swap_empty_button(normal_button_position):
    normal_button_text = find_button_text(normal_button_position)
    update_button_text(empty_button_position, normal_button_text)
    update_button_text(normal_button_position, '')

    update_button_state(normal_button_position, True)
    update_button_state(empty_button_position, False)

    update_empty_button_position(normal_button_position)

    update_click_cnt()
    update_message()

基于以上分析,再结合 TkDocs tutorial 里介绍的 Tkinter 的知识,可以写出完整的代码

完整的代码

from tkinter import *
from tkinter import ttk
import random

root = Tk()
root.title("15 Puzzle")

mainframe = ttk.Frame(root)
mainframe.grid(column=0, row=0, sticky=W)
mainframe['padding'] = 5

messageframe = ttk.Frame(root)
messageframe.grid(column=0, row=1, sticky=W)
messageframe['padding'] = 5

n = 4
button_dict = {}
empty_button_position = (n - 1, n - 1)
click_cnt = 0

message_label = ttk.Label(messageframe, text='')
message_label.grid(column=0, row=0, sticky=W)

delta_position_candidates = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def is_inside_board(row, col):
    return row >= 0 and row < n and col >= 0 and col < n

def is_empty_button(row, col):
    if not is_inside_board(row, col):
        return False
    return button_dict[(row, col)]['text'] == ''

def to_index(row, col):
    return row * n + col

def update_button_text(button_position, text):
    button_dict[button_position]['text'] = text

def build_state(disable):
    return ['disabled'] if disable else ['!disabled']

def update_button_state(button_position, disable):
    state = build_state(disable)
    button_dict[button_position].state(state)

def update_empty_button_position(new_position):
    global empty_button_position
    empty_button_position = new_position

def all_buttons_at_original_position():
    for row in range(n):
        for col in range(n):
            if find_button_text((row, col)) != to_original_text(row, col):
                return False
    return True

def find_button_text(button_position):
    return button_dict[button_position]['text']

def update_click_cnt():
    global click_cnt
    click_cnt += 1

def reset_click_cnt():
    global click_cnt
    click_cnt = 0

def swap_empty_button(normal_button_position):
    normal_button_text = find_button_text(normal_button_position)
    update_button_text(empty_button_position, normal_button_text)
    update_button_text(normal_button_position, '')

    update_button_state(normal_button_position, True)
    update_button_state(empty_button_position, False)

    update_empty_button_position(normal_button_position)

    update_click_cnt()
    update_message()
    
def update_message():
    if all_buttons_at_original_position():
        message_label['text'] = 'You won after clicking %d times' % click_cnt
        for button_position in button_dict:
            update_button_state(button_position, True)
    else:
        message_label['text'] = 'You have clicked %d times' % click_cnt

def move(normal_button_position):
    row, col = normal_button_position
    for candidate in delta_position_candidates:
        delta_row, delta_col = candidate
        target_button_position = (row + delta_row, col + delta_col)
        if target_button_position != empty_button_position:
            continue
        swap_empty_button(normal_button_position)

def to_original_text(row, col):
    if (row, col) == (n - 1, n - 1):
        return ''
    return str(row * n + col + 1)

def add_button(row, col):
    text = to_original_text(row, col)
    button = ttk.Button(mainframe, text=text, command=lambda: move((row, col)))
    button.grid(column=col, row=row, sticky='WE')
    button_dict[(row, col)] = button
    if (row, col) == (n - 1, n - 1):
        update_button_state((row, col), True)

def swap_empty_with_random_neighbor():
    row, col = empty_button_position
    while True:
        delta_row, delta_col = random.choice(delta_position_candidates)
        if not is_inside_board(row + delta_row, col + delta_col):
            continue
        normal_button_position = (row + delta_row, col + delta_col)
        swap_empty_button(normal_button_position)
        break

def shuffle_buttons():
    while True:
        for _ in range(100):
            swap_empty_with_random_neighbor()
        if not all_buttons_at_original_position():
            break
    reset_click_cnt()
    update_message()

def initialize_buttons():
    for r in range(n):
        for c in range(n):
            add_button(r, c)

def initialize_board():
    initialize_buttons()
    shuffle_buttons()

initialize_board()

root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
for child in mainframe.winfo_children(): 
    child.grid_configure(padx='1', pady='1')

root.mainloop()

运行效果

请将完整的代码(上文已提供)保存为 fifteen.py。使用下方的命令可以运行 fifteen.py

python3 fifteen.py

运行效果如下图所示(您在自己电脑上运行该程序得到的开局很可能和下图展示的局面不同)

对这个局面而言,可以点击的位置是3,7,113 个按钮

如果点击 3,局面变为 ⬇️ (请注意,“空位置” 和 3 发生了交换)

我玩了一会儿,终于得到了预期的局面 ⬇️ (一共有72 次有效的点击)

其他

整体布局的草图是如何画出来的?

我用了 IntelliJ IDEA (Community Edition)PlantUML 的插件来画那张图。完整的代码如下 ⬇️

@startsalt
{
    {+
        [ 1]|[ 2]|[ 3]|[ 4]
        [ 5]|[ 6]|[ 7]|[ 8]
        [ 9]|[10]|[11]|[12]
        [13]|[14]|[15]|[  ]
    }
    {+
        展示已操作次数
    }
}
@endsalt

到此这篇关于Python结合Tkinter实现简单的15 puzzle游戏的文章就介绍到这了,更多相关Python Tkinter开发内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python3 selenium自动化测试 强大的CSS定位方法

    python3 selenium自动化测试 强大的CSS定位方法

    今天小编就为大家分享一篇python3 selenium自动化测试 强大的CSS定位方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Python+PyQt5实现数据库表格动态增删改

    Python+PyQt5实现数据库表格动态增删改

    这篇文章主要为大家介绍如何利用Python中的PyQt5模块实现对数据库表格的动态增删改,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-03-03
  • 全景解析Python中可变对象与不可变对象的核心区别和实际应用

    全景解析Python中可变对象与不可变对象的核心区别和实际应用

    本文将重点剖析Python所有进阶开发者都无法绕开的核心命题,即可变对象(Mutable)与不可变对象(Immutable)的底层差异,并结合真实的架构场景探讨如何在实战中做出最优抉择
    2026-03-03
  • Python的encode和decode使用指南

    Python的encode和decode使用指南

    在编程的浩瀚海洋中,字符编码与解码无疑是那块最神秘、却又最基础的罗塞塔石碑,今天我们将彻底撕开Python中 encode() 和 decode() 的华丽面具,直抵其二进制灵魂的最深处,需要的朋友可以参考下
    2026-02-02
  • Python实现Ollama的提示词生成与优化

    Python实现Ollama的提示词生成与优化

    这篇文章主要为大家详细介绍了Python实现Ollama的提示词生成与优化的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-12-12
  • 基于Python制作炸金花游戏的过程详解

    基于Python制作炸金花游戏的过程详解

    《诈金花》又叫三张牌,是在全国广泛流传的一种民间多人纸牌游戏。比如JJ比赛中的诈金花(赢三张),具有独特的比牌规则。本文江将通过Python语言实现这一游戏,需要的可以参考一下
    2022-02-02
  • Python3.6+Django2.0以上 xadmin站点的配置和使用教程图解

    Python3.6+Django2.0以上 xadmin站点的配置和使用教程图解

    django自带的admin站点虽然功能强大,但是界面不是很好看。这篇文章主要介绍了Python3.6+Django2.0以上 xadmin站点的配置和使用 ,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-06-06
  • Python判断Abundant Number的方法

    Python判断Abundant Number的方法

    这篇文章主要介绍了Python判断Abundant Number的方法,实例分析了Python针对盈数的判断技巧,需要的朋友可以参考下
    2015-06-06
  • Django的models中on_delete参数详解

    Django的models中on_delete参数详解

    这篇文章主要介绍了Django的models中on_delete参数详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • Python Django实现layui风格+django分页功能的例子

    Python Django实现layui风格+django分页功能的例子

    今天小编就为大家分享一篇Python Django实现layui风格+django分页功能的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08

最新评论