Programming Assignment #3:   Backgammon Board

Assigned:    Wednesday, Sept. 28
Due:    Friday, Oct. 7    by midnight

Contents:


Collaboration Policy

For this assignment, you are allowed to work with one other student if you wish (in fact, we suggest that you do so). If any student wishes to have a partner but has not been able to locate one, please let the instructor know so that we can match up partners.

Please make sure you adhere to the policies on academic integrity in this regard.


Overview

Topic:   Control Structures -- Loops and Conditionals
Related Reading:   Chapter 4

This assignment is worth 10 points.

The goal of this assignment is to create an image of a backgammon board, patterned carefully after the following model (click on image to zoom):

Backgammon is a two-player game played on a board with 24 triangles known as points. Each player starts with 15 checkers, with an initial configuration as shown above. The points alternate in color as you go around the board, and there is a clear top-to-bottom symmetry to the placement of the checkers.

We have chosen this assignment as a way to exercise your use of control structures. Although the board is fixed and finite, and you could presumably create the image above by manually configuring the geometry for 24 different triangles, 30 circles, 24 text labels, and so on, you will only receive significant credit if you use control structures to express these patterns. As a general rule, if you ever find yourself "copying-and-pasting" code from one part of a program to another, there should be a more elegant way to express the repetition and variation.


New Concept

So far, in discussing for loops, we've only seen cases in which there is just one loop variable, such as the case:

for item in groceryList:
where item starts at the first element of the list fruitList, and then iterates through the additional items in the list, at a time. In this case, there is only one variable, item, that is changing in each iteration of the loop.

We've also seen cases with numbers, like:

for num in range(0,10):
where the loop variable num starts at the first value in range (0, here), and then iterates up to, but not including, the end value in the range (10, here). Again, only the one loop variable, num, is changing in each iteration.

Python allows us to change multiple loop variables at once, using tuples. By storing the different variables in a list of tuples, you are allowed to set multiple variables to the values in those tuples. For example, we could do something like

for x,y in [ (1,2),  (7,5),  (4,8),  (3,13), (2,15) ]:
as a way to describe a loop with five passes, such that in the first pass x=1 and y=2, and in the second pass x=7 and y=5, and so on.

We'll see shortly how this capability can help us with placement of the backgammon pieces.


Advice

To begin, we suggest designing the board without the checkers. The geometry can be conveniently modeled on a 15-by-13 grid as follows:

You should maintain a size variable that determines the width of each of those grid cells, so that you can do all other geometry computations based on the concept of such a width. This will allow you to draw the board at whatever scale you want, while keeping the rest of your geometric computations relatively clean.

You will notice that the points themselves are labeled from 1 to 24 in a clockwise fashion starting at the bottom-left. Each triangle has a width of precisely one cell and a height of 5 cells. There is a vertical separation of one cell width between the top and bottom points, as well as the horizontal separation between the left-half and right-half of the board.

The only place that we deviate from the unit grid size measure is for the checkers. Although they appear to have a diameter of one cell width, they are actually slightly smaller. In particular, we have given each a diameter of 0.9 relative to the cell width, and we have stacked them against each other based on their actual size; this makes a better appearance as more of the underlying point shows. But this means that the checker centers are not aligned precisely with the center of the grid cells.

The other issue with the checkers is that we would like you to use control structures rather than to draw 30 different checkers, but the "pattern" of the initial checkers is not regular. What is needed to code this cleanly is to understand what aspects of the checkers change. There are three such aspects:

To best manage this complexity, we simply need to carefully encode the settings for these choices. In particular, there are four sets of checkers, going from left to right: To get this type of irregular repetition, we can rely on a for-loop that uses an explicit sequence of values to encode the pattern we seek. In particular, we can hardcode the pattern using the following syntax.
for num,pt,whiteOnTop in [ (2,1,True), (5,6,False), (3,8,False), (5,12,True) ]:
As explained above in New Concept, this means that in the first iteration, the three variables num, pt, and whiteOnTop take on the values of 2, 1, and True, respectively. Then in the second iteration, they have the values of 5, 6, and False, and so on.

In essence, this for loop supports the four different repetitions we need to place our pieces, with each repetition defined by a tuple that designates:

Color Choices

You do not need to match all of our colors, but if you're interested, our points alternate between darkorange3 and tan, the frame is burlywood4 and the background behind the points is navajowhite.


Requirements

Your program must start out by asking the user for the value of the number of pixels per grid-cell. Then, pick the appropriate canvas size and lay out the board according to that scale.


Submitting Your Assignment

You should create a file, backgammon.py, which contains all of your own code.

You should also submit a separate 'readme' text file, with the requisite information.

You should submit these two files electronically by emailing them to the instructor.