Question
PYTHON 3 Overview The goal of this assignment is to create an image of a backgammon board, patterned carefully after the following model (click on
PYTHON 3
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 (Links to an external site.) 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:
- How many checkers lie on the given point
- Which point the checkers are on
- Whether white's checkers are on the top-half or bottom-half of the board.
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:
- two checkers on the first (and 24th) point, with white on top
- five checkers on the sixth (and 19th) point, with white on bottom
- three checkers on the eighth (and 17th) point, with white on bottom
- five checkers on the twelfth (and 13th) point, with white on top
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, as follows:
Enter pixels per grid cell: 25
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 (Links to an external site.). More specifically, we have created a folder named program03 in your repositiory and you should place the following two files within:
-
backgammon.py: this file should contain all of your source code.
-
readme.txt: use our programming assignment template readme.
Grading Standards
The assignment is worth 40 points, apportioned as follows:
- 4 points: Quality of the required 'readme' file
- 4 points: correct rendering of board background and center bar
- 4 points: correct and appropriate placement of text labels
- 4 points: correct placement and coloring for the triangles
- 4 points: correct placement and coloring for the checkers
- 5 points: validity of entire rendering when scaled to various sizes
- 5 points: General readibility of the source code, including well chosen variable names and appropriate inline comments
- 10 points: Appropriate use of control structures in avoiding redundancy of code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started