Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using python do the following If we set up our program so that the human player always starts and plays the Xs the computer could

Using python do the following

If we set up our program so that the human player always starts and plays the Xs the computer could play moves as follows: first priority is to complete a row, column or diagonal of Os (i.e. if there are already two Os in the row); second priority is to block completion of a row, column or diagonal of Xs; and third priority is to choose randomly from the available cells. To help with these first two priorities for the computer's move we can write functions to determine which cell would complete a row, column or diagonal of Xs or Os. Such functions would have as parameters the record_of_moves and the player_symbol (X or O) and return a tuple of the cell coordinates or (-1, -1) if there's no such move. The third priority for the computer move, called random_move, would return the cell coordinates of the random choice. This is provided for you. Your functions must satisfy the test cases in the Q.2 test harness file. The functions return a tuple - the coordinates (r,c) of the cell. The test harness file has more information.

the test suite is as follows:

#

# You are to write the three functions immediately below.

#

# The other functions use the ones you write (examine them carefully) in order

# to calculate the computer's move. Note that the function computer_turn

# returns the (r, c) coordinates of a winning move (if it can), a blocking move

# move if it must) or a random move (if thats the remaining option).

#

# The function winning_move calls the functions you write with "O" as the

# player symbol thereby determining where to place an O in order to win.

#

# The function blocking_move calls the functions you write with "X" as the

# player symbol thereby determining where to place an O in order to block a

# winning combination of Xs.

#

# The function random_move returns where to place an "O" by random choice from

# the available empty cells.

#

# Once you have written and tested the three functions the test cases for

# computer_turn should work.

# Add another few calls to computer_turn such that the random choice

# is made and you can thereby check that it works as expected.

#

import turtle, sys, random

def winning_diagonal(moves_played, player_symbol):

''' return a (row, col) tuple being the cell for move that completes

a diagonal, (-1, -1) if none '''

def winning_horizontal(moves_played, player_symbol):

''' return (row, col) tuple of cell that completes three horizontal,

or (-1,-1) if none'''

def winning_vertical(moves_played, player_symbol):

''' return (row, col) tuple of cell that completes three vertical,

or (-1,-1) if none'''

def winning_move(moves_played):

''' return (row, col) cell tuple of move that wins, or (-1, -1) if

no such move'''

#

# check for winning vertical move, winning horizontal move or winning

# diagonal move

#

if winning_horizontal(moves_played,"O") != (-1,-1):

return winning_horizontal(moves_played, "O")

elif winning_vertical(moves_played, "O") != (-1,-1):

return winning_vertical(moves_played, "O")

elif winning_diagonal(moves_played, "O") != (-1,-1):

return winning_diagonal(moves_played, "O")

else:

return (-1,-1)

def blocking_move(moves_played):

''' return (row, col) cell tuple of move that blocks, or (-1, -1) if

no such move'''

if winning_horizontal(moves_played,"X") != (-1,-1):

return winning_horizontal(moves_played, "X")

elif winning_vertical(moves_played, "X") != (-1,-1):

return winning_vertical(moves_played, "X")

elif winning_diagonal(moves_played, "X") != (-1,-1):

return winning_diagonal(moves_played, "X")

else:

return (-1,-1)

def num_unplayed(moves):

''' return count of how many unplayed moves'''

count = 0

for row in moves:

for col in row:

if col == " ":

count += 1

return count

def random_move(moves_played):

''' return (row, col) tuple of randomly chosen move from available cells

in moves_played 2-D list'''

#

# Find out how many unplayed moves there are (i.e. empty cells) and choose

# a random integer between 1 and that number.

#

n_choices = num_unplayed(moves_played)

choice = rand_choice.randint(1, n_choices)

#

# Iterate through the lists counting unplayed (empty) cells until count

# reaches the random integer - thats the chosen cell.

#

count = 0

for row in range(len(moves_played)):

for col in range(len(moves_played[0])):

#

# increment counter of unplayed cells until equals random

# choice

#

if moves_played[row][col] == " ":

count += 1

if count == choice:

print("num=",n_choices,"rand n=",choice, "(row,col):",row+1, col+1)

return(row+1, col+1)

def computer_turn(moves):

'''return the (row, col) tuple for the computer move given

the game board moves so far'''

#

# determine if there's a winning move, i.e. can complete a row,

# column or diagonal

#

if winning_move(moves) != (-1, -1):

return winning_move(moves)

#

# determine if must block completion of a row, column or diagonal

#

elif blocking_move(moves) != (-1, -1):

return blocking_move(moves)

#

# otherwise choose random move

#

else:

return random_move(moves)

#

# ============================================================================

# Test suite - do not change anything between this and the next marker comment

# lines.

# Functions for unit testing of function

#

def print_test(did_pass):

''' print particular test result'''

linenum = sys._getframe(1).f_lineno

if did_pass:

print('Test at line '+str(linenum)+' ok.')

else:

print('Test at line '+str(linenum)+' FAILED.')

def test_suite():

''' test functions '''

print("function: winning_diagonal")

print_test(winning_diagonal([["O","X","X"],["X","O","X"],["X","O"," "]],"O") == (3,3))

print_test(winning_diagonal([["O"," ","O"],["O"," ","O"],["O","O","O"]],"O") == (2,2))

print_test(winning_diagonal([[" ","O","O"],["X","O","X"],["X","O","O"]],"O") == (1,1))

print_test(winning_diagonal([["X","X","O"],["O","O","O"],[" "," ","O"]],"O") == (3,1))

print_test(winning_diagonal([["X","X","O"],["O","O","O"],["X"," ","O"]],"O") == (-1,-1))

print_test(winning_diagonal([[" ","X","O"],["O"," ","O"],[" "," "," "]],"O") == (-1,-1))

print_test(winning_diagonal([[" ","X","O"],["O","O","O"],[" "," "," "]],"X") == (-1,-1))

print_test(winning_diagonal([["X"," "," ","O"],["O","X","O","O"],[" "," ","X","O"],[" "," "," ","O"]],"X") == (-1,-1))

print_test(winning_diagonal([["X","X"," ","O"],["O","X","O","O"],[" "," ","X","O"],[" "," "," "," "]],"X") == (4,4))

print_test(winning_diagonal([["X","X"," ","O"],["O","X","O","O"],[" "," "," ","O"],[" "," "," ","X"]],"X") == (3,3))

print_test(winning_diagonal([["X","X"," ","O"],["O"," ","O","O"],[" "," ","X","O"],[" "," "," ","X"]],"X") == (2,2))

print_test(winning_diagonal([[" ","X"," ","O"],["O","X","O","O"],[" "," ","X","O"],[" "," "," ","X"]],"X") == (1,1))

print("function: winning_vertical")

print_test(winning_vertical([["O","X"," "],["O","O","X"],[" ","O"," "]],"O") == (3,1))

print_test(winning_vertical([["O","X","O"],[" "," ","O"],["O","O","X"]],"O") == (2,1))

print_test(winning_vertical([[" ","O","O"],["O","X","X"],["O"," "," "]],"O") == (1,1))

print_test(winning_vertical([[" "," ","O"],[" ","O"," "],[" ","O"," "]],"O") == (1,2))

print_test(winning_vertical([[" ","O"," "],["O"," ","O"],[" ","O"," "]],"O") == (2,2))

print_test(winning_vertical([["X","O","O"],[" ","O"," "],[" "," "," "]],"O") == (3,2))

print_test(winning_vertical([["O","X"," "],["X","O","O"],["O"," ","O"]],"O") == (1,3))

print_test(winning_vertical([["O","X","O"],[" "," "," "],[" ","O","O"]],"O") == (2,3))

print_test(winning_vertical([[" "," ","O"],["X"," ","O"],["O","O"," "]],"O") == (3,3))

print_test(winning_vertical([["X","O","O"],[" ","O"," "],[" ","X"," "]],"O") == (-1,-1))

print_test(winning_vertical([["X","O","O"],[" "," "," "],["X","X"," "]],"O") == (-1,-1))

print_test(winning_vertical([["X"," "," ","O"],["O","X","O","O"],[" "," ","X","O"],[" "," "," ","O"]],"X") == (-1,-1))

print_test(winning_vertical([["X","X"," ","O"],["O","X","O","O"],[" ","X","X","O"],[" "," "," "," "]],"X") == (4,2))

print_test(winning_vertical([["X","X"," ","O"],["O","X","O","O"],[" "," ","O","O"],[" "," ","O","X"]],"O") == (1,3))

print_test(winning_vertical([["X","X"," ","O"],["O"," ","O","O"],[" ","X","X","O"],[" "," "," ","X"]],"X") == (-1,-1))

print_test(winning_vertical([[" ","X"," ","O"],["O","X","O","O"],[" "," ","X","O"],[" ","X"," ","X"]],"X") == (3,2))

print("function: winning_horizontal")

print_test(winning_horizontal([["O","X"," "],["O","O","X"],[" ","O"," "]],"O") == (-1,-1))

print_test(winning_horizontal([["O"," ","O"],[" "," ","O"],["O","O","X"]],"O") == (1,2))

print_test(winning_horizontal([[" ","O","O"],[" ","X","X"],["O"," "," "]],"O") == (1,1))

print_test(winning_horizontal([[" ","O","O"],[" ","X","X"],["O"," "," "]],"X") == (2,1))

print_test(winning_horizontal([[" "," ","O"],["O","O"," "],[" ","O"," "]],"O") == (2,3))

print_test(winning_horizontal([[" ","O"," "],["O"," ","O"],[" ","O"," "]],"O") == (2,2))

print_test(winning_horizontal([["X","O","O"],[" ","O"," "],[" "," "," "]],"O") == (-1,-1))

print_test(winning_horizontal([["O","X"," "],["X","O","O"],["O"," ","O"]],"O") == (3,2))

print_test(winning_horizontal([["O","X","O"],[" "," "," "],[" ","O","O"]],"O") == (3,1))

print_test(winning_horizontal([[" "," ","O"],["X"," ","O"],["O","O"," "]],"O") == (3,3))

print_test(winning_horizontal([["X"," "," ","O"],["O"," ","O","O"],[" "," ","X","O"],[" "," "," ","O"]],"O") == (2,2))

print_test(winning_horizontal([["X","X"," ","O"],["O","X","O","O"],[" ","X","X","X"],[" "," "," "," "]],"X") == (3,1))

print_test(winning_horizontal([["X","X"," ","O"],["O","X","O","O"],["O"," ","O","O"],[" "," ","O","X"]],"O") == (3,2))

print_test(winning_horizontal([["X","X"," ","O"],["O"," "," ","O"],[" ","X","X","O"],["X","X"," ","X"]],"X") == (4,3))

print_test(winning_horizontal([[" ","X"," ","O"],["O","X","O","O"],[" "," ","X","O"],[" ","X"," ","X"]],"X") == (-1,-1))

#

# These tests should work once you've completed the three functions

#

print("function: computer_turn")

print_test(computer_turn([["O","X"," "],["O","O","X"],[" ","O"," "]]) == (3,1))

print_test(computer_turn([["O"," ","O"],[" "," ","O"],["O","O","X"]]) == (1,2))

print_test(computer_turn([[" ","O","O"],[" ","X","X"],["O"," "," "]]) == (1,1))

print_test(computer_turn([[" "," ","O"],[" ","X","X"],["O"," "," "]]) == (2,1))

print_test(computer_turn([[" ","O"," "],["O","X","X"],["O"," ","X"]]) == (1,1))

print_test(computer_turn([["X","O","O"],[" ","X","X"],["O"," "," "]]) == (2,1))

print_test(computer_turn([["X","O","O"],["O","X","X"],["O"," "," "]]) == (3,3))

print_test(computer_turn([["X"," "," ","O"],["O"," ","O","O"],[" "," ","X","O"],[" "," "," ","O"]]) == (2,2))

print_test(computer_turn([["X","X"," ","O"],["O","X","O","O"],[" ","X","X","X"],[" "," "," "," "]]) == (3,1))

print_test(computer_turn([["X","X"," ","O"],["O","X","O","O"],["O"," ","O","O"],[" "," ","O","X"]]) == (3,2))

print_test(computer_turn([[" ","X"," ","O"],["O","X","O","O"],["O","X","O","O"],["O"," ","O"," "]]) == (1,1))

print_test(computer_turn([["X","X"," ","O"],["O"," "," ","O"],[" ","X","X","O"],["X","X"," ","X"]]) == (4,3))

print_test(computer_turn([["X","X"," ","O"],["O"," "," ","O"],[" ","X","X","O"],["X","X","O","X"]]) == (2,2))

print_test(computer_turn([[" ","X"," ","O"],["O","X","O","O"],[" "," ","X","O"],[" ","X"," ","X"]]) == (3,2))

# Add more calls to further test the functions.

# =======================================================================

# Also add statements that simply print out the tuple returned by computer_turn in

# cases where the random choice is made. Note that the function random_move currently

# has a print statement in it to show the number of empty cells, the random choice,

# and the (r,c) tuple that is returned.

# =======================================================================

print(computer_turn([[" ","X"," "],[" ","O","X"],[" ","O"," "]]))

# Main Program

#

rand_choice = random.Random() # used in random_move function

test_suite()

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

More Books

Students also viewed these Databases questions