這篇文章給大家分享的是有關(guān)python如何實(shí)現(xiàn)簡(jiǎn)單的井字棋小游戲的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)主營博興網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件開發(fā)公司,博興h5微信平臺(tái)小程序開發(fā)搭建,博興網(wǎng)站營銷推廣歡迎博興等地區(qū)企業(yè)咨詢用python實(shí)現(xiàn)的一個(gè)井字棋游戲:
#Tic-Tac-Toe 井字棋游戲 #全局常量 X="X" O="O" EMPTY=" " #詢問是否繼續(xù) def ask_yes_no(question): response=None; while response not in("y","n"): response=input(question).lower() return response #輸入位置數(shù)字 def ask_number(question ,low,high): response=None while response not in range(low,high): response=int(input(question)) return response #詢問誰先走,先走方為X,后走方為O #函數(shù)返回計(jì)算機(jī)方,玩家的角色代號(hào) def pieces(): go_first=ask_yes_no("玩家你是否先走(y/n):") if go_first=="y": print("\n玩家你先走.") human=X computer=O else: print("\n計(jì)算機(jī)先走.") computer=X human=O return computer,human #產(chǎn)生新的棋盤 def new_board(): board=[] for square in range(9): board.append(EMPTY) return board #顯示棋盤 def display_board(board): board2=board[:] for i in range(len(board)): if board[i]==EMPTY: board2[i]=i print("\t",board2[0],"|",board2[1],"|",board2[2]) print("\t","----------") print("\t",board2[3],"|",board2[4],"|",board2[5]) print("\t", "----------") print("\t",board2[6],"|",board2[7],"|",board2[8],"\n") #產(chǎn)生可以合法走棋位置序列(也就是還未下過子位置) def legal_moves(board): moves=[] for square in range(9): if board[square]==EMPTY: moves.append(square) return moves #判斷輸贏 def winner(board): # 所有贏得可能情況,例如(0,1,2)就是第一行,(0,4,8,),(2,4,6)就是對(duì)角線 WAYS_TO_WIN=((0,1,2,),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6)) for row in WAYS_TO_WIN: if board[row[0]]==board[row[1]]==board[row[2]]!=EMPTY: winner=board[row[0]] return winner #棋盤沒有空位置 if EMPTY not in board: return "TIE" return False #人走棋 def human_move (board,human): legal =legal_moves(board) move =None while move not in legal: move=ask_number("你走那個(gè)位置?(0-8):",0,9) if move not in legal: print("\n此位置已經(jīng)落過子了") #print("Fine...") return move #計(jì)算機(jī)走棋 def computer_move(board,computer ,human): board=board[:] #按優(yōu)劣順序排序的下棋走子 BEST_MOVES=(4,0,2,6,8,1,3,5,7) #最佳下棋位置順序表 #如果計(jì)算機(jī)能贏,就走那個(gè)位置 for move in legal_moves(board): board[move]=computer if winner(board)==computer: print("計(jì)算機(jī)下棋位置...",move) return move #取消走棋方案 board[move]=EMPTY #如果玩家能贏就堵住那個(gè)位置 for move in legal_moves(board): board[move]=human if winner(board)==human: print("計(jì)算機(jī)下棋位置...",move) return move #取消走棋方案 board[move]=EMPTY #如果不是上面情況,也就是這一輪贏不了 #則是從最佳下棋位置表中挑出第一個(gè)合法位置 for move in BEST_MOVES: if move in legal_moves(board): print("計(jì)算機(jī)下棋位置....",move) return move #轉(zhuǎn)換角色 def next_turn(turn): if turn ==X: return O else: return X #主函數(shù) def main(): computer,human=pieces() turn =X board=new_board() display_board(board) while not winner(board): if turn ==human: move=human_move(board,human) board[move]=human else: move=computer_move(board,computer,human) board[move]=computer display_board(board) turn=next_turn(turn) #轉(zhuǎn)換角色 #游戲結(jié)束輸出輸贏或和棋信息 the_winner=winner(board) if the_winner==computer: print("計(jì)算機(jī)贏!\n") elif the_winner==human: print("玩家贏!\n") elif the_winner=="TIE": print("平局,游戲結(jié)束\n") main() input("按任意鍵退出游戲")
感謝各位的閱讀!關(guān)于“python如何實(shí)現(xiàn)簡(jiǎn)單的井字棋小游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!