您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > AI人工智能 > python小程序1个
#Tic-Tac-Tic#这是一个井子棋游戏,由玩家和电脑共同完成的X=XO=OEMPTY=TIE=TIENUM_SQUARES=9defdisplay_instruct():Displaygameinstructions.print(Welcometothegreattestintellectualchallengeofalltime:\Tic-TAC-Toe.Thiswillbeashowdownbetweenyourhumanbrainandmysilicon\processor.Youwillmakeyourmoveknownbyenteringanumber,0-8.Thenumberwillcorrespondtotheboardpositionasillustrated.0|1|2---------3|4|5---------6|7|8Prepareyourself,human.Theultimatebattleisabouttobegin.\n)defask_yes_no(question):Askayesornoquestion.response=Nonewhileresponsenotin(y,n):response=input(question).lower()returnresponsedefask_number(question,low,high):Askforanumberwithinarange.response=Nonewhileresponsenotinrange(low,high):response=int(input(question))returnresponsedefpieces():Determineifplayerorcomputergoesfirst.go_first=ask_yes_no(Doyourequirethefirstmove?(y/n):)ifgo_first==y:print(\nThentakethefirstmove.Youwillneedit.)human=Xcomputer=Oelse:print(\nYourbraverywillbeyourundoing...Iwillgofirst.)computer=Xhuman=Oreturncomputer,humandefnew_board():Createnewgameboard.board=[]forsquareinrange(NUM_SQUARES):board.append(EMPTY)returnboarddefdisplay_board(board):Displaygameboardonscreen.print(\n\t,board[0],|,board[1],|,board[2])print(\t,----------)print(\t,board[3],|,board[4],|,board[5])print(\t,----------)print(\t,board[6],|,board[7],|,board[8],\n)deflegal_moves(board):Createlistoflegalmoves.moves=[]forsquareinrange(NUM_SQUARES):ifboard[square]==EMPTY:moves.append(square)returnmovesdefwinner(board):Determinethegamewinner.WAYS_TO_WIN=((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(2,4,6))forrowinWAYS_TO_WIN:ifboard[row[0]]==board[row[1]]==board[row[2]]!=EMPTY:winner=board[row[0]]returnwinnerifEMPTYnotinboard:returnTIEreturnNonedefhuman_move(board,human):Gethumanmove.legal=legal_moves(board)move=Nonewhilemovenotinlegal:move=ask_number(Wherewillyoumove?(0-8):,0,NUM_SQUARES)ifmovenotinlegal:print(\nThatsquareisalreadyoccupied,foolishhuman,Chooseanother.\n)print(Fine...)returnmovedefcomputer_move(board,computer,human):Makecomputermove.board=board[:]BEST_MOVES=(4,0,2,6,8,1,3,5,7)print(Ishalltakesquarenumber,end=)formoveinlegal_moves(board):board[move]=computerifwinner(board)==computer:print(move)returnmoveboard[move]=EMPTYformoveinlegal_moves(board):board[move]=humanifwinner(board)==human:print(move)returnmoveboard[move]=EMPTYformoveinBEST_MOVES:ifmoveinlegal_moves(board):print(move)returnmovedefnext_turn(turn):Switchturns.ifturn==X:returnOelse:returnXdefcongrat_winner(the_winner,computer,human):Congratulatethewinner.ifthe_winner!=TIE:print(the_winner,won!\n)else:print(It'satie!\n)ifthe_winner==computer:print(AsIpredicted,human,Iamtriumphantoncemore.\n\Proofthatcomputersaresuperiortohumaninallregards.)elifthe_winner==human:print(No,no!Itcannotbe!Somehowyoutrickedme,human.\n\Butneveragain!I,thecomputer,soswearit!)elifthe_winner==TIE:print(Youweremostlucky,human,andsomehowmanagedtotieme.\nCelebraatetoday...forthisthebestyouwilleverachieve.)defmain():display_instruct()computer,human=pieces()turn=Xboard=new_board()display_board(board)whilenotwinner(board):ifturn==human:move=human_move(board,human)board[move]=humanelse:move=computer_move(board,computer,human)board[move]=computerdisplay_board(board)turn=next_turn(turn)the_winner=winner(board)congrat_winner(the_winner,computer,human)main()input(\n\nPresstheenterkeytoexit.)
本文标题:python小程序1个
链接地址:https://www.777doc.com/doc-4210718 .html