Question (Python) Write the function place_piece(board, pos, piece) that takes the Tic-Tac-Toe board game board , the in

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Question (Python) Write the function place_piece(board, pos, piece) that takes the Tic-Tac-Toe board game board , the in

Post by answerhappygod »

Question (Python)
Write the function place_piece(board, pos, piece)
that takes the Tic-Tac-Toe board game
board , the input position (pos) and the player piece (piece). The
function modifies the board
if the input is valid such that the player piece is placed in the
given position. If the board is modified, the function returns a
Boolean (bool) True indicating the move was valid. Otherwise,
return False and the board is left unmodified signifying the move
was not valid.
Assumptions
• The will be no winner yet
• The move will be made by the correct player (i.e., no player
moves out of turn)
• board will be a valid board configuration
• 1 <= pos <= 9
Restrictions
• The input board must be modified if the move is valid and
unmodified otherwise
Hint
• How do you modify the board and still return?
Skeleton code
"""
Tic Tac Toe
"""
from pprint import pprint
def create_board(r, c):
return [['']*c for _ in range(r)]

def print_board(board):
for i in range(2):
print('|'.join(map(str, board)))
print('-+-+-')
print('|'.join(map(str, board[-1])))
def play_game(placer,checker):
# Setup the board
board = create_board(3, 3)
for i in range(3):
for j in range(3):
board[j] = i*3+j+1
# Start the game
player = 0
pieces = ['O', 'X']
print_board(board)
# Play the game
for _ in range(9):
print()
valid = False
while not valid:
pos = int(input(f'Player {pieces[player]} move: '))
valid = placer(board, pos, pieces[player])
if not valid:
print(f'Invalid move!')
print_board(board)
winner = checker(board)
if winner:
print(f'Player {winner} won!')
return
player = 1 - player
print('Draw')
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply