1 #Tic-Tac-Toe
2 #机器人和人类下井字棋
3
4 #全局变量
5 importrandom6 X = "X"
7 O = "O"
8 EMPTY = " " #表示棋盘上的空空格
9 TIE = "TIE" #表示平局
10 NUM_SQUARES = 9 #井字棋棋盘上的方格数
11
12 #显示游戏说明
13 defdisplay_instruct():14 """Display game instrcutin."""
15 print(16 """Welcome to the gratest intellectual challenge of all time:Tic-Tac-Toe.17 This will be a showdown between your human brain and my silicon proessor.18 you will make your move known by entering a nmber,0 - 8. The number will19 correspond to the board position as illustrated:20 0 | 1 | 221 ---------22 3 | 4 | 523 ---------24 6 | 7 | 825 Prepare yourself,human .The ultimate battle is about aobegin.\n"""
26 )27
28 #询问一个“是或否”的问题。接受一个问题,返回y或n
29 defask_yes_no(quesion):30 response =None31 while response not in ("y","n"):32 response =input(quesion.lower())33 returnresponse34
35 #求情指定范围内的一个数字
36 defask_number(question,low,high):37 response =None38 while response not inrange(low,high):39 response =int(input(question))40 returnresponse41
42 #询问玩家是否希望先行棋
43 defpieces():44 go_first = ask_yes_no("Do you require the first move ? (y/n):")45 if go_first =="y":46 print "\nThen take the first move.You wil need it."
47 human =X48 computer =O49 else:50 print "\nYour bravery will be your undoing... I will go first.."
51 human =O52 computer =X53 returncomputer, human54
55 #创建新的q棋盘
56 defnew_board():57 board =[]58 for square inrange(NUM_SQUARES):59 board.append(EMPTY)60 returnboard61
62 #显示棋盘
63 defdisplay_board(board):64 print "\n\t",board[0],"|",board[1],"|",board[2]65 print "\t","--------"
66 print "\n\t",board[3],"|",board[4],"|",board[5]67 print "\t","--------"
68 print "\n\t",board[6],"|",board[7],"|",board[8]69
70 #接受一个棋盘,返回一个合法的行棋步骤
71 deflegal_moves(board):72 moves =[]73 for square inrange(NUM_SQUARES):74 if board[square] ==EMPTY:75 moves.append(square)76 returnmoves77
78 #判断输赢
79 defwinner(board):80 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))81 winner = ""
82 for row inWAYS_TO_WIN:83 if board[row[0]] == board[row[1]] == board[row[2]]!=EMPTY:84 winner =board[row[0]]85 returnwinner86 if winner=="":87 if EMPTY not inboard:88 returnTIE89 else:90 returnNone91
92 #用户行棋
93 defhuman_move(board,human):94 legal =legal_moves(board)95 move =None96 while move not inlegal:97 move = ask_number("Where you will move ?(0-8):",0,NUM_SQUARES)98 if move not inlegal:99 print ("\nThat square is already occupied,foolish human.Choose another.\n")100 print "Fine..."
101 returnmove102
103 #机器人行棋
104 defcomputer_move(board,computer,human):105 board =board[:]106 BEST_MOVES =(4,0,2,6,8,1,3,5,7)107
108 #如果机器人能赢,就走那个位置
109 for move inlegal_moves(board):110 board[move] =computer111 if winner(board) ==computer:112 printmove113 returnmove114 #技术当前行棋方案的测试,并取消之
115 board[move] =EMPTY116 #如果玩家能赢,就堵住那个位置
117 for move inlegal_moves(board):118 board[move] =human119 if winner(board) ==human:120 printmove121 returnmove122 #技术当前行棋方案的测试,并取消之
123 board[move] =EMPTY124
125 #由于本轮谁也赢不了,所以叫挑选最佳的空位来走
126 for move inBEST_MOVES:127 if move inlegal_moves(board):128 printmove129 returnmove130
131 #返回下一个行棋方
132 defnext_turn(turn):133 if turn ==X:134 returnO135 else:136 returnX137
138 #接受游戏的赢家
139 defcongract_winner(the_winner,computer,humna):140 if the_winner !=TIE:141 print the_winner,"won"
142 else:143 print "tie!"
144
145 if the_winner==computer:146 print "computer win"
147 elif the_winner ==humna:148 print "human win!"
149 elif the_winner==TIE:150 print "tie"
151
152 #153 defmain():154 display_instruct()155 computer,human =pieces()156 turn =X157 board =new_board()158 display_board(board)159
160 while notwinner(board):161 if turn ==human:162 move =human_move(board,human)163 board[move] =human164 else:165 move =computer_move(board,computer,human)166 board[move] =computer167 display_board(board)168 turn =next_turn(turn)169 the_winner =winner(board)170 congract_winner(the_winner,computer,human)171
172 main()