Question: There's a problem with this code when the game ties at the end. Can you figure it out? import random X = X O =
There's a problem with this code when the game ties at the end. Can you figure it out?
import random
X = "X"
O = "O"
EMPTY = " "
TIE = "TIE"
NUM_SQUARES = 10
def ask_yes_no(question):
"""Ask a yes or no question."""
response = None
while response not in ("y", "n"):
response = input(question).lower()
return response
def ask_number(question, low, high):
"""Ask for a number within a range."""
response = None
while response not in range(low, high):
response = int(input(question))
return response
def pieces():
print('Randomly picking a player to go first...')
# generating a random integer, either 0 or 1
ch = random.randint(0, 1)
if ch == 0:
print(" Great! You have the first move.")
human = X
computer = O
else:
print(" Looks like I'm gonna go first.")
computer = X
human = O
return computer, human
def new_board():
board = []
for square in range(NUM_SQUARES):
board.append(EMPTY)
return board
def display_board(board):
print(" \t", board[1], "|", board[2], "|", board[3])
print("\t", "---------")
print("\t", board[4], "|", board[5], "|", board[6])
print("\t", "---------")
print("\t", board[7], "|", board[8], "|", board[9], " ")
def legal_moves(board):
moves = []
for square in range(NUM_SQUARES):
if board[square] == EMPTY:
moves.append(square)
return moves
def winner(board):
WAYS_TO_WIN = ((1, 2, 3),
(4, 5, 6),
(7, 8, 9),
(1, 4, 7),
(2, 5, 8),
(3, 6, 9),
(1, 5, 9),
(3, 5, 7))
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 None
def human_move(board, human):
legal = legal_moves(board)
move = None
while move not in legal:
move = ask_number("Where will you move? (0 - 8):", 1, NUM_SQUARES)
if move not in legal:
print(" That square is already occupied, foolish human. Choose another. ")
print("Fine... ")
return move
def computer_move(board, computer, human):
# make a copy to work with since function will be changing list
board = board[:]
# the best positions to have, in order
BEST_MOVES = (5, 1, 3, 7, 9, 2, 4, 6, 8)
print("I shall take square number", end=" ")
# if computer can win, take that move
for move in legal_moves(board):
board[move] = computer
if winner(board) == computer:
print(move)
return move
# done checking this move, undo it
board[move] = EMPTY
# if human can win, block that move
for move in legal_moves(board):
board[move] = human
if winner(board) == human:
print(move)
return move
# done checkin this move, undo it
board[move] = EMPTY
for move in BEST_MOVES:
if move in legal_moves(board):
print(move)
return move
def next_turn(turn):
"""Switch turns."""
if turn == X:
return O
else:
return X
def congrat_winner(the_winner, computer, human):
if the_winner != TIE:
print(the_winner, "won! ")
else:
print("It's a tie! ")
if the_winner == computer:
print("I predicted as much. ")
elif the_winner == human:
print("No ")
elif the_winner == TIE:
print("Lucky ")
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)
the_winner = winner(board)
congrat_winner(the_winner, computer, human)
if __name__ == '__main__':
main()
input(" Press the enter key to quit.")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
