Question
Using python do the following Without storing the moves we can't check to avoid drawing in the same cell. We'll use a list in which
Using python do the following
Without storing the moves we can't check to avoid drawing in the same cell. We'll use a list in which the items are themselves lists representing a row of the TiTacToe grid. Write the functions initialise_record_of_moves and is_occupied. The former uses the grid (a tuple) to create the list representing the cells in the grid where the strings "X" and "O" can be stored as moves are played. All of the cells (items in the list) should be initialised to " " (a space). The is_occupied function has parameters moves_played (the list representation of the grid of cells) and cell (a pair tuple with row and column) and returns true if the specified cell already has an X or O drawn in it. Your functions must satisfy the test cases in the test harness provided below, and you should add a few more test cases for the is-occupied function.
the test suite is as follows:
#
# Obtain code for turtle graphics
#
import turtle, sys
def is_occupied(moves_played, cell):
''' return True if the cell, a tuple (c,r), is already occupied by a move;
otherwsie return False '''
def initialise_record_of_moves(grid):
''' return a 2-D list of size determined by the rows and columns of grid
initialised with string values = " " '''
#
# ============================================================================
# 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: initialise_record_of_moves")
grid = (2,300,2,300,0,0)
print_test(initialise_record_of_moves(grid)==[[" "," "," "],[" "," "," "],[" "," "," "]])
grid = (3,300,3,300,0,0)
print_test(initialise_record_of_moves(grid)==[[" "," "," "," "],[" "," "," "," "],[" "," "," "," "],[" "," "," "," "]])
print("function: is_occupied")
print_test(is_occupied([[" ","X"," "],[" "," "," "],[" "," ","O"]], (1,2)))
print_test(is_occupied([[" ","X"," "],[" "," "," "],[" "," ","O"]], (3,3)))
print_test(is_occupied([[" ","X"," "],["X"," "," "],[" "," ","O"]], (2,1)))
print_test(not is_occupied([[" ","X"," "],[" "," "," "],[" "," ","O"]], (1,1)))
print_test(not is_occupied([[" ","X","X"],[" "," "," "],[" "," ","O"]], (3,1)))
print_test(not is_occupied([[" "," "," "," "],[" "," "," "," "],[" "," "," "," "],[" "," "," "," "]], (1,2)))
print_test(is_occupied([[" "," "," "," "],[" "," "," "," "],[" "," "," "," "],[" "," ","O"," "]], (4,3)))
print_test(is_occupied([[" "," "," "," "],[" "," "," ","X"],[" "," "," "," "],[" "," "," "," "]], (2,4)))
print_test(is_occupied([[" "," "," ","O"],[" "," "," "," "],[" "," "," "," "],[" "," "," "," "]], (1,4)))
print_test(is_occupied([[" "," "," "," "],[" "," "," "," "],["X"," "," "," "],[" "," "," "," "]], (3,1)))
# Add more calls to further test the functions.
# =======================================================================
#
# Main Program
#
test_suite()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started