1 #Tic-Tac-Toe 2 #机器人和人类下井字棋 3 4 #全局变量 5 import random 6 X = "X" 7 O = "O" 8 EMPTY = " " #表示棋盘上的空空格 9 TIE = "TIE" #表示平局 10 NUM_SQUARES = 9 #井字棋棋盘上的方格数 11 12 #显示游戏说明 13 def display_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 will 19 correspond to the board position as illustrated: 20 0 | 1 | 2 21 --------- 22 3 | 4 | 5 23 --------- 24 6 | 7 | 8 25 Prepare yourself,human .The ultimate battle is about aobegin. """ 26 ) 27 28 #询问一个“是或否”的问题。接受一个问题,返回y或n 29 def ask_yes_no(quesion): 30 response = None 31 while response not in ("y","n"): 32 response = input(quesion.lower()) 33 return response 34 35 #求情指定范围内的一个数字 36 def ask_number(question,low,high): 37 response = None 38 while response not in range(low,high): 39 response = int(input(question)) 40 return response 41 42 #询问玩家是否希望先行棋 43 def pieces(): 44 go_first = ask_yes_no("Do you require the first move ? (y/n):") 45 if go_first =="y": 46 print " Then take the first move.You wil need it." 47 human = X 48 computer = O 49 else: 50 print " Your bravery will be your undoing... I will go first.." 51 human = O 52 computer = X 53 return computer, human 54 55 #创建新的q棋盘 56 def new_board(): 57 board = [] 58 for square in range(NUM_SQUARES): 59 board.append(EMPTY) 60 return board 61 62 #显示棋盘 63 def display_board(board): 64 print " ",board[0],"|",board[1],"|",board[2] 65 print " ","--------" 66 print " ",board[3],"|",board[4],"|",board[5] 67 print " ","--------" 68 print " ",board[6],"|",board[7],"|",board[8] 69 70 #接受一个棋盘,返回一个合法的行棋步骤 71 def legal_moves(board): 72 moves = [] 73 for square in range(NUM_SQUARES): 74 if board[square] == EMPTY: 75 moves.append(square) 76 return moves 77 78 #判断输赢 79 def winner(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 in WAYS_TO_WIN: 83 if board[row[0]] == board[row[1]] == board[row[2]]!=EMPTY: 84 winner = board[row[0]] 85 return winner 86 if winner=="": 87 if EMPTY not in board: 88 return TIE 89 else: 90 return None 91 92 #用户行棋 93 def human_move(board,human): 94 legal = legal_moves(board) 95 move = None 96 while move not in legal: 97 move = ask_number("Where you will move ?(0-8):",0,NUM_SQUARES) 98 if move not in legal: 99 print (" That square is already occupied,foolish human.Choose another. ") 100 print "Fine..." 101 return move 102 103 #机器人行棋 104 def computer_move(board,computer,human): 105 board = board[:] 106 BEST_MOVES =(4,0,2,6,8,1,3,5,7) 107 108 #如果机器人能赢,就走那个位置 109 for move in legal_moves(board): 110 board[move] = computer 111 if winner(board) == computer: 112 print move 113 return move 114 #技术当前行棋方案的测试,并取消之 115 board[move] =EMPTY 116 #如果玩家能赢,就堵住那个位置 117 for move in legal_moves(board): 118 board[move] = human 119 if winner(board) == human: 120 print move 121 return move 122 #技术当前行棋方案的测试,并取消之 123 board[move] =EMPTY 124 125 #由于本轮谁也赢不了,所以叫挑选最佳的空位来走 126 for move in BEST_MOVES: 127 if move in legal_moves(board): 128 print move 129 return move 130 131 #返回下一个行棋方 132 def next_turn(turn): 133 if turn == X: 134 return O 135 else: 136 return X 137 138 #接受游戏的赢家 139 def congract_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 def main(): 154 display_instruct() 155 computer,human = pieces() 156 turn = X 157 board = new_board() 158 display_board(board) 159 160 while not winner(board): 161 if turn == human: 162 move = human_move(board,human) 163 board[move] = human 164 else: 165 move = computer_move(board,computer,human) 166 board[move] = computer 167 display_board(board) 168 turn = next_turn(turn) 169 the_winner = winner(board) 170 congract_winner(the_winner,computer,human) 171 172 main()
执行过程:
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> Welcome to the gratest intellectual challenge of all time:Tic-Tac-Toe. This will be a showdown between your human brain and my silicon proessor. you will make your move known by entering a nmber,0 - 8. The number will correspond to the board position as illustrated: 0 | 1 | 2 --------- 3 | 4 | 5 --------- 6 | 7 | 8 Prepare yourself,human .The ultimate battle is about aobegin. do you require the first move ? (y/n):"y" Then take the first move.You wil need it. | | -------- | | -------- | | Where you will move ?(0-8):4 Fine... | | -------- | X | -------- | | 0 O | | -------- | X | -------- | | Where you will move ?(0-8):2 Fine... O | | X -------- | X | -------- | | 6 O | | X -------- | X | -------- O | | Where you will move ?(0-8):3 Fine... O | | X -------- X | X | -------- O | | 5 O | | X -------- X | X | O -------- O | | Where you will move ?(0-8):1 Fine... O | X | X -------- X | X | O -------- O | | 7 O | X | X -------- X | X | O -------- O | O | Where you will move ?(0-8):8 Fine... O | X | X -------- X | X | O -------- O | O | X tie! tie >>>