电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

python入門之井字棋小游戲

瀏覽:3日期:2022-08-04 09:48:35

引言:

剛學python好幾天了,從java到python,基礎學起來確實比較容易,語法掌握,基本概念上都比較容易入腦。

唯一比較郁悶的是老想著用java的語法去學python代碼,這點還需要后面慢慢掌握吧,相信學多種語言的你們也有這種經歷吧。

start:開始上代碼了,希望有更好的邏輯思維來寫,自己也是用最笨拙的思路去寫的,如果有可以優化的代碼請各位大神指教

#!/user/bin/python# -*- coding: utf-8 -*-import osimport sys#棋盤模塊def model(dictionary,serial=False): if serial: print(’-(初版)井字棋游戲,輸入棋號進行對戰,’) print(’對應棋號為第一行:a1-a2-a3’,end=’,’) print(’對應棋號為第二行:b1-b2-b3’,end=’,’) print(’對應棋號為第三行:c1-c2-c3’) print(dictionary[’a1’] + ’ | ’+ dictionary[’a2’] +’ | ’+ dictionary[’a3’] +’ | ’) print(’- +- +- +-’) print(dictionary[’b1’] + ’ | ’ + dictionary[’b2’] + ’ | ’ + dictionary[’b3’] + ’ | ’) print(’- +- +- +-’) print(dictionary[’c1’] + ’ | ’ + dictionary[’c2’] + ’ | ’ + dictionary[’c3’] + ’ | ’)#主模塊def main(): dictionary={’a1’:’ ’,’a2’:’ ’,’a3’:’ ’,’b1’:’ ’,’b2’:’ ’,’b3’:’ ’,’c1’:’ ’,’c2’:’ ’,’c3’:’ ’} model(dictionary, True) u1 = ’x’ #用戶1 u2 = ’o’ #用戶2 stepNumber =1 #記錄步數 break_fang = 0 #獲勝者記錄 while(stepNumber<=9): fv = True # 判斷條件2 while fv: num = input(’請用戶u1開始下棋:’) compare=1 #判斷條件1 for x in dictionary: if x.find(num)!=-1:compare=0 if compare ==0: fv=False dictionary[num] = u1 model(dictionary) # 0:繼續 1,用戶1勝,2,用戶2勝 break_fang = forResult(dictionary) if break_fang > 0: break fv =True #清楚狀態 stepNumber+=1 while fv: num1=input(’請用戶u2開始下棋:’) compare = 1 # 判斷條件1 for x in dictionary: if x.find(num1)!=-1:compare=0 if compare == 0: fv=False dictionary[num1] = u2 model(dictionary) break_fang = forResult(dictionary) if break_fang > 0: break stepNumber+=1 gameover(break_fang)#退出下棋def gameover(break_fang): c = input(’是否重新開始? yes:no:’) if c.find(’yes’)!=-1: main() else: print(’-游戲結束-’) return#判斷獲勝情況#dictionary:棋盤信息def forResult(dictionary): dicts= dict(dictionary) if dicts[’a1’] == dicts[’a2’] and dicts[’a2’] == dicts[’a3’] and len(dicts[’a3’].strip())>0: print(’游戲結束,’ + ’用戶1-獲勝’ if dicts[’a1’] == ’x’ else ’用戶2-獲勝’) return 1 if dicts[’a1’]==’x’ else 2 elif dicts[’a1’] == dicts[’b2’] and dicts[’b2’] == dicts[’c3’] and len(dicts[’c3’].strip())>0: print(’游戲結束,’ + ’用戶1-獲勝’ if dicts[’a1’] == ’x’ else ’用戶2-獲勝’) return 1 if dicts[’a1’] == ’x’ else 2 elif dicts[’a1’] == dicts[’b1’] and dicts[’b1’] == dicts[’c1’] and len(dicts[’c1’].strip())>0: print(’游戲結束,’ + ’用戶1-獲勝’ if dicts[’a1’] == ’x’ else ’用戶2-獲勝’) return 1 if dicts[’a1’] == ’x’ else 2 elif dicts[’a2’] == dicts[’b2’] and dicts[’b2’] == dicts[’c2’] and len(dicts[’c2’].strip())>0: print(’游戲結束,’ + ’用戶1-獲勝’ if dicts[’a2’] == ’x’ else ’用戶2-獲勝’) return 1 if dicts[’a2’] == ’x’ else 2 elif dicts[’a3’] == dicts[’b3’] and dicts[’b3’] == dicts[’c3’] and len(dicts[’c3’].strip())>0: print(’游戲結束,’ + ’用戶1-獲勝’ if dicts[’a3’] == ’x’ else ’用戶2-獲勝’) return 1 if dicts[’a3’] == ’x’ else 2 elif dicts[’a3’] == dicts[’b2’] and dicts[’b3’] == dicts[’c1’] and len(dicts[’c1’].strip())>0: print(’游戲結束,’ + ’用戶1-獲勝’ if dicts[’a3’] == ’x’ else ’用戶2-獲勝’) return 1 if dicts[’a3’] == ’x’ else 2 elif dicts[’b1’] == dicts[’b2’] and dicts[’b2’] == dicts[’b3’] and len(dicts[’b3’].strip())>0: print(’游戲結束,’ + ’用戶1-獲勝’ if dicts[’b1’] == ’x’ else ’用戶2-獲勝’) return 1 if dicts[’b1’] == ’x’ else 2 elif dicts[’c1’] == dicts[’c2’] and dicts[’c2’] == dicts[’c3’] and len(dicts[’c3’].strip())>0: print(’游戲結束,’ + ’用戶1-獲勝’ if dicts[’c1’] == ’x’ else ’用戶2-獲勝’) return 1 if dicts[’c1’] == ’x’ else 2 else: return 0if __name__ ==’__main__’: main()

補一點更改思路:forResult()的另一種實現,compares()函數:少了6行代碼量。

def compares(dictionary={’’:’’},string=’’): if len(dictionary)>0 | len(string.strip())==0:print(’傳值為空!’) else: axle =(’a1’,’a3’,’b2’,’c1’,’c3’) # 四個角和中間的數特殊判斷 條件1 axle_fang=False #特殊棋號需要多加一種可能性 for x in axle: if string==x:axle_fang=True if axle_fang: #條件1 if dictionary[’a1’]==dictionary[’b2’] and dictionary[’b2’]==dictionary[’c3’] and dictionary[’c3’].strip()!=’’ or dictionary[’a3’]==dictionary[’b2’] and dictionary[’b2’]==dictionary[’c1’]and dictionary[’c1’].strip()!=’’: print(’游戲結束,’ + ’用戶1-獲勝’ if dictionary[string] == ’x’ else ’用戶2-獲勝’) return 1 if dictionary[string] == ’x’ else 2 # 拆分棋號 splitStr0,splitStr1,普通棋號只需判斷倆種a倆種可能,上下-左右間的位置 splitStr0,splitStr1 = string[0],string[1] print(splitStr0+':'+splitStr1) if dictionary[splitStr0+’1’]==dictionary[splitStr0+’2’] and dictionary[splitStr0+’2’]==dictionary[splitStr0+’3’] or dictionary[’a’+splitStr1]==dictionary[’b’+splitStr1] and dictionary[’b’+splitStr1]==dictionary[’c’+splitStr1]: print(’游戲結束,’ + ’用戶1-獲勝’ if dictionary[string] == ’x’ else ’用戶2-獲勝’) return 1 if dictionary[string] == ’x’ else 2 else:return 0

end:寫完這些也有九十行代碼量了,總感覺太多了。

控制臺打印:

python入門之井字棋小游戲

python入門之井字棋小游戲

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | 二次元影像仪|二次元测量仪|拉力机|全自动影像测量仪厂家_苏州牧象仪器 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | SEO网站优化,关键词排名优化,苏州网站推广-江苏森歌网络 | 成都APP开发-成都App定制-成都app开发公司-【未来久】 | 聚合氯化铝-碱式氯化铝-聚合硫酸铁-聚氯化铝铁生产厂家多少钱一吨-聚丙烯酰胺价格_河南浩博净水材料有限公司 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 | IHDW_TOSOKU_NEMICON_EHDW系列电子手轮,HC1系列电子手轮-上海莆林电子设备有限公司 | 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 安全,主动,被动,柔性,山体滑坡,sns,钢丝绳,边坡,防护网,护栏网,围栏,栏杆,栅栏,厂家 - 护栏网防护网生产厂家 | 煤粉取样器-射油器-便携式等速飞灰取样器-连灵动 | 带式压滤机_污泥压滤机_污泥脱水机_带式过滤机_带式压滤机厂家-河南恒磊环保设备有限公司 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | RO反渗透设备_厂家_价格_河南郑州江宇环保科技有限公司 | 实验室pH计|电导率仪|溶解氧测定仪|离子浓度计|多参数水质分析仪|pH电极-上海般特仪器有限公司 | 拉力机-万能试验机-材料拉伸试验机-电子拉力机-拉力试验机厂家-冲击试验机-苏州皖仪实验仪器有限公司 | 深圳美安可自动化设备有限公司,喷码机,定制喷码机,二维码喷码机,深圳喷码机,纸箱喷码机,东莞喷码机 UV喷码机,日期喷码机,鸡蛋喷码机,管芯喷码机,管内壁喷码机,喷码机厂家 | 黑田精工电磁阀-CAMMOZI气缸-ROSS电磁-上海茂硕机械设备有限公司 | 万师讲师网-优质讲师培训师供应商,讲师认证,找讲师来万师 | 污泥烘干机-低温干化机-工业污泥烘干设备厂家-焦作市真节能环保设备科技有限公司 | 硬度计_影像测量仪_维氏硬度计_佛山市精测计量仪器设备有限公司厂家 | 佛山商标注册_商标注册代理|专利注册申请_商标注册公司_鸿邦知识产权 | 深圳高新投三江工业消防解决方案提供厂家_服务商_园区智慧消防_储能消防解决方案服务商_高新投三江 | 没斑啦-专业的祛斑美白嫩肤知识网站-去斑经验分享 | 翻斗式矿车|固定式矿车|曲轨侧卸式矿车|梭式矿车|矿车配件-山东卓力矿车生产厂家 | 红立方品牌应急包/急救包加盟,小成本好项目代理_应急/消防/户外用品加盟_应急好项目加盟_新奇特项目招商 - 中红方宁(北京) 供应链有限公司 | 新型锤式破碎机_新型圆锥式_新型颚式破碎机_反击式打沙机_锤式制砂机_青州建源机械 | 数控走心机-走心机价格-双主轴走心机-宝宇百科 | 定制/定做冲锋衣厂家/公司-订做/订制冲锋衣价格/费用-北京圣达信 | 贵州自考_贵州自学考试网 | 能耗监测系统-节能监测系统-能源管理系统-三水智能化 | 郑州巴特熔体泵有限公司专业的熔体泵,熔体齿轮泵与换网器生产厂家 | 智成电子深圳tdk一级代理-提供TDK电容电感贴片蜂鸣器磁芯lambda电源代理经销,TDK代理商有哪些TDK一级代理商排名查询。-深圳tdk一级代理 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 沈阳液压泵_沈阳液压阀_沈阳液压站-沈阳海德太科液压设备有限公司 | 丙烷/液氧/液氮气化器,丙烷/液氧/液氮汽化器-无锡舍勒能源科技有限公司 | 油罐车_加油机_加油卷盘_加油机卷盘_罐车人孔盖_各类球阀_海底阀等车用配件厂家-湖北华特专用设备有限公司 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 沈阳建筑设计公司_加固改造设计_厂房设计_设计资质加盟【金辉设计】 | 学习安徽网| 哈尔滨发电机,黑龙江柴油发电机组-北方星光 |