[Edu-sig] Turtle work (1 of 2)

John Posner jjposner at snet.net
Fri Dec 21 21:27:37 CET 2007


Here's some work I did a few months ago, prompted by visiting Andy Judkis's
class in a NJ magnet high school, where he teaches some Python programming.
This is well-trod ground, but I think "more is better" when it comes to
having practical answers to the beginner's very reasonable question, "why
start counting at 0 instead of 1?".

Many classes doing graphics with the standard Python "turtle" module reach
the point where the instructor says, "We've seen how easy it is to draw a
white square, and to draw a black square. Now, write a program that creates
a full chess board, with white and black squares."

One strategy is to repeatedly (1) using "setx" and "sety" to place the
turtle at the corner of the next square, then (2) drawing the square. It's
easy to see how 0-based counting makes it easier to determine where to start
drawing a particular square:

  # determine the row and column (0-based count vs. 1-based count)

  # 0-BASED
  row = square_num // SQ_IN_ROW  # integer division
  col = square_num % SQ_IN_ROW

  # 1-BASED
  # row = ((square_num - 1) // SQ_IN_ROW) + 1   # integer division
  # col = ((square_num - 1) % SQ_IN_ROW) + 1

(Admittedly, jumping around like this is not the most "turtle-like" of
strategies!)

The ease with which you can answer the question, "what row/column position
is square #43 at?" is pretty nifty. As a bonus, we have a good practical
application of integer arithmetic and modular arithmetic.

The remainder of this message contains the entire chess-board program. There
are 0-based vs. 1-based alternatives at several points in the code.

"""
draw a chess board, using 0-based or 1-based counting
"""

import sys
from turtle import *

#################### global variables

# number of squares in each row, and number of rows
SQ_IN_ROW = 8
# length of a square's side
SIDELEN = 40

#################### classes / functions

def DrawBox(square_num):
	"""
	draw one square of the chess board
	uses global: tur
	"""
	
	# determine the row and column (0-based count vs. 1-based count)
	# 0-BASED
	row = square_num // SQ_IN_ROW     # integer division
	col = square_num % SQ_IN_ROW
	# 1-BASED
	# row = ((square_num - 1) // SQ_IN_ROW) + 1     # integer division
	# col = ((square_num - 1) % SQ_IN_ROW) + 1

	# set color of this square (0-based count vs. 1-based count)
	# 0-BASED: different rules for even/odd rows
	if (row%2 == 0 and col%2 == 0) or (row%2 == 1 and col%2 == 1):
	# 1-BASED: turns out to be "same as above"
	# if (row%2 == 1 and col%2 == 1) or (row%2 == 0 and col%2 == 0):
		fill_box = True
	else:
		fill_box = False
	
	tur.up()
	# go to lower-left corner of this square
	tur.setx(col*SIDELEN)
	tur.sety(row*SIDELEN)
	tur.setheading(0)

	# start filling (maybe)
	if fill_box:
		tur.begin_fill()
	# start drawing
	tur.down()
	for i in range(4):
		tur.forward(SIDELEN)
		tur.left(90)
	# end filling (maybe)
	if fill_box:
		tur.end_fill()

#################### main routine
	
if __name__ == '__main__':	
	# this global is used in functions
	tur = Pen()
	# use a loop to draw all the squares (0-based count vs. 1-based
count)
	# 0-BASED
	for n in range(SQ_IN_ROW * SQ_IN_ROW):
	# 1-BASED
	# for n in range(1, SQ_IN_ROW * SQ_IN_ROW + 1):
		DrawBox(n)
	junk = raw_input("Press a key ...")
	sys.exit(0)

--
John Posner
5 Bayberry Ridge Rd
Westport, CT 06880
203-222-4952
jjposner at snet.net



More information about the Edu-sig mailing list