Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We can now determine which cell of the grid a given point (x,y) is in. To draw an X or an O in that cell

We can now determine which cell of the grid a given point (x,y) is in. To draw an X or an O in that cell we need to determine the center of the cell, and then use the draw_x or draw_o functions from assignment 5. Write two functions that each take as parameters the row and column number of a cell and the values that specify a grid. One of them returns the x value for the center of the cell and the other returns the y value of the center of the cell. The names for these two functions and the full parameter lists are specified in the test harness file given below. Use the test harness to test your functions, being sure to add further test cases. After testing the function add statements to the main program to draw a TicTacToe grid and then ask the user to enter an x and a y value; if the entered (x, y) is in the grid area, determine which cell and draw an X in that cell. Ask the user to enter a second (x, y) and provided its in the grid area determine the cell and draw an O in that cell.

The code to be used as follows:

#

# Obtain code for turtle graphics

#

import turtle, sys

# New functions to be written

#

def cell_center_x(row, col, nv, lv, nh, lh, cx, cy):

'''return the x value for the center of the cell specified by row and

col in the grid specified by nv, lv, nh, lh, cx, cy'''

def cell_center_y(row, col, nv, lv, nh, lh, cx, cy):

'''return the y value for the center of the cell specified by row and

col in the grid specified by nv, lv, nh, lh, cx, cy'''

# I'm providing my versions of the functions you've previously written.

# You can replace them with your own or use these as you see fit.

def draw_x (p, x, y, size):

""" position turtle p at (x, y) and draw X of specified size """

#

# Draw the four arms of the X by starting at the center and pointing 45

# degrees, drawing an arm and turning 90 degrees.

# First move the turtle to (x, y) and then make it point 45 degrees

# ready to draw the first arm of the X

#

p.penup()

p.goto(x,y)

p.setheading(45)

p.pendown()

#

# from (x,y) draw the four arms of the X of length size/2

#

for i in [1,2,3,4]:

p.forward(size/2)

p.penup()

p.goto(x, y)

p.pendown()

p.left(90)

def draw_circle(t, x, y, r):

'''Draw circle with turtle t at center and of radius r.'''

#

# method of Turtle class draws a circle with center r to left of

# heading direction

#

# first set heading to east and move to r south of center

#

t.setheading(0)

t.penup()

t.goto(x, y-r)

t.pendown()

t.circle(r)

def draw_line(t, cx, cy, l):

''' draw a line in whatever direction turtle t is heading centered at

(cx, cy) of length l, leave turtle in same heading'''

t.penup()

t.goto(cx, cy)

t.forward(-l/2)

t.pendown()

t.forward(l)

def draw_vertical_lines(t, cx, cy, l, n, dx):

''' with turtle t draw n vertical lines centered at (cx, cy) and

dx apart.'''

t.penup()

t.goto(cx,cy)

t.setheading(90)

x = cx-dx*(n-1)/2

t.goto(x, cy)

for i in range(n):

draw_line(t, x, cy, l)

x += dx

def draw_horizontal_lines(t, cx, cy, l, n, dy):

''' with turtle t draw n horizontal lines centered at (cx, cy) and

dy apart.'''

t.penup()

t.goto(cx,cy)

t.setheading(0)

y = cy-dy*(n-1)/2

t.goto(cx, y)

for i in range(n):

draw_line(t, cx, y, l)

y += dy

def draw_grid(t, n_v, l_v, n_h, l_h, cx, cy):

x_spacing = l_h/(n_v+1)

draw_vertical_lines(t, cx, cy, l_v, n_v, x_spacing)

y_spacing = l_v/(n_h+1)

draw_horizontal_lines(t, cx, cy, l_h, n_h, y_spacing)

def is_outside_grid(cx, cy, sx, sy, x, y):

''' return true if (x,y) is outside grid centered at (cx, cy) of size

sx horizontally and sy vertically'''

if (x < cx - sx/2 or x > cx + sx/2):

return True

elif y < (cy - sy/2) or y > (cy + sy/2):

return True

else:

return False

def column(nv, lv, nh, lh, cx, cy, x, y):

''' Return column of the grid with nv vertical lines and horizontal

lines of length lh, centered at (cx,cy), that contains (x, y). '''

#

# dx is the distance between grid lines, x_grid_line is initially the

# left border of the grid

#

dx = lh / (nv+1)

x_grid_line = cx - lh/2

cell = 0

#

# if x is on the left border return column = 1

#

if x <= x_grid_line:

return 1

#

# iterate, moving to next grid line until x_grid_line is greater than x

#

while x > x_grid_line:

cell += 1

x_grid_line += dx

return cell

def row(nv, lv, nh, lh, cx, cy, x, y):

''' Return row number of the grid with nh horizontal lines and vertical

lines of length lv, centered at (cx,cy), that contains (x, y). '''

#

# dy is the distance between grid lines, y_grid_line is initially the

# lower border of the grid

#

dy = lv / (nh+1)

y_grid_line = cy - lv/2

cell = 0

#

# if y is on the lower border return row = 1

#

if y == y_grid_line:

return 1

#

# iterate, moving to next grid line until x_grid_line is greater than x

#

while y > y_grid_line:

cell += 1

y_grid_line += dy

return cell

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

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

# lines

def print_test(did_pass):

''' print result of testing function '''

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 '''

# Not all the parameters nh, lv, cy and y are used - but

# we keep the parameter lists the same for consistency.

# Remember parameters are:

# ( row,col,nv,lv, nh, lh, cx,cy)

#

print_test(cell_center_x(1, 1, 2, 300, 2, 300, 0, 0) == -100)

print_test(cell_center_y(1, 1, 2, 300, 2, 300, 0, 0) == -100)

print_test(cell_center_x(1, 2, 2, 300, 2, 300, 0, 0) == 0)

print_test(cell_center_y(1, 2, 2, 300, 2, 300, 0, 0) == -100)

print_test(cell_center_x(1, 3, 2, 300, 2, 300, 0, 0) == 100)

print_test(cell_center_y(1, 3, 2, 300, 2, 300, 0, 0) == -100)

print_test(cell_center_x(2, 1, 2, 300, 2, 300, 0, 0) == -100)

print_test(cell_center_y(2, 1, 2, 300, 2, 300, 0, 0) == 0)

# Add more calls to properly test the functions

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

#

# Main Program

#

test_suite()

# Set up window and initialise turtle

#

window = turtle.Screen()

pen = turtle.Turtle()

pen.hideturtle()

pen.pensize(4)

pen.speed(12)

# define the tictactoe grid: size 300 and draw it centered at (0,0)

grid_size = 300

draw_grid(pen, 2, grid_size, 2, grid_size, 0, 0)

# Ask user to enter a point, and draw an X in the cell

# containing the point, provided the point is within the grid.

x = int(window.numinput("x coord", "Enter x coordinate of X or O: "))

y = int(window.numinput("y coord", "Enter y coordinate of X or O: "))

# check the point is not outside the grid area, determine which cell on the

# grid the entered (x,y) coordinate is in and draw an X in that cell

# Ask user to enter second point, and draw an O in the cell

# containing the point, provided the point is within the grid.

#

x = int(window.numinput("x coord", "Enter x coordinate of X or O: "))

y = int(window.numinput("y coord", "Enter y coordinate of X or O: "))

# check the point is not outside the grid area, determine which cell on the

# grid the entered (x,y) coordinate is in and draw an O in that cell

window.mainloop()

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 ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions

Question

What is the timecost trade-off methodology, and when is it used?

Answered: 1 week ago

Question

How can either be made stronger?

Answered: 1 week ago