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

python實(shí)現(xiàn)井字棋游戲

 更新時(shí)間:2020年03月30日 11:57:39   作者:Nick_Aaron  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)井字棋游戲的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例介紹了python實(shí)現(xiàn)井字棋游戲的方法,分享給大家,具體內(nèi)容如下

windows7下python3.4.0編譯運(yùn)行通過(guò)。由于采用了cmd調(diào)用,所以與Linux不兼容,無(wú)法在Linux下運(yùn)行。
游戲就是井字棋,小鍵盤上的數(shù)字位置對(duì)應(yīng)棋盤位置。

#本游戲python3.4.0下編寫調(diào)試,只能在windows下運(yùn)行。
import random
import subprocess
import time
#定義函數(shù)
def draw_board(the_board):
 subprocess.call("cls", shell = True)
 print(' -------\n' + ' |' + the_board[9] + '|' + the_board[8] + '|' + the_board[7] + '|\n' + ' -------\n' + ' |' + the_board[6] + '|' + the_board[5] + '|' + the_board[4] + '|\n' + ' -------\n' + ' |' + the_board[3] + '|' + the_board[2] + '|' + the_board[1] + '|\n' + ' -------')
def input_player_letter():
 letter = ' '
 while not (letter == 'X' or letter == 'O'):
 print('請(qǐng)選擇X或O作棋子:', end = '')
 letter = input().upper()
 if letter == 'X':
 return ['X', 'O']
 else:
 return ['O', 'X']
def who_first():
 if 1 == random.randint(1, 2):
 return 'computer'
 else:
 return 'player'
def is_again():
 print('再一次?(Yes or No)')
 return input().lower().startswith('y')
def is_space_free(the_board, move):
 return the_board[move] == ' '
def choose_random_from_list(the_board, move_from_list):
 possible_moves = []
 for i in move_from_list:
 if is_space_free(the_board, i):
 possible_moves.append(i)
 if len(possible_moves) != 0:
 return random.choice(possible_moves)
 else:
 return None
def make_move(the_board, the_letter, the_move):
 the_board[the_move] = the_letter
def get_board_copy(the_board):
 duplicated_board = []
 for i in board:
 duplicated_board.append(i)
 return duplicated_board
def is_board_full(the_board):
 for i in range(1, 9):
 if is_space_free(the_board, i):
 return False
 else:
 return True
def get_player_move(the_board):
 the_move = 0
 while the_move not in list(range(1, 9)) or not is_space_free(the_board, the_move):
 print('請(qǐng)輸入走步:', end = '')
 the_move = int(input())
 return the_move
def is_winner(the_board, the_letter):
 return (the_board[1] == the_letter and the_board[2] == the_letter and the_board[3] == the_letter) or (the_board[4] == the_letter and the_board[5] == the_letter and the_board[6] == the_letter) or (the_board[7] == the_letter and the_board[8] == the_letter and the_board[9] == the_letter) or (the_board[1] == the_letter and the_board[5] == the_letter and the_board[9] == the_letter) or (the_board[2] == the_letter and the_board[5] == the_letter and the_board[8] == the_letter) or (the_board[3] == the_letter and the_board[5] == the_letter and the_board[7] == the_letter) or (the_board[1] == the_letter and the_board[4] == the_letter and the_board[7] == the_letter) or (the_board[2] == the_letter and the_board[5] == the_letter and the_board[8] == the_letter) or (the_board[3] == the_letter and the_board[6] == the_letter and the_board[9] == the_letter)
def get_computer_move(the_board, computer_letter):
 global player_letter
 global move
 if player_letter == 'X':
 computer_letter = 'O'
 else:
 player_letter = 'O'
 computer_letter = 'X'
 #虛擬棋盤查看是否自己可一步得勝
 for i in range(1,9):
 copy = get_board_copy(board)
 if is_space_free(board, i):
 make_move(copy, computer_letter, i)
 if is_winner(copy, computer_letter):
 return i
 #虛擬棋盤查看是否對(duì)手可一步得勝
 for i in range(1,9):
 if is_space_free(board, i):
 copy = get_board_copy(board)
 make_move(copy, player_letter, i)
 if is_winner(copy, player_letter):
 return i
 move = choose_random_from_list(board, [1, 3, 7, 9])
 if move != 0:
 return move
 if is_space_free(board, 5):
 return 5
 return choose_random_from_list(board, [2, 4, 6, 8, 7])
print('歡迎玩 井字棋 游戲!')
time.sleep(1)
print('''▆▅▅▅▆▅▅▅▅▅▅▅▂▅▅▅▆▆▅▅▃▂▆▅▅▅▅▅▅▅▅
▆▆▆▃▂▆▆▅▃▄▆▅▂▅▆▇▇▆▆▆▄▂▆▆▆▆▆▆▆▆▅
▆▅▆▅ ▁▅▂▃▅▆▅▂▆▆▇▆▅▆▇▄▂▆▆▆▆▆▆▆▆▅
▆▅▆▆▅  ▃▆▅▆▅▂▆▇▆▅▅▆▇▅▂▆▆▆▆▆▆▆▆▅
▆▆▆▆▆▃ ▁▅▆▆▄▂▇▇▆▅▅▆▇▅▁▆▆▆▆▆▆▆▆▅
▆▅▆▆▃▂▃▁▁▅▆▄▂▇▇▆▅▆▇▇▅▂▆▆▆▅▅▅▅▅▅
▆▅▆▃▁▅▆▃▁▁▅▅▂▆▇▆▆▇▆▆▄▂▆▅▅▅▅▅▆▆▅
▆▅▆▄▅▆▆▆▄▂▂▃▃▆▆▇▇▆▆▆▅▂▆▆▆▆▆▆▆▆▆
▆▅▄▄▄▄▄▄▄▄▃ ▂▅▄▄▃▄▄▄▃▂▅▄▄▅▅▅▅▅▅
▆▅▂▂▂▂▃▃▃▃▃▂ ▁▃▂▃▃▃▃▂ ▂▃▂▃▃▃▃▃▅
▆▅▆▆▆▇▇▇▇▆▆▅▂▁▄▆▆▆▄▅▄▂▆▆▆▆▆▆▆▆▅
▆▅▆▅▆▇▆▆▆▆▆▄▄▄ ▃▆▂▂▅▄▂▆▅▅▆▅▅▆▆▅
▆▅▅▆▆▇▆▅▆▇▆▄▃▆▂ ▂▃▅▆▄▂▆▅▅▅▅▅▅▆▅
▆▅▅▆▇▆▅▅▆▇▇▄▃▆▅▂ ▃▆▅▄▂▆▅▅▅▅▅▆▆▅
▆▅▅▆▇▆▅▆▆▇▆▃▂▆▄▂▂▁▃▆▅▂▆▅▅▆▆▆▆▆▅
▆▅▆▆▇▆▆▇▇▆▆▄▂▄▁▄▅▂▁▂▅▂▆▅▆▆▆▆▆▆▅
▆▅▅▆▆▆▇▆▆▆▆▄▁▃▄▆▆▄▂▁▁▂▆▅▅▆▆▆▆▆▅
▆▅▂▂▂▂▃▂▂▂▂▂▁▃▃▃▃▂▁▁  ▂▂▂▂▂▂▃▄▅
▆▆▆▆▆▅▅▅▅▅▅▄▁▅▅▅▅▄▅▅▄ ▁▅▆▅▅▅▅▆▆
▆▆▆▆▆▆▆▆▆▆▆▅▂▆▆▆▆▆▆▆▄▂▃▂▆▆▆▆▅▅▆
▆▆▆▆▆▆▆▆▆▆▆▅▂▆▆▆▆▆▆▆▄▂▆▂▁▅▆▃▃▆▆
▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▂▆▅▁▁▃▂▅▆▆
▆▆▆▆▆▆▆▆▆▆▆▄▃▆▆▆▆▆▆▆▄▃▆▆▄▁ ▅▇▆▅
▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▃▆▆▄▁▁▁▅▆▅
▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▃▆▄▂▄▃▁ ▅▆
▆▆▆▆▆▆▆▆▆▆▆▅▃▆▆▆▆▆▆▆▅▃▅▁▄▆▆▃▁ ▄
▆▆▆▆▆▆▆▆▆▆▆▅▄▆▆▆▆▆▆▆▄▃▆▅▆▆▆▆▄▃▂''')
time.sleep(2)
subprocess.call("cls", shell = True)
while True:
 board = [' '] * 10
 player_letter, computer_letter = input_player_letter()
 turn = who_first()
 print(turn + '先走')
 time.sleep(1)
 game_is_playing = True
 while game_is_playing:
 if turn == 'player':
 draw_board(board)
 move = get_player_move(board)
 make_move(board, player_letter, move)
 if is_winner(board, player_letter):
 draw_board(board)
 print('恭喜!你贏了。')
 game_is_playinig = False
 else:
 if is_board_full(board):
  draw_board(board)
  print('平局!')
  break
 else:
  turn = 'computer'
 else:
 move = get_computer_move(board, computer_letter)
 make_move(board, computer_letter, move)
 if is_winner(board, computer_letter):
 draw_board(board)
 print('電腦勝利,你掛了!')
 game_is_playing = False
 else:
 if is_board_full(board):
  draw_board(board)
  print('平局!')
  break
 else:
  turn = 'player'
 if not is_again():
 break

更多關(guān)于python游戲的精彩文章請(qǐng)點(diǎn)擊查看以下專題:

python俄羅斯方塊游戲集合

python經(jīng)典小游戲匯總

python微信跳一跳游戲集合

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

相關(guān)文章

  • 詳解python中@classmethod和@staticmethod方法

    詳解python中@classmethod和@staticmethod方法

    在python類當(dāng)中,經(jīng)常會(huì)遇到@classmethod和@staticmethod這兩個(gè)裝飾器,那么到底它們的區(qū)別和作用是啥子呢?本文結(jié)合場(chǎng)景分析給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧
    2022-10-10
  • Python tkinter實(shí)現(xiàn)圖片標(biāo)注功能(完整代碼)

    Python tkinter實(shí)現(xiàn)圖片標(biāo)注功能(完整代碼)

    tkinter是Python下面向tk的圖形界面接口庫(kù),可以方便地進(jìn)行圖形界面設(shè)計(jì)和交互操作編程,本文通過(guò)實(shí)例代碼給大家介紹的Python tkinter實(shí)現(xiàn)圖片標(biāo)注功能,感興趣的朋友一起看看吧
    2019-12-12
  • 解決Python3 struct報(bào)錯(cuò)argument for 's' must be a bytes object

    解決Python3 struct報(bào)錯(cuò)argument for 's'&

    這篇文章主要為大家介紹了解決Python3 struct報(bào)錯(cuò)argument for 's' must be a bytes object方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • 使用Python解決常見格式圖像讀取nii,dicom,mhd

    使用Python解決常見格式圖像讀取nii,dicom,mhd

    這篇文章主要介紹了使用Python解決常見格式圖像讀取nii,dicom,mhd,下文具體操作過(guò)程需要的小伙伴可以參考一下
    2022-04-04
  • python中Tkinter詳細(xì)基礎(chǔ)教學(xué)實(shí)例代碼

    python中Tkinter詳細(xì)基礎(chǔ)教學(xué)實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于python中Tkinter詳細(xì)基礎(chǔ)教學(xué)的相關(guān)資料,文中介紹了如Label、Button、Entry、Text、Frame、Menu、Canvas、Messagebox等的基本屬性和用法,并介紹了布局管理器pack、grid和place的使用方法,需要的朋友可以參考下
    2024-12-12
  • Python多線程編程之threading模塊詳解

    Python多線程編程之threading模塊詳解

    這篇文章主要介紹了Python多線程編程之threading模塊詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • 教你用Django將前端的數(shù)據(jù)存入Mysql數(shù)據(jù)庫(kù)

    教你用Django將前端的數(shù)據(jù)存入Mysql數(shù)據(jù)庫(kù)

    這篇文章主要給大家介紹了關(guān)于如何用Django將前端的數(shù)據(jù)存入Mysql數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過(guò)圖文以及示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-11-11
  • 利用Python實(shí)現(xiàn)一鍵將頭像轉(zhuǎn)成動(dòng)漫風(fēng)

    利用Python實(shí)現(xiàn)一鍵將頭像轉(zhuǎn)成動(dòng)漫風(fēng)

    小編今天將為大家詳細(xì)介紹如何利用Python語(yǔ)言制作一個(gè)UI界面,大家可以通過(guò)一鍵點(diǎn)擊就實(shí)現(xiàn)頭像照片轉(zhuǎn)化成動(dòng)漫風(fēng)格的功能,感興趣的可以動(dòng)手嘗試一下
    2022-07-07
  • TensorFlow tf.nn.max_pool實(shí)現(xiàn)池化操作方式

    TensorFlow tf.nn.max_pool實(shí)現(xiàn)池化操作方式

    今天小編就為大家分享一篇TensorFlow tf.nn.max_pool實(shí)現(xiàn)池化操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • 用pycharm開發(fā)django項(xiàng)目示例代碼

    用pycharm開發(fā)django項(xiàng)目示例代碼

    這篇文章主要介紹了用pycharm開發(fā)django項(xiàng)目示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06

最新評(píng)論

沭阳县| 崇文区| 淮南市| 绥中县| 刚察县| 潜山县| 泸定县| 富川| 秭归县| 玛沁县| 漳平市| 东城区| 赤峰市| 屏边| 绥中县| 酉阳| 双柏县| 广州市| 东莞市| 尼木县| 都江堰市| 临武县| 新邵县| 建瓯市| 阿鲁科尔沁旗| 唐海县| 萨嘎县| 清苑县| 浪卡子县| 浑源县| 广安市| 秦皇岛市| 阿克| 唐河县| 石林| 绥宁县| 阿克苏市| 叙永县| 许昌市| 阿拉尔市| 临朐县|