欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

python制作井字棋小游戲的方法-創(chuàng)新互聯(lián)

小編給大家分享一下python制作井字棋小游戲的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、虛擬空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、中陽(yáng)網(wǎng)站維護(hù)、網(wǎng)站推廣。

引言:

剛學(xué)python好幾天了,從java到python,基礎(chǔ)學(xué)起來確實(shí)比較容易,語(yǔ)法掌握,基本概念上都比較容易入腦。

唯一比較郁悶的是老想著用java的語(yǔ)法去學(xué)python代碼,這點(diǎn)還需要后面慢慢掌握吧,相信學(xué)多種語(yǔ)言的你們也有這種經(jīng)歷吧。

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

#!/user/bin/python
# -*- coding: utf-8 -*-
import os
import sys
#棋盤模塊
def model(dictionary,serial=False):
 if serial:
  print('-(初版)井字棋游戲,輸入棋號(hào)進(jìn)行對(duì)戰(zhàn),')
  print('對(duì)應(yīng)棋號(hào)為第一行:a1-a2-a3',end=',')
  print('對(duì)應(yīng)棋號(hào)為第二行:b1-b2-b3',end=',')
  print('對(duì)應(yīng)棋號(hào)為第三行: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 #記錄步數(shù)
 break_fang = 0 #獲勝者記錄
 while(stepNumber<=9):
 fv = True # 判斷條件2
 while fv:
  num = input('請(qǐng)用戶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:繼續(xù) 1,用戶1勝,2,用戶2勝
 break_fang = forResult(dictionary)
 if break_fang > 0: break
 fv =True #清楚狀態(tài)
 stepNumber+=1
 while fv:
  num1=input('請(qǐng)用戶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('-游戲結(jié)束-')
 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('游戲結(jié)束,' + '用戶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('游戲結(jié)束,' + '用戶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('游戲結(jié)束,' + '用戶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('游戲結(jié)束,' + '用戶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('游戲結(jié)束,' + '用戶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('游戲結(jié)束,' + '用戶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('游戲結(jié)束,' + '用戶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('游戲結(jié)束,' + '用戶1-獲勝' if dicts['c1'] == 'x' else '用戶2-獲勝')
  return 1 if dicts['c1'] == 'x' else 2
 else:
 return 0
if __name__ =='__main__':
 main()

補(bǔ)一點(diǎn)更改思路:forResult()的另一種實(shí)現(xiàn),compares()函數(shù):少了6行代碼量。

def compares(dictionary={'':''},string=''):
 if len(dictionary)>0 | len(string.strip())==0:print('傳值為空!')
 else:
 axle =('a1','a3','b2','c1','c3') # 四個(gè)角和中間的數(shù)特殊判斷 條件1
 axle_fang=False #特殊棋號(hào)需要多加一種可能性
 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('游戲結(jié)束,' + '用戶1-獲勝' if dictionary[string] == 'x' else '用戶2-獲勝')
   return 1 if dictionary[string] == 'x' else 2
 # 拆分棋號(hào) splitStr0,splitStr1,普通棋號(hào)只需判斷倆種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('游戲結(jié)束,' + '用戶1-獲勝' if dictionary[string] == 'x' else '用戶2-獲勝')
  return 1 if dictionary[string] == 'x' else 2
 else:return 0

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

控制臺(tái)打?。?/p>

python制作井字棋小游戲的方法

python制作井字棋小游戲的方法

以上是“python制作井字棋小游戲的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文標(biāo)題:python制作井字棋小游戲的方法-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://chinadenli.net/article36/ddgopg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、搜索引擎優(yōu)化、響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)建站、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)