最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python入門(mén)之井字棋小游戲

 更新時(shí)間:2020年03月05日 08:06:03   作者:M青年小客  
這篇文章主要為大家詳細(xì)介紹了python入門(mén)學(xué)習(xí)之井字棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

引言:

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

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

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

#!/user/bin/python
# -*- coding: utf-8 -*-
import os
import sys
#棋盤(pán)模塊
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' #用戶(hù)1
 u2 = 'o' #用戶(hù)2
 stepNumber =1 #記錄步數(shù)
 break_fang = 0 #獲勝者記錄
 while(stepNumber<=9):
 fv = True # 判斷條件2
 while fv:
  num = input('請(qǐng)用戶(hù)u1開(kāi)始下棋:')
  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,用戶(hù)1勝,2,用戶(hù)2勝
 break_fang = forResult(dictionary)
 if break_fang > 0: break
 fv =True #清楚狀態(tài)
 stepNumber+=1
 while fv:
  num1=input('請(qǐng)用戶(hù)u2開(kāi)始下棋:')
  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('是否重新開(kāi)始? yes:no:')
 if c.find('yes')!=-1:
 main()
 else:
 print('-游戲結(jié)束-')
 return
#判斷獲勝情況
#dictionary:棋盤(pán)信息
def forResult(dictionary):
 dicts= dict(dictionary)
 if dicts['a1'] == dicts['a2'] and dicts['a2'] == dicts['a3'] and len(dicts['a3'].strip())>0:
 print('游戲結(jié)束,' + '用戶(hù)1-獲勝' if dicts['a1'] == 'x' else '用戶(hù)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é)束,' + '用戶(hù)1-獲勝' if dicts['a1'] == 'x' else '用戶(hù)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é)束,' + '用戶(hù)1-獲勝' if dicts['a1'] == 'x' else '用戶(hù)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é)束,' + '用戶(hù)1-獲勝' if dicts['a2'] == 'x' else '用戶(hù)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é)束,' + '用戶(hù)1-獲勝' if dicts['a3'] == 'x' else '用戶(hù)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é)束,' + '用戶(hù)1-獲勝' if dicts['a3'] == 'x' else '用戶(hù)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é)束,' + '用戶(hù)1-獲勝' if dicts['b1'] == 'x' else '用戶(hù)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é)束,' + '用戶(hù)1-獲勝' if dicts['c1'] == 'x' else '用戶(hù)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é)束,' + '用戶(hù)1-獲勝' if dictionary[string] == 'x' else '用戶(hù)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é)束,' + '用戶(hù)1-獲勝' if dictionary[string] == 'x' else '用戶(hù)2-獲勝')
  return 1 if dictionary[string] == 'x' else 2
 else:return 0

end:寫(xiě)完這些也有九十行代碼量了,總感覺(jué)太多了。

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python列表推導(dǎo)式 經(jīng)典代碼

    python列表推導(dǎo)式 經(jīng)典代碼

    這篇文章主要介紹了python列表推導(dǎo)式,文章內(nèi)容附有經(jīng)典的詳細(xì)代碼,感興趣的小伙按可以參考下面文章具體內(nèi)容
    2021-10-10
  • Python中l(wèi)ru_cache的使用和實(shí)現(xiàn)詳解

    Python中l(wèi)ru_cache的使用和實(shí)現(xiàn)詳解

    這篇文章主要介紹了Python 中 lru_cache 的使用和實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Python3.6 之后字典是有序的?

    Python3.6 之后字典是有序的?

    字典數(shù)據(jù)是有序的, 但是這個(gè)序不是由外部控制, 而是內(nèi)部字典定位機(jī)制的序 所以對(duì)外來(lái)講, 數(shù)據(jù)本身是無(wú)序的 你每次遍歷的順序一樣, 是因?yàn)槊杜e結(jié)果是按內(nèi)部排序輸出 而無(wú)序則表示在你無(wú)法從外部控制最終的輸出順序,下面我們來(lái)學(xué)習(xí)Python字典有序性的相關(guān)資料又當(dāng)怎樣吧
    2021-12-12
  • Python爬蟲(chóng)selenium驗(yàn)證之中文識(shí)別點(diǎn)選+圖片驗(yàn)證碼案例(最新推薦)

    Python爬蟲(chóng)selenium驗(yàn)證之中文識(shí)別點(diǎn)選+圖片驗(yàn)證碼案例(最新推薦)

    本文介紹了如何使用Python和Selenium結(jié)合ddddocr庫(kù)實(shí)現(xiàn)圖片驗(yàn)證碼的識(shí)別和點(diǎn)擊功能,感興趣的朋友一起看看吧
    2025-02-02
  • 10個(gè)Python小技巧你值得擁有

    10個(gè)Python小技巧你值得擁有

    這篇文章主要介紹了10個(gè)Python小技巧,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • python矩陣/字典實(shí)現(xiàn)最短路徑算法

    python矩陣/字典實(shí)現(xiàn)最短路徑算法

    這篇文章主要為大家詳細(xì)介紹了python矩陣/字典實(shí)現(xiàn)最短路徑算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • python math模塊的基本使用教程

    python math模塊的基本使用教程

    這篇文章主要介紹了python math模塊的基本使用教程,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01
  • tensorflow使用L2 regularization正則化修正overfitting過(guò)擬合方式

    tensorflow使用L2 regularization正則化修正overfitting過(guò)擬合方式

    這篇文章主要介紹了tensorflow使用L2 regularization正則化修正overfitting過(guò)擬合方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05
  • Python實(shí)現(xiàn)多個(gè)視頻合成一個(gè)視頻的功能

    Python實(shí)現(xiàn)多個(gè)視頻合成一個(gè)視頻的功能

    這篇文章主要介紹了可以將多個(gè)視頻拼接為一個(gè)視頻的Python工具代碼,文中的代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,快來(lái)跟隨小編一起學(xué)習(xí)一下吧
    2021-12-12
  • python 將json數(shù)據(jù)提取轉(zhuǎn)化為txt的方法

    python 將json數(shù)據(jù)提取轉(zhuǎn)化為txt的方法

    今天小編就為大家分享一篇python 將json數(shù)據(jù)提取轉(zhuǎn)化為txt的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10

最新評(píng)論

同仁县| 邵武市| 宜良县| 东方市| 大化| 陕西省| 湄潭县| 黔西县| 金山区| 磐石市| 城市| 芦溪县| 盐山县| 长沙市| 且末县| 罗源县| 萨迦县| 多伦县| 天门市| 临高县| 齐齐哈尔市| 白河县| 香港 | 克东县| 潼南县| 盐津县| 酒泉市| 简阳市| 年辖:市辖区| 吉水县| 寿宁县| 绵竹市| 获嘉县| 宝应县| 福鼎市| 边坝县| 万盛区| 邵东县| 剑河县| 尉犁县| 伊吾县|