Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Functions can be used to replace multiple code statements. This occurs in the Tic - Tac - Toe game program within the tutorial 2 .

Functions can be used to replace multiple code statements. This occurs in the Tic-Tac-Toe game program within the tutorial 2.3.5 Finishing the Tic-Tac-Toe Program; see the printBoard function created there as an example.
What if we wanted to replace the following lines in the Tic-Tac-Toe game:
if board[row][col]=='-':
board[row][col]= playerTurn
validMove=True;
else:
print("Oops, that spot was already taken. Please select another spot.")
validMove=False
with a function called check_if_empty. For the call to this function, we could use:
validMove = check_if_empty(board,row,col,playerTurn)
where we input into the function the board, row, column and which player just made a selection (playerTurn), and it returns True if the space is empty or False if it is not to validMove.
Which code segment properly implements this function?
def check_if_empty(board, row, col, playerTurn):
if board[row][col]=='-':
board[row][col]= playerTurn
return True
else:
print("Oops, that spot was already taken. Please select another spot.")
return False
def check_if_empty(board, row, col, playerTurn):
if board[row][col]=='-':
board[row][col]= playerTurn
return False
else:
print("Oops, that spot was already taken. Please select another spot.")
return True
def check_if_empty():
if board[row][col]=='-':
board[row][col]= playerTurn
return False
else:
print("Oops, that spot was already taken. Please select another spot.")
return True
def check_if_empty(board, row, col, playerTurn):
if board[row][col]!='-':
board[row][col]= playerTurn
return True
else:
print("Oops, that spot was already taken. Please select another spot.")
return False

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

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions