哈喽大家好,我是鹏哥。
今天想记录的是 —— 用python编程24点游戏。
~~~上课铃~~~
1
写在前面
目前还处于武汉新型冠状病毒肆虐中,为了响应国家号召,不得别人添乱的原则,不出门,不聚会,不访友,所以只能在家一直葛优躺,感觉要闲慌(shuang)了。

外加今天又被公司通知要延长假期到2/2,真的是好难(kai)受(xin)呀!
![]()
既然如何无聊,那就写个手机游戏玩玩吧!以我目前的水平,自然是从最简单的24点入手了。
2
效果展示


3
知识串讲(敲黑板啦)
和之前一样,这次的24点游戏也是在iphone上基于pythonista 3 APP编程的。如果对pythonista 3不熟悉的同学,可以参考之前的文章:
【Python成长之路】如何用python开发自己的iphone应用程序,并添加至siri指令
(1)界面设计:
pythonista 3目前不支持pyqt5,因此无法通过QT Designer来快速设计GUI界面。但是Pythonista 3里有自己的GUI图形设计,可以通过创建script with UI来完成简单的界面设计。

目前pythonista 3支持的控件比较少,只有以下几种:

我这边只用了Label和Button两个控件。在界面添加控件后,可以选择控件进行详情查看,从而进入控件的参数设置界面(控件坐标、控件大小、背景颜色、字体大小/颜色/形式、文本内容等),如下:

针对button控件,我们还要添加对控件点击后的方法关联,因此需要设置下控件关联方法,Result按钮对应的方法是get_result方法,如:

关于界面设计的功能/设置等功能,就简单介绍下,自己也是初学,有些高级功能也要研究下才行。
我的界面结果如下:

(2)界面代码实现
1、label控件初始化定义
label1 = sender.superview['label1']
2、加载pyui文件
在pythonista 3中,通过load_view(xx.pyui)即可加载对应的UI界面,这点使用起来还是蛮方便的。当然我这边通过get_screen_size()来进行了屏幕判断,从而根据ipad/iphone进行界面自适应。
v = ui.load_view()v.present('sheet')
3、24点计算
这段代码是参考大神的超简短24点算法:
python 穷举法 算24点(史上最简短代码):https://www.cnblogs.com/hhh5460/p/6926909.html
大致原理也是蛮简单的,通过permutations方法对输入的4个数字进行所有排列组合,通过product方法实现+-/*运算法的笛卡尔积的元组(简单来说,就是随机组合三个运算法)。
为了加深理解,我们对这两个方法进行个简单地示例:
import itertoolsa = [1,2,3,4]result = itertools.permutations(a)print(type(result))for i in result:print(i)结果是:<class 'itertools.permutations'>(1, 2, 3, 4)(1, 2, 4, 3)(1, 3, 2, 4)(1, 3, 4, 2)(1, 4, 2, 3)(1, 4, 3, 2)(2, 1, 3, 4)(2, 1, 4, 3)(2, 3, 1, 4)(2, 3, 4, 1)(2, 4, 1, 3)(2, 4, 3, 1)(3, 1, 2, 4)(3, 1, 4, 2)(3, 2, 1, 4)(3, 2, 4, 1)(3, 4, 1, 2)(3, 4, 2, 1)(4, 1, 2, 3)(4, 1, 3, 2)(4, 2, 1, 3)(4, 2, 3, 1)(4, 3, 1, 2)(4, 3, 2, 1)
从对结果的打印可以看到,result是个迭代器。
同理,对product方法是对两个列表进行排列组合,示例代码中只是用到了repeat参数。
如果最后打印是*i,那就返回元组内的元素;如果打印是i,则返回元组。
import itertoolsa = [1,2]b = ['c','d']result = itertools.product(a,b)print(type(result))for i in result:print(i)结果是:<class 'itertools.product'>(1, 'c')(1, 'd')(2, 'c')(2, 'd')
4
示例代码
# coding=utf-8# @公众号 : "鹏哥贼优秀"# @Date : 2020/1/28# @Software : PyCharm# @Python version: Pythonista 3import uiimport randomimport itertools# 通过遍历进行计算24点def calculate_twenty_four(cards):for nums in itertools.permutations(cards): # 四个数for ops in itertools.product('+-*/', repeat=3): # 三个运算符(可重复!)# 构造三种中缀表达式 (bsd)bds1 = '({0}{4}{1}){5}({2}{6}{3})'.format(*nums, *ops) # (a+b)*(c-d)bds2 = '(({0}{4}{1}){5}{2}){6}{3}'.format(*nums, *ops) # (a+b)*c-dbds3 = '{0}{4}({1}{5}({2}{6}{3}))'.format(*nums, *ops) # a/(b-(c/d))for bds in [bds1, bds2, bds3]: # 遍历try:if abs(eval(bds) - 24.0) < 1e-10: # eval函数return bdsexcept ZeroDivisionError: # 零除错误!continuereturn 'Not found!'def get_next_number(sender):label_list = init_ui(sender)for label in label_list[:4]:label.text = str(random.randint(1,9))label_list[4].text = 'Please click OK to get the result!'def get_result(sender):label_list = init_ui(sender)card = [label.text for label in label_list[:4]]result = calculate_twenty_four(card)if result != 'Not found!':label_list[4].text = 'Result is : ' + result + '=24'else:label_list[4].text = 'Not found!'def reset(sender):label_list = init_ui(sender)for label in label_list:if label != 'label5':label.text = ''else:label.text = 'Please click Start to start game!'def init_ui(sender):label1 = sender.superview['label1']label2 = sender.superview['label2']label3 = sender.superview['label3']label4 = sender.superview['label4']label5 = sender.superview['label5']return [label1,label2,label3,label4,label5]v = ui.load_view()if min(ui.get_screen_size()) >= 768:# iPadv.frame = (0, 0, 360, 400)v.present('sheet')else:# iPhonev.present(orientations=['portrait'])
5
总结
最后来个我蛮喜欢的抖音结尾吧,哈哈。
~~~下课铃~~~

【往期热门文章】:
【Python成长之路】10行代码教你免费观看无广告版的《庆余年》腾讯视频
【Python成长之路】如何用python开发自己的iphone应用程序,并添加至siri指令
【Python成长之路】从 零做网站开发 -- 基于Flask和JQuery,实现表格管理平台

点击下方诗句,可以留言互动喔
【关注“鹏哥贼优秀”公众号,回复“python学习材料”,将会有python基础学习、机器学习、数据挖掘、高级编程教程等100G视频资料,及100+份python相关电子书免费赠送!】
扫描二维码
与鹏哥一起
学python吧!



1万+

被折叠的 条评论
为什么被折叠?



