Question: python tic tac toe but import random def drawBoard ( board ) : # This function prints out the board that it was passed. #
python tic tac toe but
import random
def drawBoardboard:
# This function prints out the board that it was passed.
# "board" is a list of strings representing the board ignore index
printboard board board
print
printboard board board
print
printboard board board
def inputPlayerLetter:
#Lets the player type which letter they want to be
#Returns a list with the player's letter as the first item and the computer's letter as the second.
letter
while not letter X or letter O:
printDo you want to be X or O
letter inputupper
# The first element in the list is the player's letter; the second is the computer's letter,
if letter X:
return XO
else:
return OX
def whoGoesFirst:
# Randomly choose which player goes first.
if random.randint:
return 'computer'
else:
return 'player'
def makeMoveboard letter, move:
boardmove letter
def isWinnerbo le:
# Given a board and a player's letter, this function returns True if that player has won.
# We use bo instead of "board" and le instead of "letter" so we don't have to type as much.
return bo le and bo le and bo le or # Across the
top
bo le and bo le and bo le or # Across the middle
bo le and bo le and bo le or # Across the bottom
bo le and bo le and bo le or # Down the left side
bo le and bo le and bo le or # Down the middle
bo le and bo le and bo le or # Down the right
side
bo le and bo le and bo le or # Diagonal
bo le and bo le and bo le # Diagonal
def getBoardCopyboard:
# Make a copy of the board list and return it
# boardCopy
for i in board:
boardCopy.appendi
return boardCopy
def isSpaceFreeboard move:
# Return True if the passed move is free on the passed board.
return boardmove
def getPlayerMoveboard:
# Let the player enter their move.
move
while move not in split:
isSpaceFreeboard intmove
printWhat is your next move?
move input
input intmove
def chooseRandomMoveFromListboard movesList:
# Returns a valid move from the passed list on the passed board.
# Returns None if there is no valid move.
possibleMoves
for i in movesList:
if isSpaceFreeboard i:
possibleMoves.appendi
if lenpossibleMoves:
return random.choicepossibleMoves
else:
return None
def getComputerMoveboard computerLetter:
# Given a board and the computer's letter, determine where to move and return that move.
if computerLetter X:
playerLetter O
else:
playerLetter X
# Here is the algorithm for our TicTacToe AI:
# First, check if we can win in the next move.
for i in range:
boardCopy getBoardCopyboard
if isSpaceFreeboardCopy i:
makeMoveboardCopy computerLetter, i
if isWinnerboardCopy computerLetter:
return i
# Check if the player could win on their next move and block them.
for i in range:
boardCopy getBoardCopyboard
if isSpaceFreeboardCopy i:
makeMoveboardCopy playerLetter, i
if isWinnerboardCopy playerLetter:
return i
# Try to take one of the corners, if they are free.
move chooseRandomMoveFromListboard
if move None:
return move
# Try to take the center, if it is free.
if isSpaceFreeboard:
return
# Move on one of the sides.
return chooseRandomMoveFromListboard
def isBoardFullboard:
# Return True if every space on the board has been taken. Otherwise, return False.
for i in range:
if isSpaceFreeboard i:
return False
return True
printWelcome to TicTacToe!
while True:
# Reset the board.
theBoard
playerLetter, computerLetter inputPlayerLetter
turn whoGoesFirst
printThe turn will go first.
gameIsPlaying True
while gameIsPlaying:
if turn 'player':
# Player's turn
drawBoardtheBoard
move getPlayerMovetheBoard
this way why is it failing to run
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
