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

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

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

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

windows7下python3.4.0編譯運行通過。由于采用了cmd調用,所以與Linux不兼容,無法在Linux下運行。
游戲就是井字棋,小鍵盤上的數(shù)字位置對應棋盤位置。

#本游戲python3.4.0下編寫調試,只能在windows下運行。
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('請選擇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('請輸入走步:', 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
 #虛擬棋盤查看是否對手可一步得勝
 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

更多關于python游戲的精彩文章請點擊查看以下專題:

python俄羅斯方塊游戲集合

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

python微信跳一跳游戲集合

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

相關文章

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

    詳解python中@classmethod和@staticmethod方法

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

    Python tkinter實現(xiàn)圖片標注功能(完整代碼)

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

    解決Python3 struct報錯argument for 's'&

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

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

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

    python中Tkinter詳細基礎教學實例代碼

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

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

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

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

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

    利用Python實現(xiàn)一鍵將頭像轉成動漫風

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

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

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

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

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

最新評論

庄浪县| 兴化市| 巴里| 瓮安县| 东台市| 宝兴县| 肃宁县| 铁力市| 瑞金市| 新津县| 盐边县| 贵阳市| 芦山县| 团风县| 襄城县| 东山县| 淄博市| 临安市| 衡东县| 磐安县| 乌拉特中旗| 阳谷县| 滨海县| 山阳县| 广宁县| 淄博市| 赤城县| 镇远县| 长宁区| 边坝县| 宁国市| 安陆市| 澎湖县| 壤塘县| 杭锦旗| 郧西县| 澎湖县| 祁门县| 黎城县| 郓城县| 钟山县|