I would like to create a function using the code below called drawMove which takes 2 parameters (x or 0) and square number. That draws an x or 0 in the numbered boxes on the grid below. Input should be:

drawMove(turtle,cellsize,O,6) ---> this should draw a O in the 6th box on the grid. Anyone know how I can go about this?

``````"""
def drawLine(t, x0, y0, x1, y1):
# get t's original position
x, y = t.pos()

# go to the initial point
t.up()
t.goto(x0, y0)

# draw a line to the end point
t.down()
t.goto(x1, y1)

# return t to its original position
t.up()
t.goto(x,y)

"""
1     3

|     |
|     |
6  _____|_____|_____  7
|     |
|     |
4  _____|_____|_____  5
|     |
|     |
(0,0).     |     |

0     2

Draw a TicTacToe grid, using turtle t, consisting of
four lines. Draw the grid by calling function drawLine
four times. Draw the grid in the upper right quadrant of
the screen, so that the lower left point of the grid has
coordinates (0, 0). Return t to its initial position before
exiting.

Each cell of the TTT grid is a square with side length
size. The end points of the four lines are illustrated
in the grid above. Thus, point 0 has coordinates (x0,y0).
The four lines of the grid connect points 0 and 1, 2 and
3, 4 and 5, 6 and 7, respectively.

Note the use below of the Python multiple assignment
statement. For example, the statement

a, b = 2, 3

assigns a the value 2 and b the value 3.
"""
def tttDrawGrid(t, size):
# get t's initial position
x, y = t.pos()

# calculate the endpoints of the four grid lines
x0, y0 = size, 0
x1, y1 = size, 3*size
x2, y2 = 2*size, 0
x3, y3 = 2*size, 3*size
x4, y4 = 0, size
x5, y5 = 3*size, size
x6, y6 = 0, 2*size
x7, y7 = 3*size, 2*size

# draw the four grid lines
drawLine(t, x0, y0, x1, y1)
drawLine(t, x2, y2, x3, y3)
drawLine(t, x4, y4, x5, y5)
drawLine(t, x6, y6, x7, y7)

# return t to its original position
t.up()
t.goto(x, y)``````

You have to know the relative coordinates of the square and create text usually at a middle location. You will probably want to set the font to use and it's size. To modify your drawing

``````X1      X2    X3    X4
Y1      |     |
|     |
Y2 _____|_____|_____
|     |
|     |
Y3 _____|_____|_____
|     |
|     |
Y4      |     |``````

you have to know, or be able to calculate these points to draw the lines and place the text. So the 6th square=row 2, column 3. Place and "X" or "O" half way between X3 and X4 for the x value and half way between Y2 and Y3 for the y value. This info can be stored in a list so you can pass the square number and use the list to convert the square number to begin and end coordinates for x and y. If you have problems with the code you generate, post back with the code, and ask any specific questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.