Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My tic-tac_toe python script doesn't execute properly. What am I missing? import random import re def toss_coin(): coin_states = ['heads', 'tails'] return random.choice(coin_states) def get_computer_move(board):

My tic-tac_toe python script doesn't execute properly. What am I missing?

import random

import re

def toss_coin():

coin_states = ['heads', 'tails']

return random.choice(coin_states)

def get_computer_move(board):

"""This function should implement a strategy for a computer player to play"""

column = random.randint(0, 2)

row = random.randint(0, 2)

while board[row][column] != " ":

row = random.randint(0, 2)

column = random.randint(0, 2)

return row, column

def get_player_move():

"""Returns the player move as a row and column"""

valid_input = False

while not valid_input:

placement = input("Where would you like to play (enter as row,column e.g. 1,3)")

match = re.search('[1,2,3],[1,2,3]', placement)

valid_input = match is not None

if valid_input:

row, col = placement.split(',')

row, col = int(row)-1, int(col)-1

return row, col

def check_win(board):

"""Checks to see if there is a winner"""

for row in range(3):

if board[row][0] == board[row][1] == board[row][2] != ' ':

return True

for column in range(3):

if board[0][column] == board[1][column] == board[2][column] != ' ':

return True

if board[0][0] == board[1][1] == board[2][2] != ' ':

return True

elif board[2][0] == board[1][1] == board[0][2] != ' ':

return True

else:

return False

def print_board(board):

"""This function will print a tic-tac-toe board to the console"""

for index, row in enumerate(board):

print('|'.join(row))

if index < 2:

print('-----')

def play_tic_tac_toe(player_name):

board = [['' ,'', ''], ['', '', ''],['', '', '']]

print(f'Welcome to Tic Tac Toe {player_name}')

coin = toss_coin()

print(f"Flipping to see whether you or me goes first.. Its {coin}")

turn_message = "Its heads so you are x and go first" if coin == "heads" else "Its tails so I go first and you are x's"

print(turn_message)

active_game = True

while active_game:

box = get_computer_move(board)

board[box[0]][box[1]] = 'X'

moves = moves + 1

print_board(board)

if check_win(board):

print("The computer wins!")

if moves == 9:

print("Draw!")

get_player_move()

if row < 0 or row > 2 or col < 0 or col > 2:

print("Incorrect move! You lose.")

if board[row][column] != ' ':

print("The square is not available! You lose.")

board[row][column] = 'O'

moves = moves + 1

print_board(board)

if check_win(board):

print("You win!")

if __name__ == "__main__":

name = input('What is your name?')

keep_playing = True

while keep_playing:

play_tic_tac_toe(name)

#finish implement application loop

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

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

Students also viewed these Databases questions