Although we typically hesitate to offer any particular implementation as a "solution", we thought it might be informative to see how control structures can be used to avoid any semblence of repetition of code for this project.
###############################################
# Demonstration of use of control structures to
# avoid redundancy in Backgammon project
#
# Author: Michael Goldwasser
###############################################
from cs1graphics import *
###############################################
# create initial canvas based on user input
###############################################
size = int(raw_input('How many pixels per "grid" cell? '))
board = Canvas(15 * size, 13 * size, 'burlywood4', 'Backgammon')
###############################################
# add the felt and dividing line
###############################################
for centerX in (4, 11): # two rectangles, one centered at 4*size, one at 11*size
felt = Rectangle(6 * size, 11 * size)
felt.setFillColor('navajowhite')
felt.move(centerX * size, 6.5 * size)
felt.setDepth(60)
board.add(felt)
line = Path(Point(7.5 * size, 0), Point(7.5 * size, 13 * size))
line.setBorderWidth(2)
board.add(line)
###############################################
# There are lots of times that we will need to know the x-value at which
# a "point" on the backgammon board belongs. So let's precompute a list
# so that x[pt] is that value for 1 to 24 (and we'll just leave x[0] as junk)
###############################################
x = [0] * 25
for pt in range(1, 25):
if pt <= 12:
column = pt
else:
column = 25 - pt # so 24->1, 23->2, 22->3, 21->4, ...
if column >= 7:
column += 1 # everything on rightside moves over by one due to middle bar
x[pt] = size * (column + 0.5) # this is center x-coordinate
###############################################
# add the numbers and triangles
###############################################
for pt in range(1, 25):
if pt >= 13:
dy = -1 # orient upward from center (i.e. top of board)
else:
dy = +1 # orient downward from center (i.e. bototm of board)
t = Text(str(pt))
t.setFontSize(int(round(size / 3.0))) # scale font based on grid
t.move(x[pt], size * (6.5 + 6 * dy))
board.add(t)
point = Polygon(Point(x[pt] , size * (6.5 + dy*0.5)), # tip
Point(x[pt] + 0.5*size, size * (6.5 + dy*5.5)), # rightbase
Point(x[pt] - 0.5*size, size * (6.5 + dy*5.5))) # leftbase
if (pt % 2 == 1):
point.setFillColor('darkorange3')
else:
point.setFillColor('tan')
board.add(point)
###############################################
# add the checkers
###############################################
checkScale = 0.9
for num,pt,onTop in ((2,1,True), (5,6,False), (3,8,False), (5,12,True)):
for dy in (-1, +1): # top then bottom
baseY = size * (6.5 + dy * 5.5)
for c in range(num):
checker = Circle(checkScale * size / 2)
checker.move(x[pt], baseY - dy * size * checkScale * (0.5+c))
checker.setDepth(40)
if ((dy == -1) == onTop):
checker.setFillColor('white')
else:
checker.setFillColor('black')
board.add(checker)