Course Home | Class Schedule | Moodle CMS | Git Submission | Perusall | Tutoring

Saint Louis University

Computer Science 1300/5001
Introduction to Object-Oriented Programming

Michael Goldwasser

Fall 2019

Computer Science Department

Programming Assignment 02

Backgammon

Due: 11:59pm, Monday, 30 September 2019


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

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.


Advice

To begin, we suggest designing the board without the checkers. Although we do not draw the board in this way, it may help to model the geometry by thinking about locations as overlayed 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)]:

This is a bit new, in that we are using a for loop to set a triple of variables rather than just one. That is, we're used to seeing something like

for x in sequence:

But if the sequence is a sequence of tuples, you are allowed to set multiple variables to the values in those tuples. That is, 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.

Going back to our backgammon applications, we have four different repetitions we need, where each one is defined by a triple designating the number of checkers, the point for those checkers, and whether white checkers are at the top of the board (rather than the bottom).

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 with such a scale.


Submitting Your Assignment

This project must be submitted electronically using our department's git repository. More specifically, we have created a folder named program02 in your repositiory and you should place the following two files within:

For further technical details about use of our department git server, see instructions here.

See as well a discussion of the late policy for programming assignments.


Grading Standards

The assignment is worth 40 points, apporitioned as follows:


Michael Goldwasser
Last modified: Saturday, 21 December 2019