• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
56%
SHARE
TWEET

# Untitled

a guest May 19th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. X = "X"
2. O = "O"
3. EMPTY = " "
4. TIE = "TIE"
5. NUM_SQUARES = 9
6.
7. def display_instruct():
8.     print \
9.            ( """
10.            Welcome to the greatest intellectual challenge of all time: Tic-Tac-Toe.
11.            This will be a showdown between your human brain and my silicon processor.
12.
13.            You will make your move known by entering a number, 0 - 8. The number
14.            will correspond to the board position as illustrated:
15.
16.                                         0 | 1 | 2
17.                                        -----------
18.                                         3 | 4 | 5
19.                                        -----------
20.                                         6 | 7 | 8
21.
22.            Prepare your self, human. The ultimate battle is about to begin. \n
23.            """)
24.
26.     response = None
27.     while response not in range(low, high):
28.         response = int(raw_input(question))
29.     return response
30.
31. def pieces():
32.         human = X
33.         computer = O
34.         return computer, human
35.
36. def new_board():
37.     board = []
38.     for square in range(NUM_SQUARES):
39.         board.append(EMPTY)
40.     return board
41.
42. def display_board(board):
43.     print ("\n\t", board[0], "|", board[1], "|", board[2])
44.     print ("\t", "---------")
45.     print ("\t", board[3], "|", board[4], "|", board[5])
46.     print ("\t", "---------")
47.     print ("\t", board[6], "|", board[7], "|", board[8], "\n")
48.
49. def legal_moves(board):
50.     moves = []
51.     for square in range(NUM_SQUARES):
52.         if board[square] == EMPTY:
53.             moves.append(square)
54.     return moves
55.
56. def winner(board):
57.     WAYS_TO_WIN = ((0, 1, 2),
58.                    (3, 4, 5),
59.                    (6, 7, 8),
60.                    (0, 3, 6),
61.                    (1, 4, 7),
62.                    (2, 5, 8),
63.                    (0, 4, 8),
64.                    (2, 4, 6))
65.
66.     for row in WAYS_TO_WIN:
67.         if board[row[0]] == board[row[1]] == board[row[2]] != EMPTY:
68.             winner = board[row[0]]
69.             return winner
70.
71.         if EMPTY not in board:
72.             return TIE
73.         return None
74.
75. def human_move(board, human):
76.     legal = legal_moves(board)
77.     move = None
78.     while move not in legal:
79.         move = ask_number("Where will you move? (0 - 8): ", 0, NUM_SQUARES)
80.         if move not in legal:
81.             print ("\nThat square is already occupied, foolish human. Choose another.\n")
82.     print ("Fine...")
83.     return move
84.
85. def computer_move(board, computer, human):
86.     board = board[:]
87.     BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)
88.     print ("I shall take square number")
89.
90.     # if computer can win, take that move
91.     for move in legal_moves(board):
92.         board[move] = computer
93.         if winner(board) == computer:
94.             print (move)
95.             return move
96.         board[move] = EMPTY
97.
98.     # if human can win, block that move
99.     for move in legal_moves(board):
100.         board[move] = human
101.         if winner(board) == human:
102.             print (move)
103.             return move
104.         board[move] = EMPTY
105.
106.     # since no one can win on next move, pick best open square
107.     for move in BEST_MOVES:
108.         if move in legal_moves(board):
109.             print (move)
110.             return move
111.
112. def next_turn(turn):
113.     if turn == X:
114.         return 0
115.     else:
116.         return X
117.
118. def congrat_winner(the_winner, computer, human):
119.     if the_winner != TIE:
120.         print (the_winner, "won!\n")
121.     else:
122.         print ("It's a tie!\n")
123.     if the_winner == computer:
124.         print ("As I predicted, human, I am triumphant once more. \n" \
125.             "Proof that computers are superior to humans in all regards.\n")
126.
127.     elif the_winner == human:
128.         print ("No, no! It cannot be! Somehow you tricked me, human. \n" \
129.             "But never again! I, the computer, so swears it\n!")
130.
131.     elif the_winner == TIE:
132.         print ("You were most lucky, human, and somehow managed to tie me. \n" \
133.             "Celebrate today... for this is the best you will ever achieve.\n")
134.
135. def main():
136.     display_instruct()
137.     computer, human = pieces()
138.     turn = X
139.     board = new_board()
140.     display_board(board)
141.
142.     while not winner(board):
143.         if turn == human:
144.             move = human_move(board, human)
145.             board[move] = human
146.         else:
147.             move = computer_move(board, computer, human)
148.             board[move] = computer
149.         display_board(board)
150.         turn = next_turn(turn)
151.     the_winner = winner(board)
152.     congrat_winner(the_winner, computer, human)
153.
154. main()
RAW Paste Data
Top