Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'll take square Traceback (most recent call last): line 199, in main() line 189, in main board[move] = computer TypeError: list indices must be integers

I'll take square Traceback (most recent call last): line 199, in main() line 189, in main board[move] = computer TypeError: list indices must be integers or slices, not NoneType

import random

X = "X"

O = "O"

EMPTY = " "

TIE = "TIE"

NUM_SQUARES = 10

def ask_yes_no(question):

response = None

while response not in ("y", "n"):

response = input(question).lower()

return response

def ask_number(question, low, high):

while True:

try:

response = int(input("Enter a number within the range: "))

except ValueError:

continue

if low <= response <= high:

return response

# updated the method

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 is 0, human player will take the first move, otherwise computer

# will take the first move

if ch == 0:

print(" Darn...Looks like the odds started with you. ")

human = X

computer = O

else:

print(" Looks like I'm in favor of the odds today. 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 makeMoves(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):

moves = makeMoves(board)

move = None

while move not in moves:

move = ask_number("Choose your move: ", 1, NUM_SQUARES)

if move not in moves:

print(" You kidding? I took that one already. 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'll take square", end=" ")

# if computer can win, take that move

for move in makeMoves(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 makeMoves(board):

board[move] = human

if winner(board) == human:

print(move)

return move

# done checkin this move, undo it

board[move] = EMPTY

# since no one can win on next move, pick best open square

for move in BEST_MOVES:

if move in makeMoves(board):

print(move)

return move

# Turn rotation

def next_turn(turn):

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("Too bad. Maybe next time. ")

elif the_winner == human:

print("No.......! I'm supposed to be unbeatable! ")

elif the_winner == TIE:

print("That was just luck on your part. ")

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)

# start the program

if __name__ == '__main__':

main()

input(" Press the enter key to quit.")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions

Question

ERP Implementation at Zostner Industries.

Answered: 1 week ago

Question

What is the future for sustainable incentives and rewards?

Answered: 1 week ago

Question

What is the current year you are evaluating?

Answered: 1 week ago