国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

python入門之井字棋小游戲

瀏覽:7日期: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 編程
相關文章:
主站蜘蛛池模板: 精品欧美日韩一区二区三区 | 日韩毛片高清免费 | 制服诱惑中文字幕 | 99久久精品国产一区二区成人 | 国产精品一区久久精品 | 爱爱亚洲| 深夜福利视频大全在线观看 | 美国毛片网 | 日本一级大黄毛片免费基地 | 99在线热视频只有精品免费 | 中文字幕一区中文亚洲 | 爱福利极品盛宴 | 中文字幕日韩一区二区不卡 | 亚洲精品一区二区三区在线播放 | 中文字幕日韩国产 | 欧美日韩一区二区视频免费看 | 亚洲天堂免费 | 日韩专区欧美 | 亚欧人成精品免费观看 | 国产a视频| 午夜性刺激免费视频观看不卡专区 | 制服丝袜怡红院 | 亚洲国产欧美在线人成aaa | www.av在线.com| 日韩看片 | 亚洲第一区视频在线观看 | 中文字幕一区二区三区久久网站 | 国产真实乱子伦精品 | 欧美毛片日韩一级在线 | 国产在线视频一区 | 亚洲欧美男人天堂 | 国产在线观看网址在线视频 | 九九久久精品国产 | 日韩一区二区中文字幕 | 草草影院ccyycom浮力影院 | 亚洲欧美视频一区二区三区 | 精品视频一区二区 | 一级国产视频 | 国内精品久久久久久中文字幕 | 国产成人精品视频午夜 | 一区二区日韩 |