Question: python reversegam ai this program but needs to play automatically import random import sys WIDTH = 8 # Board is 8 spaces wide. HEIGHT =
python reversegam ai
this program but needs to play automatically
import random
import sys
WIDTH # Board is spaces wide.
HEIGHT # Board is spaces tall.
def drawBoardboard:
# Print the board passed to this function. Return None.
print
print
for y in rangeHEIGHT:
printsy end
for x in rangeWIDTH:
printboardxy end
printsy
print
print
def getNewBoard:
# Create a brandnew, blank board data structure.
board
for i in rangeWIDTH:
board.append
return board
def isValidMoveboard tile, xstart, ystart:
# Return False if the player's move on space xstart, ystart is invalid.
# If it is a valid move, return a list of spaces that would become the player's if they made a move here.
if boardxstartystart or not isOnBoardxstart ystart:
return False
if tile X:
otherTile O
else:
otherTile X
tilesToFlip
for xdirection, ydirection in
:
x y xstart, ystart
x xdirection # First step in the x direction
y ydirection # First step in the y direction
while isOnBoardx y and boardxy otherTile:
# Keep moving in this x & y direction.
x xdirection
y ydirection
if isOnBoardx y and boardxy tile:
# There are pieces to flip over. Go in the reverse direction until we reach the original space, noting all the tiles along the way.
while True:
x xdirection
y ydirection
if x xstart and y ystart:
break
tilesToFlip.appendx y
if lentilesToFlip: # If no tiles were flipped, this is not a valid move.
return False
return tilesToFlip
def isOnBoardx y:
# Return True if the coordinates are located on the board.
return x and x WIDTH and y and y HEIGHT
def getBoardWithValidMovesboard tile:
# Return a new board with periods marking the valid moves the player can make.
boardCopy getBoardCopyboard
for x y in getValidMovesboardCopy tile:
boardCopyxy
return boardCopy
def getValidMovesboard tile:
# Return a list of xy lists of valid moves for the given player on the given board.
validMoves
for x in rangeWIDTH:
for y in rangeHEIGHT:
if isValidMoveboard tile, x y False:
validMoves.appendx y
return validMoves
def getScoreOfBoardboard:
# Determine the score by counting the tiles. Return a dictionary with keys X and O
xscore
oscore
for x in rangeWIDTH:
for y in rangeHEIGHT:
if boardxyX:
xscore
if boardxyO:
oscore
return X:xscore, O:oscore
def enterPlayerTile:
# Let the player enter which tile they want to be
# Return a list with the player's tile as the first item and the computer's tile as the second.
tile
while not tile X or tile O:
printDo you want to be X or O
tile inputupper
# The first element in the list is the player's tile, and the second is the computer's tile.
if tile X:
return XO
else:
return OX
def whoGoesFirst:
# Randomly choose who goes first.
if random.randint:
return 'computer'
else:
return 'player'
def makeMoveboard tile, xstart, ystart:
# Place the tile on the board at xstart, ystart and flip any of the opponent's pieces.
# Return False if this is an invalid move; True if it is valid.
tilesToFlip isValidMoveboard tile, xstart, ystart
if tilesToFlip False:
return False
boardxstartystart tile
for x y in tilesToFlip:
boardxy tile
return True
def getBoardCopyboard:
# Make a duplicate of the board list and return it
boardCopy getNewBoard
for x in rangeWIDTH:
for y in rangeHEIGHT:
boardCopyxy boardxy
return boardCopy
def isOnCornerx y:
# Return True if the position is in one of the four corners.
return x or x WIDTH and y or y HEIGHT
def getPlayerMoveboard playerTile:
# Let the player enter their move.
# Return the move as x yor return the strin
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
